JSP Client Request

JSP Client Request

In this article, I am going to discuss JSP Client Request with Examples. Please read our previous article where we discussed Different Types of Tags and Their Life Cycle in JSP. At the end of this article, you will understand the following pointers.

  1. What is JSP Client Request?
  2. Understanding Request Headers (Accept, Accept-Charset, Accept-Encoding, Accept-Language, Authorization, Connection, Content-Length, Cookie, Host, If-Modified-Since, If-Unmodified-Since, Referer, User-Agent)
  3. The HttpServletRequest Class and its Method
  4. HTTP Header Request Example
What is JSP Client Request?

When a browser requests a Webpage, it sends tons of data to the online server. This information cannot be read directly because this information travels as a part of the header of the HTTP request. When the online page is requested, it sends information to the online server within the HTTP header. We can use this information using the HTTPServletRequest object. The information sent by the browser is stored in the request header of the HTTP request. And we are using different headers to send information to the request object.

Request Headers in JSP
  1. Accept: It specifies the MIME types that the browser or other clients can handle.
  2. Accept-Charset: It specifies the character sets that the browser can use to display the information.
  3. Accept-Encoding: It specifies the types of encodings that the browser knows how to handle.
  4. Accept-Language: It specifies the client’s preferred languages in case the servlet can produce results in more than one language.
  5. Authorization: It is used by the clients to identify themselves when accessing password-protected webpages.
  6. Connection: It indicates whether the client can handle persistent HTTP connections.
  7. Content-Length: It is applicable only to POST requests and gives the size of the POST data in bytes.
  8. Cookie: It returns cookies to serves that previously sent to the browser.
  9. Host: It specifies the host and port as given in the original URL.
  10. If-Modified-Since: It indicates that the client wants the page only if it has been changed after the specified date.
  11. If-Unmodified-Since: It is the reverse of If-Modified_Since which specifies that the operation should succeed only if the document is older than the specified date.
  12. Referer: It indicates the URL of the referring webpages.
  13. User-Agent: It identifies the browser or other client making the request and can be used to return different content to different types of browsers.
HttpServletRequest Object

It is an instance of a javax.servlet.http.HttpServletRequest object. Each time a client requests a page, the JSP engine creates a replacement object to represent that request. It extends the ServletRequest interface to provide request information for HTTP servlets. Then the servlet container creates an HttpServletRequest object and passes it as an argument to the servlet’s service method.

Methods of HttpServletRequest object:
  1. getCookies(): It returns an array containing all of the cookie objects the client sent with this request.
  2. getAttributeNames(): It returns an enumeration containing the names of the attributes available to this request.
  3. getHeaderNames(): It returns an enumeration of all the header names it contains.
  4. getParameterNames(): It returns a String object containing the names of the parameters contained in this request.
  5. getSession(): It returns the current session associated with this request.
  6. getLocale(): It returns the preferred Locale that the client will accept content in.
  7. getInputStream(): It helps us to retrieve the body of the request as binary data using a ServletInputstream.
  8. getAuthType(): It returns the name of the authentication scheme used to protect the servlet.
  9. getContentType(): It returns the MIME type of the body of the request.
  10. getContextPath(): It returns the portion of the request URI.
  11. getMethod(): It returns the name of the HTTP method with which this request was made.
  12. getPathInfo(): It returns extra path information associated with the URL.
  13. getProtocol(): It returns the name and version of the protocol the request uses.
  14. getQueryString(): It returns the query string that is contained in the requested URL.
  15. getRequestedSessionId(): It returns the session ID specified by the client.
  16. getServletPath(): It returns the part of the request’s URL that calls the JSP.
  17. isSecure(): It returns True or False indicating whether the request was made using a secure channel or not.
  18. getContentLength(): It returns the length of the request body and made available by the input stream.
  19. getServerPort(): It returns the port number on which the request was received.
  20. getRemoteAddr(): It returns the IP address of the client that sent the request.
HTTP Header Request Example

In this example, we are printing the name of the user with a welcome message. Here we are receiving the input from the user on the index.html page and displaying it in the welcome.jsp page using request implicit object.

<!DOCTYPE html>
<title>request implicit object</title>
<body bgcolor="pink">
 <form action="welcome.jsp">
  <input type="text" name="uname"> <input type="submit"
   value="GO"><br />
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<body bgcolor="pink">
  String name = request.getParameter("uname");
 out.print("welcome " + name);

Run your code to get the following output.

HTTP Header Request Example in JSP

Type your name and click on the “GO” button

JSP Client Request with Examples

In the next article, I am going to discuss JSP Server Response Headers with Examples. Here, in this article, I try to explain JSP Client Request with Examples. I hope you enjoy this JSP Client Request article.

Leave a Reply

Your email address will not be published. Required fields are marked *