java servlet · dynamic: html document is generated by a program in response to an http request 3...
TRANSCRIPT
AGENDA
Introduction
Java Servlet
Web/Application Server
Servlet Life Cycle
Web Application Life Cycle
Servlet API
Writing Servlet Program
Summary
2
INTRODUCTION
Web server response can be static or dynamic
Static: HTML document is retrieved from the file system and
returned to the client
Dynamic: HTML document is generated by a program in
response to an HTTP request
3
JAVA SERVLET … (CONT.)
Java Technology for producing dynamic server responses
ClientServlet
HTTP
request
Client readable
response
Web S
erv
er
Servlet Container
Other
Services
Servlet
Servlet
Invokes
Servlet
Gives
response
4
SERVLET VS. APPLICATION
Servlets do not have a main()
Entry point to servlet code is via call to a method (e.g. doGet())
Servlet interaction with end user is indirect via request/response
object APIs
Primary servlet output is typically HTML
8
SERVLET LIFE CYCLE
Controlled by the container
The container performs the following steps:
1. If an instance of the servlet does not exist, the web container
a) Loads the servlet class.
b) Creates an instance of the servlet class.
c) Initializes the servlet instance by calling the init method
2. Invokes the service method, passing request and response
objects.
3. If it needs to remove the servlet, the container finalizes the servlet
by calling the servlet’s destroy method.
9
WEB APPLICATION LIFECYCLE
The process for creating, deploying, and executing a web
application can be summarized as follows:
1. Develop the web component code.
2. Develop the web application deployment descriptor, if
necessary.
3. Compile the web application components and helper classes
referenced by the components.
4. Optionally, package the application into a deployable unit.
5. Deploy the application into a web container.
6. Access a URL that references the web application.10
SERVLET API
Contained in two packages
javax.servlet
javax.servlet.Http
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
11
WRITING SERVLET PROGRAM
Creating and Initializing a Servlet
Use the @WebServlet annotation
contains metadata about the servlet
specify at least one URL pattern
Extend javax.servlet.http.HttpServlet class
12@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
WRITING SERVLET PROGRAM … (CONT.)
Override service method
The term service method is used for any method in a servlet class
that provides a service to a client.
The service method of a GenericServlet object
The doMethod methods of an HttpServlet object
or in any other protocol-specific methods
13
The general pattern for a service method
to extract information from the request,
access external resources, and
then populate the response, based on that information.
14
public void doGet(HttpServletRequest request,
HttpServletResponse response)
WRITING SERVLET PROGRAM … (CONT.)
WRITING SERVLET PROGRAM … (CONT.)
Getting Information from Requests
ServletRequest interface defines methods for accessing the
following information:
Parameters: to convey information between clients and servlets
Object-valued attributes: for communication between the web
container and a servlet or between collaborating servlets
Information about the protocol: used to communicate the
request and about the client and server involved in the request
Information relevant to localization
15
WRITING SERVLET PROGRAM … (CONT.)
HttpServletRequest
http://[host]:[port][request-path]?[query-string]
request-path
Context path
Servlet path
Path info
query-string
Parameters
Values
16
WRITING SERVLET PROGRAM … (CONT.)
ServletResponse interface
Retrieve an output stream to use to send data to the client
To send character data, use the PrintWriter returned by the response's getWriter method.
To send binary data in a Multipurpose Internet Mail Extensions (MIME) body response, use the ServletOutputStream returned by getOutputStream.
To mix binary and text data, as in a multipart response, use a ServletOutputStream and manage the character sections manually.
Indicate the content type (for example, text/html)
setContentType(String)
HTTP response objects: javax.servlet.http.HttpServletResponse
17
HTTP://LOCALHOST:8080/LECTUREADVANCEDJAVAWEB/HELLOSERVLET
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<body>");
out.print("<h2>Hello World!</h2>");
out.print("</body>");
out.print("</html>");
}
}
18
SUMMARY
Is a server-side program
Needs Web server for execution
Communication through Requests/Response
javax.servlet and javax.servlet.http packages
19