Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Handling HTTP Requests

• The doGet () method is invoked by server


through service () method to handle a HTTP
GET request.
• Write a servlet to diaply login id and password
HTML code
<HTML>
<BODY>
<CENTER>
<FORM NAME="Form1" METHOD=“get"
ACTION="ServletGetExample">

<B>Login ID</B> <INPUT TYPE="text" NAME="loginid" >

<B>Password</B> <INPUT TYPE="password" NAME="passw" >


<INPUT TYPE=submit VALUE="Submit".>
</P>
</BODY>
</HTML>
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class ServletGetExample extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
PrintWriter out = res.getWriter();
String login= req.getParameter("loginid");
String password= req.getParameter("passw");
out.println("Your login ID is: ");
out.println(login);
out.println("Your password is: ");
out.println(password);
out.close();
}
}
• In this example, the doGet () method of
HttpServlet class is overridden to handle the
HTTP GET request.
• The two parameters passed to the doGet ()
method are req and res, the Objects of
HttpServletRequest and HttpServletResponse
interface respectively.
• The req object allows to read data provided in
the client request and the res object is used to
develop Response for the client request.
• getParameter () method of
HttpServletRequest interface is used to
retrieve data attached to the URL sent to the
server.
• The string appearing to the right of the
question mark in URL is known as the query
string, contains the parameters for the HTTP
GET request.
• To handle post request.
• Use doPost() instead of doGet() method
Theory related to Servlet
• Methods in the HttpServlet class that handle
client requests take two arguments:
• An HttpServletRequest object, which
encapsulates the data from the client

• An HttpServletResponse object, which


encapsulates the response to the client
• HttpServletRequest Objects
• An HttpServletRequest object provides access to HTTP header data, such as any cookies
found in the request and the HTTP method with which the request was made.
The HttpServletRequestobject also allows you to obtain the arguments that the client
sent as part of the request.
• To access client data:
• The getParameter method returns the value of a named parameter. If your parameter
could have more than one value, use getParameterValues instead.
The getParameterValues method returns an array of values for the named parameter.
(The method getParameterNames provides the names of the parameters.)

• For HTTP GET requests, the getQueryString method returns a String of raw data from the
client. You must parse this data yourself to obtain the parameters and values.

• For HTTP POST, PUT, and DELETE requests,

– If you expect text data, the getReader method returns a BufferedReader for you to
use to read the raw data.
– If you expect binary data, the getInputStream method returns
a ServletInputStream for you to use to read the raw data
• Note: Use either a getParameter[Values] method or one of the methods that allow you
to parse the data yourself. They can not be used together in a single request.
• HttpServletResponse Objects
• An HttpServletResponse object provides two ways of returning data to
the user:

• The getWriter method returns a Writer

• The getOutputStream method returns a ServletOutputStream

• Use the getWriter method to return text data to the user, and
the getOutputStream method for binary data.
• Closing the Writer or ServletOutputStream after you send the
response allows the server to know when the response is complete.
• HTTP Header Data
• You must set HTTP header data before you
access the Writer or OutputStream.
The HttpServletResponse class provides
methods to access the header data. For
example, the setContentType method sets the
content type. (This header is often the only
one manually set.)

You might also like