chapter 15 introduction to java servlet n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 15 Introduction to Java Servlet PowerPoint Presentation
Download Presentation
Chapter 15 Introduction to Java Servlet

Loading in 2 Seconds...

play fullscreen
1 / 18

Chapter 15 Introduction to Java Servlet - PowerPoint PPT Presentation

Download Presentation
Chapter 15 Introduction to Java Servlet
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chapter 15 Introduction to Java Servlet • What is the Servlet? • Why Servlet? • Write simple Servlet • Lab

  2. What is The Servlet? • Similar to the way applets run on a browser and extend the browser's capabilities, servlets run on a Java-enabled Web server and extend the server's capabilities. • Servlets are Java programs that use the Java Servlet Application Programming Interface (API) and the associated classes and methods. • Servlets extend server capabilities by creating a framework for providing request and response services over the Web.

  3. What is The Servlet? • Java applications that run on Web server • When a client sends a request to the server, the server can send the request information to a servlet and have the servlet construct the response that the server sends back to the client.

  4. Why The Servlet? • Are Java programming language-based, platform-independent • Object Oriented • Browsers, Web Applications, Java technology-based applets and Applications, and other programs can all use HTTP • Any kind of data can be transmitted over HTTP -- not just HTML • Architecturally faster than CGI

  5. Why The Servlet? • Communicate with other server resources, including databases and Java-based applications such as JDBC, EJB, JavaMail, JavaIDL, RMI, and more • The incredible functionality of the Java platform libraries make Servlets the most flexible middle tier

  6. Architecture Overview HTML File 1 Java-enabled Web Server Web Browser 2 3 Servlets 5 4 HTML File

  7. Servlet Programming Model • A servlet program must • implement javax.servlet.Servlet interface • extend javax.servlet.GenericServlet or javax.servlet.http.HttpServlet • A servlet programming Model • Request : represented by a java.servlet.ServletRequest object • Service : preformed by invoking the service() method • Response : represented by a java.servlet.ServletResponse object

  8. Servlet Life Cycle • The life-cycle methods include: • init() for initializing the servlet. This method is called when the servlet is first loaded. It is called only once. • Service() for performing the service. This method is called after initialization. • Destory() for destroying the servlet. This method is called before unloading and is called only once. It can be used to perform cleanup operations.

  9. Servlet Life Cycle • doGet() for processing the HTTP GET method. This method is called after initialization • doPost() for processing the HTTP POST method. This method is called after initialization.

  10. HTTP Request-Response HTTP Server Client 1 HTTPServlet init 2 HTTPServletRequest HTTPServletResponse HTTP request 3 service 4 getParameter repeat 5 Do some work... setContentType 6 ServletOutputStream getOutputStream 7 printin repeat 8 close 9 return 10 HTTP response Client Server dtm225/java.ppt

  11. ServletRequest Interface • The ServletRequest interface provides the means for getting information from the client to the servlet for a service request • getParameter(String name) returns “the single value (String) “of the named parameter for this request. • getParameters(String name) returns “the values (String[]) of the named parameter for this reuquest. • getParameterNames() returns an Enumeration containing the names of the parameters for this request. • getRemoteAddr() returns the IP address (String) of the agent that sent this request.

  12. ServletResponse Interface • The ServletResponse interface provides the means for sending response data from • setContentLength(int len) sets the content legth of this response. • setContentType(String type) sets the content type of this response. • getOutputStream() returns a ServletOutputStream for writing the response data.

  13. Creating Servlet 1. Extend the HttpServlet abstract class. 2. Override the appropriate methods. The ServletSample overrides the doGet() method. 3. Get HTTP request information, if any. (parameter for html page) • The Request object has specific methods to retrieve information provided by the client: • getParameterNames() • getParameter() • getParameterValues()

  14. Creating Servlet 4. Generate the HTTP response. • The HttpServletResponse object generates a response to return to the requesting client. • Its methods allow you to set the Response header and the Response body. • The Response object also has the getWriter() method to return a PrintWriter object. • Use the print() and println() methods of the PrintWriter object to write the servlet Response back to the client.

  15. Servlet Example/HTML <HTML> <HEAD> <TITLE>Introductions</TITLE> </HEAD> <BODY> <FORM METHOD=GET ACTION=“/servlet/Hello”> If you don’t mind me asking, what is your name? <INPUT TYPE=TEXT NAME=“name”><P> <INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML> Form.html

  16. Servlet Example import*; import javax.servlet.*; import javax.servlet.http*; // Step 1: Extend HttpServlet. public class Hello extends HttpServlet{ // Step 2: Specify the required methods. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ // Step 3: Get the HTTP request information, if any. String name = req.getParameter(“name”);

  17. Servlet Example // Step 4: Create the HTTP response. res.setContentType(“text/html”); PrintWriter out = res.getWriter(); out.println(“<HTML>”); out.println(“<HEAD><TITLE>Hello, “ +name+”</TITLE></HEAD>”); out.println(“<BODY>”); out.println(“Hello, “+ name); out.println(“</BODY></HTML>”); } }

  18. Lab 15 • Lab 15 :