1 / 29

Web Programming with Servlets & JSPs SERVLET REQUESTS & RESPONSES

Web Programming with Servlets & JSPs SERVLET REQUESTS & RESPONSES. Objectives. Sending Request Sending Response Servlet Life Cycle Workshop. ServletRequest interface. Provides access to specific information about the request Defines object (ServletRequest object)

Download Presentation

Web Programming with Servlets & JSPs SERVLET REQUESTS & RESPONSES

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Programming with Servlets & JSPs SERVLET REQUESTS & RESPONSES

  2. Objectives Sending Request Sending Response Servlet Life Cycle Workshop

  3. ServletRequest interface • Provides access to specific information about the request • Defines object (ServletRequest object) • Containing actual request (ex: protocol, URL, and type) • Containing raw request (ex: headers and input stream) • Containing client specific request parameters • Is passed as an argument to the service() method

  4. ServletRequest interface (cont)

  5. HttpServletRequestinterface • Extends ServletRequest Interface • Add a few more methods for handling HTTP-specific request data • Defines an HttpServletRequest object passed as an argument to the service() method

  6. HttpServletRequest interface (cont)

  7. HttpServletRequest interface (cont)

  8. Examples

  9. Examples (cont) • RequestServlet … Enumeration parNames = request.getParameterNames (); int count = 0; while(parNames.hasMoreElements ()){ ++count; String parName = (String)parNames.nextElement (); out.print ("parName" + count + " is " + parName); String parVal = request.getParameter (parName); out.println (" and value is " + parVal + "<br/>"); } String strServer = request.getServerName (); out.println ("Server Name: " + strServer + "<br/>"); int length = request.getContentLength (); out.println ("Length in bytes " + length + "<br/");

  10. Examples (cont) String strHost = request.getHeader ("host"); out.println ("Header - host: " + strHost + "<br/>"); String strMethod = request.getMethod (); out.println ("Request Method " + strMethod + "<br/>"); String strPath = request.getPathInfo (); out.println ("Path Info: " + strPath + "<br/>"); String strAuth = request.getAuthType (); out.println ("Authentication Type: " + strAuth + "<br/>"); String qs = request.getQueryString(); out.println ("Query String " + qs); ………

  11. Examples (cont)

  12. Examples (cont)

  13. Examples (cont) …. String[] strSelect = request.getParameterValues ("rmv"); if(strSelect!=null){ for(int i=0; i<strSelect.length; i++){ out.println ("Selected item name: " + strSelect[i] + "<br/>"); } } ….

  14. Examples (cont)

  15. Examples (cont) …. count=0; Enumeration headerNames = request.getHeaderNames (); while(headerNames.hasMoreElements ()){ ++count; String headerName = (String)headerNames.nextElement (); out.print ("header " + headerName + " is "); String headerVal = request.getHeader (headerName); out.println (headerVal + "<br/>"); } …. out.println ("Headers Accept "); Enumeration headers = request.getHeaders ("Accept"); while(headers.hasMoreElements ()){ String header = (String)headers.nextElement (); out.println (header + "<br/>"); }

  16. ServletResponse interface • Is response sent by the servlet to the client • Include all the methods needed to create and manipulate a servlet’s output • Retrieve an output stream to send data to the client, decide on the content type ... • Define objects passed as an argument to service() method

  17. ServletResponse interface (cont)

  18. HttpServletResponse interface • Extends ServletResponse Interface. Defines HttpServlet objects to pass as an argument to the service() method to the client • Set HTTP response, HTTP header, set content type of the response, acquire a text stream for the response, acquire a binary stream for the response, redirect an HTTP request to another URL or add cookies to the response

  19. HttpServletResponse interface (cont)

  20. Examples

  21. Examples (cont) • Using sendRedirect response.sendRedirect (response.encodeRedirectURL ("ResServlet")); • ResServlet response.setContentType("text/html;charset=UTF-8"); ServletOutputStream out = response.getOutputStream(); out.println("<html>"); out.println("<head>"); out.println("<title>Response Demo</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>This is a Servlet Response</h1>"); out.println("Content Type: " + response.getContentType () + "<br/>"); out.println("</body>"); out.println("</html>"); out.close();

  22. Uninstantiated Instantiation Initialization meet request Meet multi request success success Detroy received request and using thread failure Unload Unavailable destroy failure Servlet life cycle init() service() destroy() • The life cycle is defined by • init() – called only one by the server in the first request • service() – process the client’s request • destroy() – called after all requests have been processed or a server-specific number of seconds have passed

  23. Examples int a =0; public void init() { System.out.println ("init"); a += 5; System.out.println ("a = " + a);} //------------------------------------------------------------------ protected void processRequest (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ … a += 10; out.println("a = " + a); … }

  24. Examples (cont)

  25. Examples (cont) • Addition the service method public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ System.out.println ("service"); response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("This is service"); }

  26. Examples (cont) • Addition the destroy method public void destroy(){ System.out.println ("Destroy"); } • Execute Undeploy the current project on Tomcat Server

  27. HTTP Request processing life cycle A servlet instance is received An instance variable is set to the request object Servlet is authenticated against the request object Response is generated after pre-processing Servlet is checked for reusability and if required held back Servlet is replaced in the servlet manager

  28. Workshop Activities Building the Servlet with get Parameter from an inputted form and show the result depending on the method of HttpServletRequest Interface. Then, building the Servlet is similar using the HttpServletResponse Interface

  29. Exercises • Do it again all of demos • Using servlet to write the programs that can do • Present the Login form (naming LoginServlet) with title Login, header h1 – Login, 02 textbox with naming txtUser and txtPass, and the Login button • Processing Login information using • Hard coding logic • Database connection • If authentication is true, present the list of students with Student table (StudentID – varchar(3), StudentName – varchar(38), DateBirth – Datetime, …)

More Related