<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>The <i><b>HttpServlet </b></i>class request processing methods take two parameters.</p>
<ul>
<li><b>javax.servlet.http.HttpRequest</b></li>
<li><b>javax.servlet.http.HttpResponse</b></li>
</ul>
<p>For instance, here is the signature of the <i><b>HttpServlet.doGet() </b></i>method:</p>
<pre class="highlight">protected void doGet( 
 HttpServletRequest request, 
 HttpServletResponse response) 
 throws ServletException, IOException { 
 
} 
</pre>
<p>The purpose of the <i><b>HttpRequest </b></i>object is to represent the HTTP request a browser sends to your web application. Thus, anything the browser may send, is accessible via the <b><i>HttpRequest</i></b>.</p>
<p>The <i><b>HttpRequest </b></i>object has a lot of methods, so I will just cover the most commonly used here.</p>
<ul>
<li><b>Parameters</b></li>
<li><b>Headers</b></li>
<li><b>InputStream</b></li>
<li><b>Session</b></li>
<li><b>ServletContext</b></li>
</ul>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<h2><b>Parameters</b></h2>
<p>The request parameters are parameters that are sent from the browser along with the request. Request parameters are typically sent as part of the URL (in the &#8220;query string&#8221;), or as part of the body of an HTTP request. For instance:<br />
<i><b>https://dineshonjava.com/somePage.html?param1=hello&;m2=world</b></i></p>
<p>Notice the &#8220;<i><b>query string</b></i>&#8221; part of the URL: <i><b>?param1=hello&;m2=world</b></i> This part contains two parameters with parameter values:</p>
<p><i><b>param1=hello</b></i><br />
<i><b>param2=world</b></i></p>
<p>You can access these parameters from the <i><b>HttpRequest </b></i>object like this:</p>
<pre class="highlight">protected void doGet( 
 HttpServletRequest request, 
 HttpServletResponse response) 
 throws ServletException, IOException { 
 
 String param1 = request.getParameter("param1"); 
 String param2 = request.getParameter("param2"); 
 
} 
</pre>
<p>You would use the same code, if the request parameters were sent in the body part of the HTTP request. If no parameter exists with the given name, null is returned.</p>
<p>In general, if the browser sends an HTTP GET request, the parameters are included in the query string in the URL. If the browser sends an HTTP POST request, the parameters are included in the body part of the HTTP request.</p>
<h2><b>Headers</b></h2>
<p>The request headers are name, value pairs sent by the browser along with the HTTP request. The request headers contain information about e.g. what browser software is being used, what file types the browser is capable of receiving etc. In short, at lot of meta data around the HTTP request.</p>
<p>You can access the request headers from the <i><b>HttpRequest </b></i>object like this:</p>
<pre class="highlight">String contentLength = request.getHeader("Content-Length"); 
</pre>
<p>This example reads the Content-Length header sent by the browser.</p>
<p>The Content-Length header contains the number of bytes sent in the HTTP request body, in case the browser sends an HTTP POST request. If the browser sends an HTTP GET request, the Content-Length header is not used, and the above code will return null.</p>
<p>In general, If no header exists with the name passed to getHeader(), null is returned.</p>
<h2><b>InputStream</b></h2>
<p>If the browser sends an HTTP POST request, request parameters and other potential data is sent to the server in the HTTP request body. It doesn&#8217;t have to be request parameters that is sent in the HTTP request body. It could be pretty much any data, like a file or a SOAP request (web service request).</p>
<p>To give you access to the request body of an HTTP POST request, you can obtain an InputStream pointing to the HTTP request body. Here is how it is done:</p>
<pre class="highlight">InputStream requestBodyInput = request.getInputStream(); 
</pre>
<p>NOTE: You will have to call this method before calling any getParameter() method, because calling the getParameter() method on an HTTP POST request will cause the servlet engine to parse the HTTP request body for parameters. Once parsed, you cannot access the body as a raw stream of bytes anymore.</p>
<p>What you do with the data read from the InputStream is up to you. The servlet engine does not help you parse or interprete that data. You just get it raw.</p>
<h2><b>Session</b></h2>
<p>It is possible to obtain the session object from the HttpRequest object too.</p>
<p>The session object can hold information about a given user, between requests. So, if you set an object into the session object during one request, it will be available for you to read during any subsequent requests within the same session time scope.</p>
<p>Here is how you access the session object from the HttpRequest object:</p>
<pre class="highlight">HttpSession session = request.getSession(); 
</pre>
<h2><b>ServletContext</b></h2>
<p>You can access the ServletContext object from the HttpRequest object too. The ServletContext contains meta information about the web application. For instance, you can access context parameters set in the web.xml file, you can forward the request to other servlets, and you can store application wide parameters in the ServletContext too.</p>
<p>Here is how you access the ServletContext object from the HttpRequest object:</p>
<pre class="highlight">ServletContext context = request.getSession().getServletContext(); 
</pre>
<p>As you can see, you have to first get the session object, to get access to the ServletContext object.</p>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/class-httpservlet/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/httpresponse-servlet/">Next</a> >;>;</b></div>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><b>Servlet Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/java-servlets-overview/">Java Servlets Overview</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-life-cycle/">Servlet Life Cycle</a></b></li>
<li><b><a href="https://dineshonjava.com/hello-world-example-in-servlet-interface/">Servlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-servletconfig-and-cervletcontext-in-java-servlet/ "> Difference between ServletConfig and ServletContext<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/"> Difference between GenericServlet and HttpServlet<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-web-application-servlet/">What is web application?</a></b></li>
<li><b><a href="https://dineshonjava.com/advantages-of-servlets-over-cgi/">Advantages of Servlets over CGI</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/">GenericServlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/requestdispatcher-interface/">RequestDispatcher Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servletconfig-interface/">ServletConfig</a></b></li>
<li><b><a href="https://dineshonjava.com/servletcontext-interface/">ServletContext </a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-filters/">Servlet Filter Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servlets-database-access/">Database Access Example using Sevlet</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-upload-file-example/">File Uploading Example using Servlet</a></b></li>
</ol>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/class-httpservlet/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/httpresponse-servlet/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '266'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…