0% found this document useful (0 votes)
22 views5 pages

Servlet

Uploaded by

dev5680
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Servlet

Uploaded by

dev5680
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Web Application

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, ...

Directory Structure of a Java


Web Application Directory Structure on CS3
Application Root Directory Application Root Directory www
JSPs and static resources JSPs and static resources

WEB-INF WEB-INF

web.xml web.xml

classes classes

Compiled Java classes Compiled Java classes

lib lib

Additional Java libraries Additional Java libraries

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

Generating HTML Servlet Mapping


HttpServletResponse @WebServlet(<URL Pattern(s)>)
Set content type to “text/html”
 setContentType()
Generate an HTML page
 getWriter().println()
 <html>, <head>, <body> ...

Java Annotations Annotation Examples …


Available since JDK 1.5 (Java 5) Error detection

Data about a program that is not part @Override


protected void doGet()
of the program itself
Can be used by compiler, VM, and other Suppress warning
software tools for various purposes
@SuppressWarnings(“unchecked”)
public List<User> getAllUsers()
{
return (List<User>) new ArrayList();
}

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 )
}

@WebServlet Elements for


@WebServlet URL Patterns
http://download.oracle.com/javaee/6/a value
pi/javax/servlet/annotation/WebServlet.  URL pattern(s) of the servlet
html  The default element
urlPatterns
 Same purpose as value
 Usually used when more than one element is
specified
 Only one of value and urlPatterns can be
specified

@WebServlet Examples Wildcard in Servlet Mapping

@WebServlet( “/HelloServlet” ) A string beginning with a / and ending


@WebServlet( {“/HelloServlet”, “/member/*”} )
with a /*
 E.g. /*, /content/*
@WebServlet( name=“Hello”, urlPatterns={“/HelloServlet”, “/*.html”} )
A string beginning with a *.
@WebServlet(
urlPatterns=”/MyPattern”,
 E.g. *.html, *.do
initParams={@WebInitParam(name="ccc", value="333")}
)

See Servlet Specification 3.0, Section 12

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/*

Why Use init() Instead of


Servlet Life Cycle Constructor
When the servlet is loaded – init() Historical reasons – see
 Executed only once http://csns.calstatela.edu/wiki/content/
 Don’t forget super.init(config) cysun/notes/servlet_data_init
Per request – service() ServletContext cannot be accessed
 dispatch to doXxx() in a constructor
When the servlet is unloaded –
destroy()

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

loadOnStartup Example About web.xml

@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.

Versions Debugging Servlets


Servlet/JSP Spec Tomcat Java Using the Eclipse debugger
3.0/2.2 7.0.x 1.6  Set break points
2.5/2.1 6.0.x 1.5
2.4/2.0 5.5.x 1.4
 Debug As  Debug on Server
View the source of the generated HTML
 View Source in browser
The version attribute of <web-app> in web.xml  Validation
 http://validator.w3.org/
 Use the Web Developer addon of Firefox

You might also like