HttpServletResponse and HttpServletRequest

Source: Internet
Author: User
Tags call back

  1. Principles of coding issues
    1. The English alphabet does not appear garbled, all encoding tables contain Unicode encoding tables
    2. Character stream = byte stream + encoding table using the Tomcat default encoding table when using Tomcat server
    3. tomcat6.x tomcat7.x default is all iso-8859-1 tomcat8.x default use UTF-8
    4. In JSP programming, GetByte (); The default is to use the ANSI local encoding table GBK
    5. GetByte () has a construct with an encoded table that specifies what encoding the character will use to return a byte array
  2. HttpServletResponse
    1. getoutputstream and getwriter () cannot be used together????

      These two streams do not need to be released manually, the servlet will automatically release????

      response.getoutputstream (). Write ( "Chinese garbled" .getbytes ());

      response.getwriter (). Print ( "Chinese garbled" )

    2. Common headers
      1. Refresh Response.setheader ("Refresh", "1"), refresh once a second
      2. Sets the specified time redirection response.setheader ("Refresh", "3; Url=/index.jsp "); URL can be omitted

        Forwarding:/representing the current application

    • Only one request, shared request
    • Use forwarding when you need to share request
    • The address bar does not change
    • Internal behavior of the server
    • Only internal addresses can be forwarded

Implement page jump by getting context object/represent the current project cannot use relative path

Getservletcontext (). Getrequestdispatcher ("/index.jsp"). Forward (request, response);

Get Request object, implement page jump

Request.getrequestdispatcher ("/test.html"). Forward (request, response);

Redirect:/represents the Tomcat root directory

    • You can forward internal addresses, or you can access external resources
    • do not write/represent relative paths

      < Span style= "color:black; Font-family: Song body; Font-size:10pt ">//uses relative paths when pointing to is the current class path

      response.sendredirect" myServlet_ OutputStream ");

      response.setheader ("Refresh", "3; Url= class file in the current type Space ");

    1. Buffer Time Response.setdateheader ("Expires", System.currentimemillis () +1*1000*60*60); /1 hours

      The Address bar direct enter key is used when the cache needs to be refreshed to update the page

      A static web resource can be cached

      Dynamic Web resources are not recommended for use with cached

      Control without buffering

// Control No Cache

Response.setheader ("Expires","0");

Response.setheader ("Cache-control","No-cache");

Response.setheader ("Pragma","No-cache");

  1. the solution of character stream garbled
    1. Modify Browse parsing encoding
    2. Tell the browser that you are using UTF-8, and that byte arrays are also UTF-8 (the following three types) of bytes output
    3. Write the character label directly response.getoutputstream (). write (); è not recommended for use

      Response.getoutputstream (). Write ("<meta http-equiv=\" content-type\ "content=\" text/html; charset=UTF-8\ " > ". getBytes ());

      Response.getoutputstream (). Write (" Chinese garbled ". GetBytes ("UTF-8"));

      When the header tag is in use, some browsers will directly recognize it as a string display.

    4. response.setheader ( "Content-type" "Text/html;charset=utf-8"

      response.getOut Putstream (). Write ( " Chinese garbled " .getbytes ( "UTF-8"

    5. directly set the value within the label Response.setcontenttype (); è recommended use of

      response.setcontenttype (" Text/html;charset=utf-8 "

      response.getOut Putstream (). Write ( " Chinese garbled " .getbytes ( "UTF-8"

  2. A solution to the character stream garbled

    a , write the label directly in the character stream can not be used, the rest are available, recommended as follows!

    Response.setcontenttype ("Text/html;charset=utf-8");

    response.getwriter (). Print (" Chinese garbled d");

    1. HttpServletRequest
      1. Method of Request
    • Java.lang.String Getcontextpath ( Apply name
    • ???? Java.lang.String GetMethod () Gets the request method, gets the uppercase
    • ???? Java.lang.String Getquerystrin () Gets the requested data
    • ???? Java.lang.StringBufferget Requesturl () The access path to the current project, it is recommended to use ServletContext Getrealpath instead of
    • ???? Java.lang.String Getrequesturi () Gets the access path that does not include the host name Port number
    • ???? Getrealpath () Gets the absolute path
    • ???? Getprotocol () Get protocol and version
    • ???? GETREMOTEADDR () Gets the IP address of the caller
    1. Get form data (the most common method)
      1. Gets the form's value Request.getparameter ("") according to the form's name; Get a single value
      2. Gets the value array of the form based on the name of the form request.getparametervalues ("") is generally used for checkboxes and radio
      3. Gets all the name Request.getparameternames () in the form;
      4. Get duplicate form name can only get to the first value retrieved
      5. CheckBox and Radio if not selected, the client does not send data to the server
    2. Capture the internal mechanism of the data (reflection principle)

User user = new User ();

???????? // How much data does the form submit?

???????? enumeration<string> e = request.getparameternames ();

???????? While (e.hasmoreelements ()) {

???????????? // get each form's name

???????????? String fieldName = E.nextelement ();

???????????? String fieldvalue = Request.getparameter (fieldName);

????????????

???????????? try {

???????????????? // Introspection

???????????????? PropertyDescriptor PD = new propertydescriptor (fieldname,user. class );

???????????????? Method m = Pd.getwritemethod (); // that gets the specified field. Set method

???????????????? M.invoke (user, fieldvalue);

????????????} catch (Exception E1) {

???????????????? // TODO auto-generated catch block

???????????????? E1.printstacktrace ();

????????????}

????????????

????}

  1. A simple method of object encapsulation ( use apache

    beanutils.populate (User, Request.getparametermap ());

    import org.apache.commons.beanutils.BeanUtils;

    can automatically get the key and value and encapsulate it into a given object, the principle is inside the province

  2. Chinese garbled question

    get Way: username= new String ( Username.getbytes ("Iso-8859-1"), "UTF-8"); Use to return to the original form

    using Getinpustream () must use post for file uploads!

  3. Jump and include

    // Jump request.getrequestdispatcher ("/register.html"). Forward (request, response);

    // include request.getrequestdispatcher ("/register.html"). Include (request, response);

HttpServletResponse and HttpServletRequest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.