Title: Java Servlets
1Java Servlets
2Java Servlets
- SUNs answer to CGI
- Run within a Java web server
- We configure Apache to talk to Tomcat
- Steps
- Create instance of your class and load
- Launch thread to call specific methods
- One instance of class, many threads
3My First Servlet
import java.io. import javax.servlet. import
javax.servlet.http. public class HelloWorld
extends HttpServlet public void
doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException PrintWriter
out response.getWriter() out.println(Hello
World)
4Notes
- Default return mime type is text/plain
- Can also implement doPost
- Same arguments
- What if you want to answer to both?
- Running the servlet
- Compile
- You will need the java 2 enterprise edition jar
in your classpath - Put class into WEB-INF/classes
- Invoke ? http//host/context/servlet/HelloWorld
5Another Example
import java.io. import javax.servlet. import
javax.servlet.http. public class HelloWorld
extends HttpServlet public void
doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException response.setCon
tentType(text/html) PrintWriter out
response.getWriter() out.println(ltHTMLgt\n
ltHEADgtlttitlegtHello WWWlt/titlegtlt/HEADgt\n
ltbodygt\n lth1gtHello
World!lt/h1gt\n lt/bodygtlt/htmlgt)
6The HttpServletRequest Object
- String getParameter(String key)
- String getParameterValues(String key)
- Enumeration getParameterNames()
7The HttpServletResponse Object
- void setContentType(String type)
- PrintWriter getWriter()
- ServletOutputStream getOutputStream()
- Used to send binary data back to client
- void setHeader(String name, String value)
- Other methods as well
8Performance Issues
- Strings are immutable
- New string object is created for each string
- Also for each concatenation of 2 strings
- ? a lot of object creation and cleanup
- Use a single StringBuffer object
- Use append() method to add text to it
- Use flush method to push output to client
9Organizational Issues
- WEB-INF/classes can get crowded
- Use java packages to clean things up
- package mypackage //place at top of file
- Put classes in WEB-INF/classes/mypackage
- Invoke http//host/context/servlet/mypackage.Hello
World
10Headers and CGI variables
- Request object has methods to access
- String value getHeader(String name)
- Others
- PrintEnv code
- http//clotho/snell/TestApps/TestGetPostServlet.h
tml
11Cookies
- Use the Cookie class
- Constructor
- Cookie(String name, String value)
- Cookie Methods
- String getDomain() or void setDomain(String dom)
- String getName() or void setName(String name)
- String getValue() or void setValue(String value)
- String getPath() or void setPath(String path)
- boolean getSecure() or void setSecure(boolean
flag) - int getMaxAge() or void setMaxAge(int seconds)
12Cookies
- Create a cookie
- Construct it
- Set values
- Add the cookie to the response before content
type - response.addCookie(Cookie theCookie)
- Get Cookies from request
- Cookie cookies request.getCookies()
13Sessions
- High level API
- HttpSession object
- Built on top of cookies or URL-rewriting
- You dont have to worry which one
- Convenient place to store information
- Arbitrary objects
- Associated with each session
14The Session API
- Get the HttpSession object
- HttpSession session request.getSession(true)
- true automatically creates one if one doesnt
exist - Get and Set information using HttpSession methods
- Object getAttribute(String name)
- void setAttribute(String name, Object value)
- void removeAttribute(String name)
- String getAttributeNames()
- String getID()
- boolean getId()
- long getCreationTime() and long
getLastAccessedTime() - int getMaxInactiveInterval() and void
setMaxInactiveInterval(int sec) - void invalidate()
15Example Toy Shop
- http//clotho/snell/TestApps/ToyShop.html
16The Servlet Life Cycle
- Initialization
- void init() or
- void init(ServletConfig config)
- Be sure to call super.init(config) on first line
- doXxx methods are called (doGet, doPut, etc.)
- Consider if you need single thread
- public class YourServlet extends HttpServlet
implements SingleThreadModel - Destruction
- void destroy()
- dont rely on this. The server could crash