JSP Client Request, Server Response & HTTP Status Codes

JSP Client Request, Server Response & HTTP Status Codes

  • JSP actions which use constructs in XML syntax to control the behavior of the servlet engine. We will learn more in detail about various JSP Action elements like client request, server response, HTTP status codes.
  • In this tutorial, you will learn-
  • JSP Client Request
  • JSP Server Response
  • JSP HTTP Status Codes
  • JSP Client Request
  • When the web page is requested, it sends information to the web server in the HTTP header.
  • We can use this information using HTTPServletRequest object.
  • Information sent by the browser is stored in the request header of HTTP request.
  • We are using different headers to send information to the request object.
  • Different headers are described below:
HeaderDescriptionExample
AcceptIt specifies MIME types that browser or other clients can handleImage/png or image/jpeg
Accept-charsetIt uses the character set used by the browser to display the informationISO-8859-1
Accept- EncodingIt specifies type of encoding handled by the browserGzip or compress
Accept-languageIt specifies clients specified languageEn,en_us
AuthorizationHeader used by clients when trying to access password protected web pages 
ConnectionIt indicates whether client can handle persistent HTTP connections(browser can retrieve multiple files)Keep-alive
Content-lengthApplicable to post requests. It gives size of post data of bytes 
CookieReturns cookie to server(those which were previously sent to the browser) 
HostSpecifies the host and port of the original URL 
If modified sinceIt indicates that it requires only a page if it has been changed or modified 
If unmodified sinceIt indicates that it requires a page only if it has not been changed or modified 
ReferrerIndicates URL of referring URL page 
User-agentIdentifies browser or client making request 
  • Following methods are used to read the HTTP header in JSP page:
  • Cookie[] getCookies() – returns an array containing cookie objects that the client has sent
  • Enumeration getAttributeNames() – contains enumeration of names of attributes for request
  • Enumeration getHeaderNames() – contains enumeration of names of header .
  • Enumeration getParameterNames() – contains enumeration of getting parameter names in the request.
  • HttpSessiongetSession() – returns the current session associated with the request or if does not have a session then it will create a new one.
  • Locale getLocale() – returns the preferred locale that client will accept content in.It has been assigned to the response. By default, the value will be default locale of the server.
  • Object getAttribute(String name) – returns the value of named attribute as an object.
  • ServletInputStreamgetInputStream() – retrievesbody of request as binary data.
  • String getAuthType() – returns the name of authentication scheme to protect servlet
  • String getCharacterEncoding() – returns name of the character encoding used in the body of the request.
  • String getContentType() – returns the MIME type of body of the request.
  • String getContextPath() – returns the part of request URI indicates context path of URI
  • String getHeader(String name) – returns the request header as a string
  • String getMethod() – returns the name of the HTTP method like GET, POST
  • String getParameter(String name) – returns the parameter of the request as a string.
  • String getPathInfo() – returns the path information associated with the URL
  • String getQueryString() – returns the query string that is associated with the request URL
  • String getServletPath() – returns the part of URLs of the request that calls the JSP
  • String[] getParameterValues(String name) – returns the array of string objects containing the values that request parameter has
  • Example:
  •  
  • In the example below, we are using different methods using request object
  •  
  • <%@ page language=“java” contentType=“text/html; charset=ISO-8859-1” pageEncoding=“ISO-8859-1”%>
  •  
  • <%@ page import=“java.io.* java.util.*” %>
  •  
  • <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
  •  
  • <html>
  •  
  • <head>
  •  
  • <meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>
  •  
  • <title>Client Request Guru JSP</title>
  •  
  • </head>
  •  
  • <body>
  •  
  • <h2>Client Request Guru JSP</h2>
  •  
  • <table border=“1”>
  •  
  • <tr>
  •  
  • <th>guru header</th><th>guru header Value(s)</th>
  •  
  • </tr>
  •  
  • <%
  •  
  • HttpSession gurusession = request.getSession();
  • out.print(“<tr><td>Session Name is </td><td>” +gurusession+ “</td.></tr>”);
  •  
  • Locale gurulocale = request.getLocale ();
  •  
  • out.print(“<tr><td>Locale Name is</td><td>” +gurulocale + “</td></tr>”);
  •  
  • String path = request.getPathInfo();
  •  
  • out.print(“<tr><td>Path Name is</td><td>” +path+ “</td></tr>”);
  •  
  • String lpath = request.get();
  •  
  • out.print(“<tr><td>Context path is</td><td>” +lipath + “</td></tr>”);
  •  
  • String servername = request.getServerName();
  •  
  • out.print(“<tr><td>Server Name is </td><td>” +servername+ “</td></tr>”);
  •  
  • int portname = request.getServerPort();
  •  
  • out.print(“<tr><td>Server Port is </td><td>” +portname+ “</td></tr>”);
  •  
  • Enumeration hnames = request.getHeaderNames();
  •  
  • while(hnames.hasMoreElements()) {
  •  
  • String paramName = (String)hnames.nextElement();
  •  
  • out.print (“<tr><td>” + paramName + “</td>” );
  •  
  •  
  • String paramValue = request.getHeader(paramName);
  • out.println(“<td> “ + paramValue + “</td></tr>”);
  •  
  • }
  •  
  •  
  •  
  • %>
  • Explanation of the code:
  • Code Line 17: Using request object, we are getting the session object of that particular session, and we get the object value of that session
  • Code Line 19: Using request object, we are getting locale of that particular session i.een_US locale for that JSP.
  • Code Line 21: Using request object, we are getting path info for that JSP. In this case, it is null as there is no path for URL mentioned.
  • Code Line 23: Using request object, we are getting context path, i.e., root path
  • Code Line 25: Using request object, we are getting the server name.
  • Code Line 27: Using request object, we are getting server port.
  • Code Line 29-35: Using request object, we are getting header names which come out as enumeration, and hence we get all header values in the header names.
  • In this, we get all header values as a cookie, host, connection, accept language, accept encoding.
  • 200 – Indicates everything is fine
  • 301 – It has moved permanently
  • 304 – Not modified since last change
  • 400 – Bad request
  • 404 – Not found
  • 405 – Method not found
  • 500 – Internal Server Error
  • 503 – Service unavailable
  • 505 – HTTP version not supported
  • Some of its methods are listed below:
  • Public void setStatus(intstatusCode)
  • It sets the status code whichever we want to set in that JSP Page.This will give us the message of status code which has been set
  • Public void sendRedirect(String URL)
  • It generates 302 response along with the location header giving URL of the new document
  • Public void sendError(intcode,Stringmsg)
  • It sends the status code along with the short message and it is formatted inside HTML document.
  • Example:
  • In this example, we are sending error to JSP page explicitly.
  • <%@ page language=“java” contentType=“text/html; charset=ISO-8859-1” pageEncoding=“ISO-8859-1”%>
  •  
  • <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
  •  
  • <html>
  •  
  • <head>
  •  
  • <meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>
  •  
  • <title>Guru Status Code</title>
  •  
  • </head>
  •  
  • <body>
  •  
  • <% response.sendError(404,“Guru Page Not Found”); %>
  •  
  • </body>
  •  
  • </html>
  • Explanation of the code:
  • Code Line 10: Using response object we are sending the error to a page with two parameters.
  • Status code – It can be any of the above. In this case, we have described as 404
  • Message – It can be any specific message that we want to show the error
  • If you execute the above code, you get the following output:
  • JSP Action - File Upload, JSP Filter, Client Request, Server Response, Cookies Handling, Date Handling
  • Output:
  • Here we get error code as 404, which was sent from the code and also displays”Guru Page not found” message seen in the output.
  • Summary:
  • In this article, we have learnt about client request and server response on how the request is intercepted and how the responses are manipulated.
  • Also, we have learnt about HTTP status codes to show error codes or success codes for a particular file.
top
X
Welcome to Our Website
Welcome to WPBot
wpChatIcon