Download presentation
Presentation is loading. Please wait.
Published byHolly Page Modified over 9 years ago
1
Chapter 4 Request and Response
2
Servlets are controlled by the container
4
Servlet interface Servlet programming is made possible through the classes and interfaces of two packages: javax.servlet and javax.servlet.http. Of these classes and interfaces, the javax.servlet.servlet interface is the most important interface. –All servlets must implement this interface or extend a class that does.
5
Servlet interface The Servlet interface has five methods, whose signatures are as follows: –public void init(ServletConfig config) throws ServletException –public void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException –public void destroy() –public ServletConfig getServletConfig() –public java.lang.String getServletInfo()
6
Servlet lifecycle The servlet lifecycle is simple; there is only one main state – initialized If the servlet isn’t initialized, then it is either –Being initialized –Being destroyed –Or simply does not exist
7
Servlet lifecycle
8
The three big lifecycle moments –init() –service() –doGet() and/or doPost() doGet() and doPost() are invoked by service()
9
Servlet lifecycle init() –When it’s called The container calls init() on the servlet instance after the servlet instance is created but before the servlet can service any client request –What it’s for Gives you a chance to initialize your servlet before handling any client requests –Do you override it? Possibly –E.g., If you need to initialize a database connection, you can do it here
10
Servlet lifecycle service() –When it is called When the first client request come in, the container starts a new thread or allocate a thread from the pool, and causes the servlet’s service() method to be invoked –Wht it is for This method looks at the request, determines the HTTP method (GET, POST, etc.) and invokes the matching doGet(), do Post(), etc. on the servlet –Do you override it? No
11
Servlet lifecycle doGet(), doPost() –When it is called The service() method invokes doGet() or doPost() based on the HTTP method (GET, POST, etc.) from the request –What is it for You as a servlet developer write your own code in doGet()/doPost() –Do you override it Always at least one of them
12
Servlet lifecycle In a nutshell, a fully functional servlet container does the following for each HTTP request for a servlet: –When the container starts up (or first client use), it loads the servlet class, instantiates servlet, and calls its init() method (once only) –For each request, construct an instance of javax.servlet.ServletRequest and an instance of javax.servlet.ServletResponse –Invoke the servlet's service() method, passing the ServletRequest and ServletResponse objects. doGet() and/or doPost() are then called by service() method –When the servlet class is shut down, call the servlet's destroy() method and unload the servlet class.
13
HTTP request Each HTTP request runs in a separate thread –The container runs multiple threads to process multiple requests to a single servlet –Every client request generates a new pair of request and response objects
14
HTTP request
15
Question In the picture above you show two different clients, each with its own thread. What happens if the same client makes multiple requests? Is it one thread per client or one thread per request? Answer: –One thread per request. The Container doesn’t care who makes the request-every incoming request means a new thread.
16
Question When is the servlet loaded into the container? Answer: –This happens either on Container startup or first client use
17
Servlet Initialization
18
Servlet does not exist constructorInit() Constructor of the servlet only makes an object, not a servlet. init() makes the object a servlet and ready to service client request.
19
Servlet Initialization Remember do not put anything in the servlet’s constructor –It is too early to do servlet-specific things in constructor –You can put that in init()
20
Servlet Initialization 1. A ServletConfig Object 2. A ServletContext Object
21
ServletConfig object One ServletConfig object per servlet Use it to pass deploy-time information to the servlet Use it to access the ServletContext Parameters are configured in the Deployment Descriptor
22
ServletContext One Servlet Context per web app Use it to access web app parameters (also configured in the Deployment Descriptor) Use it as a kind of application bulletin- board –Where you can put up messages that other parts of the application can access Use it to get server info –Such as the name and version of the Container, and the version of the API that is supported
23
HttpServletRequest interface extends ServletRequest interface
24
HttpServletResponse interface extends ServletResponse interface
25
Questions Can interface extend interface? Can interface implement interface? What does it mean if some class implements an interface?
26
Request and reponse Keep in mind that HttpServletRequest and HttpServletResponse are interfaces, not classes Also there are no class in J2EE API that implements these two interfaces –It is the Container that implements these two interfaces –The classes aren’t in the API because they’re left to the vendor to implement
27
Request and reponse The client’s request always includes a specific HTTP method. –If the HTTP request method is a GET, the service() method calls doGet() –If the HTTP request method is a POST, the service() method calls doPost()
28
Request and reponse There are other HTTP 1.1 methods besides GET and POST. –They are HEAD, TRACE, OPTIONS, PUT, DELETE, and CONNECT –However, you probably won’t care about these methods
29
Difference between GET and POST POST has a body, GET does not Both GET and POST can send parameters, but with GET, the parameter data is limited to what you can stuff into the Request line
31
No, it is not just about the size
32
Difference between GET and POST When you use GET, the parameter data shows up in the browser’s input bar, right after actual URL (and separated with a “?”) Parameter Name Value ubbget_topic f7 t021696
33
Difference between GET and POST Security is also an issue –You might not want the parameters to be visible Another issue is whether you want the end-users to be able to bookmark the request page –GET requests can be bookmarked –POST requests cannot be bookmarked
34
What determines whether the browser sends a GET or POST request
35
By default, HTTP GET request will be sent to the server Parameters will show up in the browser’s input bar
36
What determines whether the browser sends a GET or POST request Questions: What will happen if you use HTTP GET (or does not specify request method) in the form and do not have a doGet() in your servlet?
37
Difference between GET and POST If you want to support both GET and POST no matter the form sends a GET or POST request, you can do this: That is, put the logic in doGet() and have doPost() method delegate to the doGet() method if necessary
38
Sending parameters each of which has one value
41
Sending parameters each of which has multiple values
44
Request redirect and dispatch You can choose to have another servlet or JSP to handle the response for your request –You can either redirect request to a completely different URL –Or you can dispatch the request to some other component in your web application
45
Request redirect Request redirect makes the browser do the work A redirect lets servlet off the hook completely. After deciding that it cannot do the work, the servlet simply calls the sendRedirect() method
46
Request redirect
50
Request dispatch Request dispatch is different from request redirect –Request dispatch does the work on the server side Redirect makes the client do the work while request dispatch makes something else on the server do the work
51
Request dispatch
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.