Table of contents
1.
Introduction
2.
What is a client Request?
3.
Request Headers in JSP
4.
JSP request implicit object 
4.1.
Methods of Request Implicit object
5.
HTTP Header Request Example
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

JSP Client Request

Author Aryan Raj
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JSP(JavaServer Pages) technology allows you to create web content with both static and dynamic components easily. JavaServer Pages is an advanced version of Servlet Technology.

JSP is a server-side programming language that combines HTML, XML, Java Servlet, and JavaBeans technologies into one highly productive technology. First, It is converted into a servlet by a JSP container before processing the client's request. We can also insert Java code in HTML/XML pages.

While building dynamic web applications, client and server interactions are essential for sending and receiving information over the Internet. 

When the client requests a webpage, a lot of information is sent back by the webserver. Such information can't be read directly because they are part of an HTTP header request.

In this blog, you will learn about the various request headers provided by JSP.

 

What is a client Request?

When a client requests a web page, it sends information to the online server within the HTTP header. 

The JavaServer Pages request can be defined as an implicit object that is an instance of "HttpServletRequest" and is formed for all JSP requests through the web container. The information sent by the browser is stored in the request header of an HTTP request.

This JSP request gets request information like a Server port, Parameter, Remote address, Server name, Header information, Character encoding, Content type, etc.

Request Headers in JSP

Different Headers used in JSP are given below:

Accept: It specifies Multipurpose Internet Mail Extensions (MIME) types that the browsers or other clients can handle.
Accept-Charset: It specifies the character set used by the browser to display the information.
Accept-Encoding: It specifies the encoding handled by the browser.
Accept-language: It specifies clients' specified language.
Authorization: It is used by clients when they are trying to access password-protected web pages.
Connection: It indicates whether the client can handle persistent HTTP connections. The browser can retrieve multiple files.
Content-length: It is Applicable to post requests. It gives the size of post data in bytes.
Host: It specifies the host and port of the original URL.
If-modified-since: It indicates that it requires only a page if it has been modified.
If-unmodified-since: It indicates that it requires a page only if it has not been modified.
Referrer: It indicates the URL of referring URL Webpage.
User-Agent: It identifies the browser or client making the request.

 

JSP request implicit object 

Implicit request object used in Java is an instance of an javax.servlet.http.HttpServletRequest interface . Every 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 Request Implicit object

Methods of HttpServletRequest Object

Enumeration getAttributeNames()

It is used to return an Enumeration containing the attribute names presented to this request.

 

Cookie[] getCookies()

It returns an array containing all of the client's cookie objects sent associated with a particular request.

 

Enumeration getParameterNames()

It returns a String object containing the parameters' names in this request.

 

Enumeration getHeaderNames()

It 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)

It is used to return the current HttpSession linked with the request or create a new session if there is no current session.

 

Locale getLocale()

It is used for returning the chosen Locale that will be accepted by the client, based upon the Accept-Language header.

 

Object getAttribute(String name)

It is used for returning the value of the named attribute as an Object or set as null.

 

ServletInputStream getInputStream()

It is used to retrieve the request's body in the form of binary data through a ServletInputStream.

 

String getAuthType() It is used to return the authentication scheme's name implemented for protecting the servlet.
String getCharacterEncoding() It is used for returning the name of the character encoding implemented in the body of a request.
String getContentType()

It is used for returning the MIME type of the requested content body.

 

String getContextPath()

It is used for returning the portion of the request URI, which indicates the context of the request.

 

String getHeader(String name)

It is used for returning the value of the specified request header in the form of a String.

 

String getMethod()

It is used for returning the name of the HTTP method (GET, PUT, and POST) through which this request was made.

 

String getPathInfo()

It is used for returning any extra path information connected to the URL sent by the client when the request is made.

 

String getProtocol()

It is used for returning the name and the version of the protocol.

 

String getQueryString()

It is used to return the query string in the request URL following the path.

 

String getRemoteAddr()

It is used for returning the client's Internet Protocol (IP) address, which is used by all websites.

 

String getRemoteHost()

It is used for returning the fully qualified name of the client who sent the request.

 

String getRemoteUser()

It returns the user's login, making an authenticated request or null if the user has not yet authenticated.

 

String getRequestURI()

It 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()

It is used for returning the particular session ID of the client.

 

String getServletPath()

It is used for returning the part of this request's URL, which calls the JSP.

 

String getParameterValues(String name)

It is used to return an array of String objects containing all of the values of a requested parameter or otherwise returns null.

 

boolean isSecure()

It is used for returning a Boolean value indicating if a request was made through a secure channel (HTTPS, FTPS) or not.

 

int getContentLength()

It is used for returning the length of a request body.

 

int getIntHeader(String name)

 

It is used for returning the value of a particular request header as an int.
int getServerPort()

It is used for returning the port number on which a request was received.

 

 

HTTP Header Request Example

(In this example, we are adding two numbers)

We have created one HTML page(index.html) and a JSP page(add.jsp). In the shown example, we are receiving the input from the user in the index.html page and displaying the sum of both numbers in add.jsp page using the request implicit object. 

index.html

 <!DOCTYPE html>
 <html>
 <body>
  <form action="add.jsp" method="get">
    Enter 1st number: <input type="text" name="num1"><br>
    Enter 2nd number: <input type="text" name="num2"><br>
  <input type="submit">
  </form>
 </body>
 </html>

 

add.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

 <!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=UTF-8">
 <title>Insert title here</title>
 </head>
 <body bgcolor="cyan">
  <%
      int i = Integer.parseInt(request.getParameter("num1"));

      int j = Integer.parseInt(request.getParameter("num2"));

      int k = i + j;

      out.println("Output: + "+k);
  %>
 </body>
 </html>

 

Output:

We got our result as 17 and the Background color as cyan. 

You can see the http header request, we got add.jsp in the address bar and numerical values of both inputs.

You can also read about the JSP Architecture and Lifecycle.

Frequently Asked Questions

Q1. What is HttpServlet?

Ans: HttpServlet is an abstract class given under the servlet-api present. It lies under javax.servlet.http package and has no abstract methods. It extends GenericServlet class. When the servlet container uses HTTP protocol to send requests, it creates HttpServletRequest and HttpServletResponse objects.

 

Q2. Why is servlet called server-side programming?

Ans: It is an interface that must be implemented for creating any Servlet.

Servlet is a class that extends the servers' capabilities and responds to incoming requests. It can respond to any requests.

Before Servlet, Common Gateway Interface(CGI )scripting language was common as a server-side programming language.

 

Q3. What is the difference between HttpServlet and GenericServlet ?

Ans: The main difference between HttpServlet and GenericServlet is that the HttpServlet is protocol-dependent and can only be used on HTTP protocol. Whereas, GenericServlet is protocol-independent and can be used with any protocol such as FTP, HTTP, SMTP, CGI, etc. 

 

Q4. What is a client request?

Ans: Clients are typically seen as the requesting program or user in a client-server architecture. Client end-user devices typically include laptops, desktop computers, and smartphones. In a computer network, a client in a client-server model is what requests a service or resource from a server.

Key Takeaways

In this blog, we discussed Client requests in JSP, Request Headers, and Methods. We have also discussed Http Header Requests with the help of a Web Example.

Recommended Readings:

If you are pursuing a new career in Web Development, we suggest you get your fundamentals crystal clear with our Full Stack Development course

Happy Learning, Ninja!

This course will help you!

Live masterclass