290 likes | 404 Views
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)
E N D
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) • 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
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
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/");
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); ………
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/>"); } } ….
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/>"); }
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
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
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();
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
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); … }
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"); }
Examples (cont) • Addition the destroy method public void destroy(){ System.out.println ("Destroy"); } • Execute Undeploy the current project on Tomcat Server
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
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
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, …)