java_ first year _JavaWeb (5)

HttpServletRequest object

All information can be obtained at the time of the client access server, requested by HttpServletRequest object

  • Obtain information about the client
  1. getRequestURL: full URL when the client makes a request to return
  2. getRequestURI: Returns the name of part of the resources requested name row
  3. getQueryString: Returns the request parameter section
  4. getRemoteAddr: returns the IP address of the client's request
  5. ......
String requestURL= request.getRequestURL;
String requestURI = request.getRequestURI;
String queryString = request.getQueryString;
String IP = getRemoteAddr;
  • Obtaining the client's request header
  1. getHeader (string name); return String;
  2. getHeaders (String name); return to the Enumeration;
  3. getHeaderNames (); return to the Enumeration; return all request header;
PrintWriter out = response.getWriter();
Enumertion<String> reqHeadInfos = request.getHeaderNames();
while(reqHeadInfos.hasMoreElements()){
    String name = reqHeadInfos.nextElement();
    String value = request.getHeader(name);
    out.write(name +":"+value);
}    
  • Obtain data submitted by the client
  1. getParameter(String name );
  2. getParameterValues(String name);
  3. getParameterMap (); common framework for the preparation;

Forms Information:

<form action="xxx" method = "post">
<input type="text" name = "userid" maxlength = "2"><br>
<input type="checkbox" name = "hobby" value = "唱歌">唱歌
<input type="checkbox" name = "hobby"Dancing
>"Dance"= value<input type="checkbox" name = "hobby" value = "rap">rap
<input type="checkbox" name = "hobby" value = "打篮球">打篮球
<br>

Submitted to the xxxjava file to get data submitted by the request:

PrintWriter out = response.getWriter();
String username = request.getParameter("name");
String[] hobbys = request.getParameterValues("hobby");
String hobbyStr = "";
for (int i = 0 ;hobbys!=null && i <hobbys.length;i++){
    if(i = hobbys.length-1){
        hobbyStr += hobbys[i];
        }else{
            hobbyStr +=hobbys[i]+",";
        }
}
out.write("name="+username);
out.write("hobbys="+hobbyStr);

request form to receive garbled question

  • POST to submit forms

The reason is garbled and inconsistent coding client server communication, the server can interface inheritance setCharacterEncoding (charset) method comes through ServletRequest to unify the encoding settings;

Unicode first set before getting the data:

request.setCharacterEncoding("UTF-8");
  • GET to submit forms

Even Unicode, GET form to submit will be garbled, for an unknown reason, request objects are ISO8859-1 character encoding to receive the data, in order to solve the garbage problem, request objects must first get to ISO8859-1 character encoding reception the array of data bytes, and then specify "UTF-8" reconstruct encoded string;

PrintWriter out = response.getWriter();
String name = request.getparameter("name");
name = new String(name.getBytes("ISO8859-1"),"UTF-8");
out.write("name = "+name);

Request object implements forwarding

Before we have learned to achieve forwarded through ServletContext:

= ReqDispatcher the RequestDispatcher the this .getServletContext () the getRequestDispatcher (. " / XXX / XXX " ); // the RequestDispatcher objects returned here also be acquired directly through the getRequestDispatcher request object () method to 
reqDispatcher.forward (request, Response); // then achieved by a forward request forwarding method

Thus achieving forwarded by the Request object as follows:

request.getRequestDispatcher("/xxx/xxx").forward(request,response);

 

Guess you like

Origin www.cnblogs.com/lzj-learn/p/11611678.html