Learning from the servlet (ix)

Source: Internet
Author: User
Tags response code

This article describes the last point of the Response object HttpServletResponse object.

The response object controls a timed refresh of the browser, creates a servlet in my web App "Myservlet", sets the response header in the servlet, and the code for timed refreshes is simple:

Response.setheader ("Refresh", "3");    // 3 seconds to refresh one time

You can tell your browser to refresh the page 3 seconds. Of course, the "Refresh" response header can also be timed to jump to the specified page, the following code:

Response.setheader ("Refresh", "3;url= '/myservlet/index.jsp '");

Will access the index.jsp resources under my "Myservlet" web app after 3 seconds of accessing my servlet.

As I said before, using the <meta> tag can simulate the response header, and here you can use the servlet to write data to the client when writing the <meta> tag to the response data entity, which is parsed by the browser and then controlled:

1 String data = "Silver Soul"; 2 response.setcontenttype ("Text/html;charset=utf-8"); 3 response.getwriter (). Write ("<meta http-equiv= ' refresh ' content= ' 3;url=/myservlet/index.jsp ' >" ); 4 Response.getwriter (). write (data);

Note here: Before writing any data to the response object, set up the encoding table that is used, if it is not valid to set the encoding table after the data is written, such as the following code:

1 String data = "Silver Soul"; 2 response.getwriter (). write (data); 3 response.setcontenttype ("Text/html;charset=utf-8");

By looking at Httpwathch you can tell that the encoding table is not set successfully:

  

It is not possible to put the code that sets the encoding table in writing to the response object, as follows:

1 String data = "Silver Soul"; 2 response.getwriter (). Write ("<meta http-equiv= ' refresh ' content= ' 3;url=/myservlet/index.jsp ' >" ); 3 response.setcontenttype ("Text/html;charset=utf-8"); 4 Response.getwriter (). write (data);

This will also set the encoding table invalid, causing the Chinese garbled problem, so before writing any data to the response object, make sure that the encoding table is set up first.

Request redirection: The response code 302 and the "location" response header were previously learned in the HTTP protocol to set the request redirection.

A simple code example (Web project named "Myservlet"):

1 response.setstatus (httpservletresponse.sc_moved_temporarily);  // 302 Status Code 2 response.setheader ("Location", "/myservlet/index.jsp");

For programmers who are less familiar with the HTTP protocol, there are, of course, a quicker way to do it:

1 response.sendredirect ("/myservlet/index.jsp");

Verify: In the Address bar in the browser type the address of the servlet, the browser will jump to the Web directory index.jsp, which can be seen from the browser address bar.

About the details in Request redirection:

One request redirect sends two requests to the server. That is, it generates two response response objects and request requests, and the address bar of the browser changes, and the URL jumps to the redirected page address. This is different from forwarding (as mentioned in the study of Servlet (v)).

Finally, we go back to the Getoutputstream method and Getwriter method of response Response object, these two methods have introduced in detail the problem of solving Chinese garbled characters in the study of Servlet (vii). Now we also need to note that the Getoutputstream method and the Getwriter method are mutually exclusive and cannot be used at the same time. Otherwise, the following problems may occur:

  

Perhaps people will look down on this problem, that is not in a servlet do not use both byte stream and character stream well, pay attention to it.

The problem is certainly not so simple, such as using different IO streams in forwarding, or this problem occurs:

Suppose my "Myservlet" has two servlet:servletresponse1 and ServletResponse2 in this Web project.

The code in SERVLETRESPONSE1 is:

1     response.getwriter (); 2     this. Getservletcontext (). Getrequestdispatcher ("/web-inf/classes/servletresponse2"); 3     Dispatcher.forward (request, response);

The code in SERVLETRESPONSE2 is:

1     Response.getoutputstream ();

So the result is a direct deployment error:

  

The response object is passed to ServletResponse2 in ServletResponse1, and the method that gets the writer object is already called by itself response. So you can no longer get OutputStream objects in ServletResponse2, so pay special attention to getting IO Stream objects when forwarding.

It is also an error to send the servlet to the JSP file after calling the Getoutputstream () method.

For example, the code in SERVLETRESPONSE1 is:

1     OutputStream out = response.getoutputstream (); 2     this. Getservletcontext (). Getrequestdispatcher ("/index.jsp"); 3     Dispatcher.forward (request, response);

And accessing this servlet will see:

  

This is because the Getwriter () method is used by default to get a stream of characters when the servlet writes data to the JSP, and then writes the data to the JSP, which is bound to conflict if the servlet before forwarding the JSP uses Getoutputstream. If you want to pass in a JSP that must be data that uses a byte stream to transfer, check the relevant solution.

Learning from the servlet (ix)

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.