Servlet Requests
When a servlet is asked to handle a request, it typically needs specific information about the request so that it can process the request appropriately. Most frequently, a servlet will retrieve the value of a form variable and use that value in its output. A servlet may also need access to information about the environment in which it is running. For example, a servlet may need to find out about the actual user who is accessing the servlet, for authentication purposes.
The ServletRequest and HttpServletRequest interfaces provide access to this kind of information. When a servlet is asked to handle a request, the server passes it a request object that implements one of these interfaces. With this object, the servlet can determine the actual request (e.g., protocol, URL, type), access parts of the raw request (e.g., headers, input stream), and get any client-specific request parameters (e.g., form variables, extra path information). For instance, the getProtocol() method returns the protocol used by the request, while getRemoteHost() returns the name of the client host. The interfaces also provide methods that let a servlet get information about the server (e.g., getServername(), getServerPort()). As we saw earlier, the getParameter() method provides access to request parameters such as form variables. There is also the
getParameterValues() method, which returns an array of strings that contains all the values for a particular parameter. This array generally contains only one string, but some HTML form elements (as well as non-HTTP oriented services) do allow multiple selections or options, so the method always returns an array, even if it has a length of one.
HttpServletRequest adds a few more methods for handling HTTP-specific request data. For instance, getHeaderNames() returns an enumeration of the names of all the HTTP headers submitted with a request, while getHeader() returns a particular header value.
Checking Request Information to Restrict Servlet Access
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SecureRequestServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Semi-Secure Request</TITLE></HEAD>");
out.println("<BODY>");
String remoteHost = req.getRemoteHost();
String scheme = req.getScheme();
String authType = req.getAuthType();
if((remoteHost == null) || (scheme == null) || (authType == null)) {
out.println("Request Information Was Not Available.");
return;
}
if(scheme.equalsIgnoreCase("https") && remoteHost.endsWith(".gov")
&& authType.equals("Digest")) {
out.println("Special, secret information.");
}
else {
out.println("You are not authorized to view this data.");
}
out.println("</BODY></HTML>");
}
}
Services: - Servlet Requests Homework | Servlet Requests Homework Help | Servlet Requests Homework Help Services | Live Servlet Requests Homework Help | Servlet Requests Homework Tutors | Online Servlet Requests Homework Help | Servlet Requests Tutors | Online Servlet Requests Tutors | Servlet Requests Homework Services | Servlet Requests