CS119 L5 Servlets
CS119 L5 Servlets
Enterprise Systems
Programming
Servlets
Servlets: server-side Java programs that enable
dynamic processing of web-based requests
Web-based requests are coursed through html
forms
Servlets process these requests and typically
generates html-formatted responses to these
requests
Servlets resides on a web server that supports
servlet functionality
e.g., Apache Tomcat
These servers are also called web containers or servlet
containers
Web application structure
A web application consists of several files
placed inside a context root folder
Structure:
<context-root-folder-name>
html file(s) referring to servlet pages
WEB-INF
web.xml
classes
<package-name>
Java servlet class file(s)
Web-application example
Simple Example:
mywebapp
welcomeform.html
WEB-INF
web.xml
classes
servlets
WelcomeServlet.class
Web-application example
Simple Example:
refers to servlet page
mywebapp (html source need not reside
inside mywebapp)
welcomeform.html
WEB-INF
contains mapping(s) of URL
web.xml to actual servlet code
classes
servlets
WelcomeServlet.class servlet code
could be an elaborate
package folder hierarchy
Web-application example
Simple Example:
…
<form action="/mywebapp/welcome"
mywebapp …
welcomeform.html
WEB-INF
maps "/welcome"
web.xml to servlets.WelcomeServlet
classes
servlets
WelcomeServlet.class
web.xml contents
web.xml: deployment descriptor
Most important portions:
Establishing aliases: associate servlet name to
actual servlet code
Mapping: associates a URL to a servlet name
web.xml example
<web-app>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>
servlets.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
HTML forms
HTML form: section of a webpage that
contains input elements (e.g., text boxes)
Often contains a submit element
(a button) that enables submission of the
form contents to a web server
Submission is associated with an action
(the URL of the page that processes the
form)
HTML form example
<form action="/mywebapp/welcome"
method="get">
TYPE IN SOME TEXT
<input type = "text" name ="firstname" />
<input type = "submit" value="Click" />
</form>
Form methods
Two types of form submission methods
GET: form data is appended to the URL
(data separated from the action URL by
question mark)
Use this for query-type actions or indempotent
actions
POST: form data is “passed” or included as
a message to the web-server
Use this for actions with side-effects
HttpServlet class
Class that servlets extend
Expect to override at least one of the following
methods:
protected doGet( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
protected doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
Read form data through the request parameter,
generate output/resulting webpage through the
response parameter
HttpServletRequest
Most important method: getParameter()
Given the input parameter name (indicated in the
HTML form), returns a string that represents the
associated form data
May need to use Java conversion features (such
as Integer.parseInt()) to convert to the
appropriate type for processing
Examples:
String name = request.getParameter( "firstname" );
int year = Integer.parseInt(
request.getParameter( "year" ) );
HttpServletResponse
Used to facilitate result of processing a form
“generates” html content
Invoke the getWriter() method to get a
PrintWriter handle, then issue print, println
methods on that handle
Invoke getContentType(“text/html”) to specify
that you are generating html content
Example:
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println( "<h2> Welcome </h2>" );
Complete doGet() example
protected void doGet( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
String name = request.getParameter( "firstname" );
int favoriteNumber = name.length();
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();