6.3 HTTP Package
6.3 HTTP Package
http Package
Interfaces-
Classes-
HttpServletRequest interface-
-This interface extends the ServletRequest interface to provide request information for HTTP servlets.
Methods of HttpServletRequest Interface-
Methods Description
String getContextPath() returns the portion of the request URI that indicates the context of the request
Cookies getCookies() returns an array containing all of the Cookie objects the client sent with this
request
HttpSession getSession() returns the current HttpSession associated with this request or, if there is no
current session and create is true, returns a new session
String getMethod() Returns the name of the HTTP method with which this request was made, for
example, GET, POST, or PUT.
Part getPart(String name) gets the Part with the given name
String getPathInfo() returns any extra path information associated with the URL the client sent when it
made this request.
String getServletPath() returns the part of this request's URL that calls the servlet
HttpServletResponseInterface-
-Extend the ServletResponse interface to provide HTTP –specific functionality in sending a reponse.
Methods of HttpServletResponseInterface-
Methods Description
void addCookie(Cookie cookie) adds the specified cookie to the response.
void sendRedirect(String Sends a temporary redirect response to the client using the specified
location) redirect location URL and clears the buffer
String getHeader(String name) gets the value of the response header with the given name.
void setHeader(String name, sets a response header with the given name and value
String value)
void setStatus(int sc) sets the status code for this response
void sendError(int sc, String msg) sends an error response to the client using the specified status and
clears the buffer
HttpServlet class-
- The HttpServlet class extends the GenericServlet class and implements Serializable interface.
- It provides http specific methods such as doGet, doPost, doHead, doTrace etc.
Method Description
public void service(ServletRequest req,ServletResponse dispatches the request to the protected service method by
res) converting the request and response object into http type.
protected void service(HttpServletRequest req, receives the request from the service method, and dispatches
HttpServletResponse res) the request to the doXXX() method depending on the incoming
http request type.
protected void doGet(HttpServletRequest req, handles the GET request. It is invoked by the web container.
HttpServletResponse res)
protected void doPost(HttpServletRequest req, handles the POST request. It is invoked by the web container.
HttpServletResponse res)
protected void doHead(HttpServletRequest req, handles the HEAD request. It is invoked by the web container.
HttpServletResponse res)
protected void doOptions(HttpServletRequest req, handles the OPTIONS request. It is invoked by the web
HttpServletResponse res) container.
protected void doPut(HttpServletRequest req, handles the PUT request. It is invoked by the web container.
HttpServletResponse res)
protected void doTrace(HttpServletRequest req, handles the TRACE request. It is invoked by the web container.
HttpServletResponse res)
protected void doDelete(HttpServletRequest handles the DELETE request. It is invoked by the web container.
req, HttpServletResponse res)
protected long returns the time when HttpServletRequest was last modified since
getLastModified(HttpServletRequest req) midnight January 1, 1970 GMT.
Unlike Generic Servlet, the HTTP Servlet doesn’t override the service() method. Instead it overrides the doGet()
method or doPost() method or both. The doGet() method is used for getting the information from server while the doPost()
method is used for sending information to the server.
In Http Servlet there is no need to override the service() method because this method dispatches the Http
Requests to the correct method handler, for example if it receives HTTP GET Request it dispatches the request to the
doGet() method.
Web.xml file
<servlet>
<servlet-name>check</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>check</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
MyServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
String user = request.getParameter("user");
out.println("<h2> Welcome "+user+"</h2>"); }
finally {
out.close();
}
}
}