Servlet
Servlet
Components
CS320 Web and Internet Programming Compiled Java classes (.class files)
Introduction to Java Servlets Servlets, beans, filters, ...
Addtional Java libraries (.jar files)
JavaServer Pages (JSPs)
Chengyu Sun Static resources
California State University, Los Angeles HTML, CSS, images, ...
Metadata files
web.xml, ...
WEB-INF WEB-INF
web.xml web.xml
classes classes
lib lib
Directory Structure of an
Eclipse Dynamic Web Project Servlet HelloWorld
import java.io.*;
Application Root Directory WebContent import javax.servlet.*;
import javax.servlet.http.*;
JSPs and static resources
WEB-INF
@WebServlet( “/HelloWorld” )
public class HelloWorld extends HttpServlet {
web.xml public void doGet( HttpServletRequest request,
HttpServletResponse response )
classes build/classes throws ServletExceptoin, IOException
Compiled Java classes {
PrintWriter out = response.getWriter();
lib out.println( “Hello World” );
Additional Java libraries
}
}
1
Some Simple Observations Example: HelloWorld in HTML
Inherits from HttpServlet Modify the HelloWorld servlet to
http://download.oracle.com/javaee/6/api/javax/se output in HTML
rvlet/http/HttpServlet.html
There’s no main() method
doGet()
Input: HttpServletRequest
Output: HttpServletResponse sent back to the
client browser
2
… Annotation Examples About Annotations
Servlet mapping in Sevelet 3.x Specification An annotation may have elements
@WebServlet(“/HelloServlet”) An element has a type (like a variable in
public class HelloServlet extends HttpServlet
Java)
Web service The default element is value
@WebService
{} can be omitted for array values if
public class HashService { there’s only one value in the array
@WebMethod
public String md5( String text )
}
3
Be Careful with URL Patterns Example: RequestCounter
Invalid patterns Display the number of times a servlet is
E.g. /member/*.html, or requested
member/index.html
Conflicting patterns
E.g. two /HelloServlet
Overlapping patterns
E.g. *.html and /member/*
Example:
SharedRequestCounter Application Scope
Use one servlet to count the number of A “storage area” where data can stored
requests, and another servlet to display and accessed
the count Data in application scope will remain
there as long as the application is
running
Data in application scope is shared by
all servlets
4
Access Application Scope loadOnStartup
HttpServlet By default, a servlet is not created until
getServletContext() it is accessed for the first time
HttpServletContext Could cause problem if one servlet must
run before another servlet
setAttribute(String name, Object value)
Give any object a name and save it to Use the loadOnStartup element of
application scope @WebServlet to have a servlet
getAttribute(String name) created during application startup
Retrieve the object from application scope
@WebServlet(
Web application deployment descriptor
name=“Hello”, <web-app>
urlPatterns={“/HelloServlet”, “/*.html”}, version
loadOnStartup=1
)
<welcome-file-list>
More about web.xml in Java Servlet
Specification
The value for loadOnStartup is the order in which
the application server will start the servlets.