Lec 7.2-Servlet
Lec 7.2-Servlet
Objectives
• Apache Tomcat
• http://jakarta.apache.org/tomcat/
• IDE: NetBeans, Eclipse
• https://netbeans.org/
• https://eclipse.org/
• Some Tutorials:
• Creating Servlet in Netbeans:
http://www.studytonight.com/servlet/creating-servlet-in-
netbeans.php
• Java Servlet Example:
http://w3processing.com/index.php?subMenuId=170
Compiling and Invoking Servlets
• Organization
• Related files grouped together in a single directory
hierarchy.
• HTML files, JSP pages, servlets, beans, images, etc.
• Portability
• Most servers support Web apps.
• Can redeploy on new server by moving a single file.
• Separation
• Each Web app has its own:
• ServletContext, Class loader
• Sessions, URL prefix, Directory structure
Structure of a Web Application
Servlet
Java Servlets
• Servlets can
• create dynamic (HTML) content in response to a request
• handle user input, such as from HTML forms
• access databases, files, and other system resources
• perform any computation required by an application
Java Servlets
• Compile:
• Need Servlet.jar. Available in Tomcat package
.service(HttpServletRequest req,
HttpServletResponse res)
invoked for each HTTP request
parameters encapsulate the HTTP request and response
.destroy()
invoked when the servlet is unloaded
(when the servlet container is shut down)
Servlet Methods
.doGet()
.service() .doPost()
.doHead()
… etc.
Methods
package servlet;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlet.HelloServlet</servlet-
class>
</servlet> The servlet's package
and class names
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
The pathname used to invoke the servlet
(relative to the web application URL)
Environment Entries
• in web.xml:
<env-entry-description>password</env-entry-
description>
<env-entry>
<env-entry-name>UserId</env-entry-name>
<env-entry-value>Xy87!fx9*</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
Environment Entries
try {
Context envCtx = (Context)
(new InitialContext()).lookup("java:comp/env");
password = (String) envCtx.lookup("password");
} catch (NamingException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Handling HTML Forms
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Simple Form Servlet
<form action="hello" method="post" >
<p>User Id:<input type="text" name="userid" /></p>
<p><input type="submit" value="Say Hello" /></p>
</form>
out.println("<html><head><title>Hello</title></he
ad>"
+ "<body><p>Hello, " + userId
+ "!</p></body></html>");
out.close();
}
State Management
• Name: session-id
• Content: 104-1898635-929144
• Expiration Date: Monday, June 29, 2009 3:33:30 PM
• Domain: .ehsl.org
• Path: /slms
• Secure: no
Client
Cookie Session
[session-id] Memory
session
• The methods
session.setAttribute(key, value)
session.getAttribute(key)
store and retrieve session memory
• key is a string; value can be any object
• For example,
session.setAttribute("userid", userId);
String userId =
(String)session.getAttribute("userid");
Problem
30
Initial Solution
HTML
page
JSP page
34
Application Programming Structure
beer_v1
src web
BeerSelect BeerExpert
.java .java
Structure of Folder Development
beer_v1 webapps tomcat
classes
com web.xml
example
web model
BeerSelect BeerExpert
.class .class
form.html
<form method="POST“
action="SelectBeer.do">
Select beer characteristics
<p>Color:
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select>
<center> <input type="SUBMIT"> </center>
</form>
web.xml
41
Servlet BeerSelect – Version 2
43
Current Architecture of the Application
Desired Application Architecture
Result.jsp
request.setAttribute("styles", result);
RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
Application Test
48
Review
• Java servlets
• Servlet methods and operation
• HTML forms and servlets
• HTTP cookies
• Web application state management
• Beer Recommendations with MVC model