In dynamic web application development, client and server interactions are essential for sending and receiving information over the Internet. When the browser requests a webpage, a lot of information is sent to the webserver. Such information cannot be read directly because such information is part of an HTTP header request. In this chapter, you will learn about the various request headers provided by JSP.
JSP Client Request
The JSP request can be defined as an implicit object is an instance of "HttpServletRequest" and is formed for all JSP requests through the web container. This JSP request gets request information like a parameter, remote address, header information, server port, server name, character encoding, content type, etc.
JSP request Implicit Object
- A request object is an implicit object that is used to request an implicit object, which is to receive data on a JSP page, which has been submitted by the user on the previous JSP/HTML page.
- The request implicit object used in Java is an instance of a javax.servlet.http.HttpServletRequest interface where a client requests a page every time the JSP engine has to create a new object for characterizing that request.
- The container creates it for every request.
- It is used to request information such as parameters, header information, server names, cookies, and HTTP methods.
- It uses the getParameter() method to access the request parameter.
Here is an example of a JSP request implicit object where a user submits login information, and another JSP page receives it for processing:
<!DOCTYPE html> <html> <head> <title>User login form</title> </head> <body> <form action="login.jsp"> Please insert Username: <input type="text" name="u_name" /> <br /> Please insert Password: <input type="text" name="passwd" /> <br /> <input type="submit" value="Submit Details" /> </form> </body> </html>
<%@ page import = " java.util.* " %> <% String username = request.getParameter("u_name"); String password = request.getParameter("passwd"); out.print("Name: "+username+" Password: " +passwd); %>
Methods of request Implicit Object
- Enumeration getAttributeNames(): is used for returning an Enumeration that contains the attribute names presented to this request.
- Cookie getCookies(): is used for returning an array that contains all of the cookie-objects of the client sent associated with a particular request.
- Enumeration getParameterNames(): is used for returning an enumeration of String-objects that contain the names of the parameters rested in this request.
- Enumeration getHeaderNames(): is used for returning an enumeration of all the header names associated with the request.
- HttpSession getSession(): It is used to return the current session connected with your request or create a session if it does not have any session.
- HttpSession getSession(boolean create): is used to return the current HttpSession linked with the request or create a new session if there is no current session.
- Locale getLocale(): is used for returning the chosen Locale that will be accepted by the client, based upon the Accept-Language header.
- Object getAttribute(String name): is used for returning the value of the named attribute as an Object or set as null.
- ServletInputStream getInputStream(): is used to retrieve the body of the request in the form of binary data through a ServletInputStream.
- String getAuthType(): is used for returning the name of the authentication scheme (BASIC, SSL, or null) implemented for protecting the servlet.
- String getCharacterEncoding(): is used for returning the name of the character encoding implemented in the body of a request.
- String getContentType(): is used for returning the MIME type of the requested content body.
- String getContextPath(): is used for returning the portion of the request URI, which is used for indicating the context of the request.
- String getHeader(String name): is used for returning the value of the specified request header in the form of a String.
- String getMethod(): is used for returning the name of the HTTP method (GET, PUT, and POST) through which this request was made.
- String getPathInfo(): is used for returning any extra path information connected to the URL sent by the client when the request is made.
- String getProtocol(): is used for returning the name and the version of the protocol.
- String getQueryString(): is used for returning the query string contained in the request URL following the path.
- String getRemoteAddr(): is used for returning the Internet Protocol (IP) address of the client, which is used by all websites.
- String getRemoteHost(): is used for returning the fully qualified name of the client who sent the request.
- String getRemoteUser(): is used to return the user's login, making an authenticated request or null if the user has not yet authenticated.
- String getRequestURI(): is used for returning the part of a request's URL from the protocol name till the starting line of the HTTP request.
- String getRequestedSessionId():is used for returning the particular session ID of the client.
- String getServletPath(): is used for returning the part of this request's URL, which calls the JSP.
- String getParameterValues(String name): is used for returning an array of String objects that will contain all of the values of a requested parameter or otherwise returns null.
- boolean isSecure(): is used for returning a Boolean value indicating if a request was made through a secure channel (HTTPS, FTPS) or not.
- int getContentLength(): is used for returning the length of a request body.
- int getIntHeader(String name): is used for returning the value of a particular request header as an int.
- int getServerPort(): is used for returning the port number on which a request was received.