Back to: JSP Tutorials for Beginners and Professionals
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.
- What is JSP Client Request?
- 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)
- The HttpServletRequest Class and its Method
- 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
- Accept: It specifies the MIME types that the browser or other clients can handle.
- Accept-Charset: It specifies the character sets that the browser can use to display the information.
- Accept-Encoding: It specifies the types of encodings that the browser knows how to handle.
- Accept-Language: It specifies the client’s preferred languages in case the servlet can produce results in more than one language.
- Authorization: It is used by the clients to identify themselves when accessing password-protected webpages.
- Connection: It indicates whether the client can handle persistent HTTP connections.
- Content-Length: It is applicable only to POST requests and gives the size of the POST data in bytes.
- Cookie: It returns cookies to serves that previously sent to the browser.
- Host: It specifies the host and port as given in the original URL.
- If-Modified-Since: It indicates that the client wants the page only if it has been changed after the specified date.
- 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.
- Referer: It indicates the URL of the referring webpages.
- 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:
- getCookies(): It returns an array containing all of the cookie objects the client sent with this request.
- getAttributeNames(): It returns an enumeration containing the names of the attributes available to this request.
- getHeaderNames(): It returns an enumeration of all the header names it contains.
- getParameterNames(): It returns a String object containing the names of the parameters contained in this request.
- getSession(): It returns the current session associated with this request.
- getLocale(): It returns the preferred Locale that the client will accept content in.
- getInputStream(): It helps us to retrieve the body of the request as binary data using a ServletInputstream.
- getAuthType(): It returns the name of the authentication scheme used to protect the servlet.
- getContentType(): It returns the MIME type of the body of the request.
- getContextPath(): It returns the portion of the request URI.
- getMethod(): It returns the name of the HTTP method with which this request was made.
- getPathInfo(): It returns extra path information associated with the URL.
- getProtocol(): It returns the name and version of the protocol the request uses.
- getQueryString(): It returns the query string that is contained in the requested URL.
- getRequestedSessionId(): It returns the session ID specified by the client.
- getServletPath(): It returns the part of the request’s URL that calls the JSP.
- isSecure(): It returns True or False indicating whether the request was made using a secure channel or not.
- getContentLength(): It returns the length of the request body and made available by the input stream.
- getServerPort(): It returns the port number on which the request was received.
- 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.
index.html
<!DOCTYPE html> <html> <head> <title>request implicit object</title> </head> <body bgcolor="pink"> <form action="welcome.jsp"> <input type="text" name="uname"> <input type="submit" value="GO"><br /> </form> </body> </html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body bgcolor="pink"> <% String name = request.getParameter("uname"); out.print("welcome " + name); %> </body> </html>
Output
Run your code to get the following output.
Type your name and click on the “GO” button
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.