1. A case specific analysis: the use of Request.getparameter () method and request garbled processing
Packagecom.request;
Importjava.io.IOException;
Importjava.io.UnsupportedEncodingException;
Importjava.util.Enumeration;
Importjavax.servlet.ServletException;
Importjavax.servlet.http.HttpServlet;
Importjavax.servlet.http.HttpServletRequest;
Importjavax.servlet.http.HttpServletResponse;
Publicclass Request3 extends HttpServlet {
public void doget (httpservletrequest request,httpservletresponse response)
Throws Servletexception, IOException {
/* Hyperlink garbled problem: If the hyperlink in the JSP file can be directly used in Java urlencoder.eccoding () class to solve,
* But functions in HTML are resolved by means of solving a GET request.
*/
System.out.println (NewString (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "UTF-8");
}
public void Test2 (HttpServletRequest request)
Throws Unsupportedencodingexception {
/* Request mode is get garbled solution, request way for Get method of more trouble, need to manually solve,
* The default encoding for the Tomact server is iso-8859-1, so you need to manually modify the encoding. You can also modify the Server.xml file in Conf in the Tomcat installation path
*/
String name =request.getparameter ("name");
Name = NewString (Name.getbytes ("iso-8859-1"), "UTF-8");
String password = newstring (request.getparameter ("password"). GetBytes ("Iso-8859-1"), "UTF-8");
System.out.println (name+ "----" +password);
}
Form form is requested by post
public void Test1 (HttpServletRequest request) {
Post request method of garbled resolution, only for post function
try {
Request.setcharacterencoding ("UTF-8");
catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
String name =request.getparameter ("username");
String password =request.getparameter ("password");
String sex =request.getparameter ("sex");
String City =request.getparameter ("City");
String[] likes =request.getparametervalues ("likes");
String L = "";
for (inti=0;l!=null&&i<likes.length;i++) {
String like = Likes[i];
L = l+ "" +like;
}
String mome =request.getparameter ("Mome");
System.out.println ("User name:" +name);
System.out.println ("Password:" +password);
System.out.println ("Sex:" +sex);
System.out.println ("City:" +city);
System.out.println ("Resume:" +mome);
System.out.println ("hobby:" +l);
}
public void DoPost (Httpservletrequestrequest, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
2. Anti-Theft Chain Technology Case study
Packagecom.request;
Importjava.io.IOException;
Importjavax.servlet.ServletException;
Importjavax.servlet.http.HttpServlet;
Importjavax.servlet.http.HttpServletRequest;
Importjavax.servlet.http.HttpServletResponse;
Publicclass Request4 extends HttpServlet {
public void doget (httpservletrequest request,httpservletresponse response)
Throws Servletexception, IOException {
Solve the response of garbled, in fact, the response is garbled from this servlet class to get the Chinese characters, request garbled is from the HTML or JSP interface request from the Chinese characters
Response.setcontenttype ("Text/html;charset=utf-8");
Gets the referer in the request header (reference, that is, the anti-theft parameter)
String referer =request.getheader ("Referer");
To make a judgment, if the Referer parameter is blank or not the format I requested, redirect to the interface I specified, or it must be right or wrong here.
if (Referer==null | |! Referer.startswith ("http://localhost")) {
Response.sendredirect ("/webdemo/index.jsp");
Return
}
String value = "Secret ... ";
Response.getwriter (). write (value);
}
public void DoPost (Httpservletrequestrequest, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
The 3.Servlet API defines a RequestDispatcher interface, commonly known as a request dispatcher . It defines the following two methods:
public Voidforward (ServletRequest request, servletresponse response)
public void include (ServletRequest request, Servletresponseresponse)
There are two main ways to get RequestDispatcher instances:
• Invoke the Getrequestdispatcher (StringUrl) method provided by the ServletContext interface.
• Invoke the Getrequestdispatcher (StringUrl) method provided by the ServletRequest interface.
• include method:
The Requestdispatcher.include method is used to include the resource content encapsulated by the RequestDispatcher object as part of the current response content, enabling programmable server-side inclusion functionality. The included Servlet program cannot change the status code and response headers of the response message, and if there are such statements in it, the execution results of those statements are ignored.
Application scenario for request forwarding : MVC design Pattern (detailed below)
The L Request object provides a Getrequestdispatcher method that returns a RequestDispatcher object that invokes the forward method of this object to implement request forwarding.
The Request object is also a domain object , and the developer brings the data through the request object to other Web resources through the request object when implementing the forwarding process.
Üsetattribute method
Ügetattribute method
Üremoveattribute method
Ügetattributenames method
Example :
Packagecom.request;
Importjava.io.IOException;
Importjavax.servlet.ServletException;
Importjavax.servlet.http.HttpServlet;
Importjavax.servlet.http.HttpServletRequest;
Importjavax.servlet.http.HttpServletResponse;
Publicclass Request5 extends HttpServlet {
public void doget (httpservletrequest request,httpservletresponse response)
Throws Servletexception, IOException {
Request.getrequestdispatcher ("/header.html"). Include (Request,response);
Response.getoutputstream (). Write ("Aaaaaaaaaa". GetBytes ());
Request.getrequestdispatcher ("/fooder.html"). Include (Request,response);
}
public void DoPost (Httpservletrequestrequest, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}