Servlets - SPLessons

Servlet Request

Chapter 10

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Request

Servlet Request

shape Introduction

The Servlet Request is an interface which defines different methods to handle the client requests to access a servlet, ServletRequest provides an instance to give the requests of client for the servlet, where servlet container establish a object and send an argument to the service method. Following is the structure of this interface.

public interface ServletRequest

Click Here To Know More About Servlet Life Cycle Methods

shape Methods

Servlet Request, Following are the methods of the ServletRequest interface.

Methods Description
getParameter(String name) It returns the value of parameter of client request type on the text file and the return type is String.
getParameterValues(String name) It returns an array of string which contains all parameter names. It is mainly used for the drop down box and it returns String.
getParameterNames() It returns an enumeration.
getContentLength() It returns size of data content and return type is int.
getServerPort() It returns port number and the return type is int.
getServerName() It returns host name of server and the return type is string.
getCharacterEncoding() It returns the set encoding request otherwise it shows null and return type is String.
getCharacterQueryString() It returns the request URL path and the return type is String.
getInputStream() It gets the information stream of the subprocess. The stream gets information from the standard output stream and the return type is ServletInputStream.

shape Conceptual
Figure

Servlet Request, Following is the figure to understand that how servlet can manage multiple requests.

As discussed that servlet will provide Threads to process the multiple request and this is the advantage of servlet and it overcomes the problem of CGI(Common Gateway Interface).

Click Here To Know More About CGI

Example on ServletRequest to display user name

shape Examples

welcome.html

<html>
<body>
 
<form action="./welcome">
user::<input type="text" name="user"/>
fullname::<input type="text" name="fullname"/>
 <input type="submit" value="go"/>
</form>
</body>
</html>

Here the developer just created two text boxes, one for User and another for full name and also created a submit button that is go.
web.xml

<web-app>
 
<servlet>
   <servlet-name>Demo</servlet-name>
    <servlet-class>ServletRequest</servlet-class>
 </servlet>
 
 <servlet-mapping>
    <servlet-name>Demo</servlet-name>
   <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
   
</web-app>

Here make sure that servlet name should be same and URL pattern should be match with HTML form action URL.
ServletRequest.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletRequest extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.print("");  
		
		String username = request.getParameter("user");
		ServletConfig config = getServletConfig();
		String fullname = request.getParameter("fullname");
		
		out.print(" <h1><b>Hi "+username+" welcome to Splessons</b></h1>"); 
		out.print("<h2><b>This is your fullname "+fullname+" </b></h2>"); 

		out.print("");  
		out.flush();
		out.close();
 }
}

Where ServletRequest is the class name, this class is extending HttpServlet , In doGet Method the parameters are appended to the URL and sent along with header information, DoGet is faster if we set the response content length since the same connection is used.

Output:

Compile the code from HTML page then the following output will be displayed.

After entering the data to the above form result will be as follows.

Difference Between System.out.println() and PrintWriter

Both are used to display the output. But system.out.println is used in java and out.prinln (PrintWriter) is used in scripting languages. An user will create the object for the class PrintWriter as “out”. out.println() is used in JSP. If an user create object for that PrintWriter class as “splesson”, user have to print line as splesson.println().

Summary

shape Key Points

  • Servlet Request – Program should be compiled from HTML page.
  • In the servlet class, package name with class name needs to be written like packgename.classname .