Java Servlets - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Java Servlets

Description:

throws ServletException, IOException. PrintWriter out = response.getWriter ... throws ServletException, IOException. response.setContentType('text/html' ... – PowerPoint PPT presentation

Number of Views:184
Avg rating:3.0/5.0
Slides: 17
Provided by: quinno
Category:

less

Transcript and Presenter's Notes

Title: Java Servlets


1
Java Servlets
2
Java 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

3
My 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)
4
Notes
  • 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

5
Another 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)
6
The HttpServletRequest Object
  • String getParameter(String key)
  • String getParameterValues(String key)
  • Enumeration getParameterNames()

7
The 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

8
Performance 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

9
Organizational 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

10
Headers and CGI variables
  • Request object has methods to access
  • String value getHeader(String name)
  • Others
  • PrintEnv code
  • http//clotho/snell/TestApps/TestGetPostServlet.h
    tml

11
Cookies
  • 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)

12
Cookies
  • 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()

13
Sessions
  • 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

14
The 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()

15
Example Toy Shop
  • http//clotho/snell/TestApps/ToyShop.html

16
The 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
Write a Comment
User Comments (0)
About PowerShow.com