Servlet Request Header Response Application simple case

Source: Internet
Author: User

Servlet Request Header Response Application simple case: Access Aservlet redirect to bservlet,5 seconds after jumping to cservlet and displaying the picture:

Aservlet

1  PackageCn.yzu;2 Importjava.io.IOException;3 Importjavax.servlet.ServletException;4 ImportJavax.servlet.http.HttpServlet;5 Importjavax.servlet.http.HttpServletRequest;6 ImportJavax.servlet.http.HttpServletResponse;7  Public classAservletextendsHttpServlet {8 9      Public voiddoget (httpservletrequest request, httpservletresponse response)Ten             throwsservletexception, IOException { OneSystem.out.println ("Aservlet is accessed to ... "); A         //REDIRECT request header, can be replaced by a response.sendredirect ("Bservlet"); -Response.setstatus (302); -Response.setheader ("Location", "Bservlet"); the          -         //Cancel Cache request header, 3 groups suitable for all browsers, available <meta> tags instead -Response.setheader ("Pragma", "No-cache"); -Response.setheader ("Cache-control", "No-cache"); +Response.setdateheader ("Expires", 1); -     } +}
View Code

Bservlet

1  PackageCn.yzu;2 3 Importjava.io.IOException;4 5 Importjavax.servlet.ServletException;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten  Public classBservletextendsHttpServlet { One      Public voiddoget (httpservletrequest request, httpservletresponse response) A             throwsservletexception, IOException { -System.out.println ("Bservlet is accessed to ... "); -System.out.println ("Auto Jump to Cservlet after 3 seconds"); the         //Auto Refresh request Header -Response.setheader ("Refresh", "3; Url=cservlet "); -     } -}
View Code

Cservlet

1  PackageCn.yzu;2 3 ImportJava.io.FileInputStream;4 Importjava.io.IOException;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten  One Importorg.apache.commons.io.IOUtils; A  -  Public classCservletextendsHttpServlet { -      Public voiddoget (httpservletrequest request, httpservletresponse response) the             throwsservletexception, IOException { -System.out.println ("Cservlet is accessed to ... "); -         //Response Response Byte stream (universal, can also output characters) -FileInputStream stream=NewFileInputStream ("c:/users/administrator/desktop/ui/Avatar/lebron.jpeg"); +         //io gadget, provided by Apache, converts input flow into byte array -         bytebt[]=Ioutils.tobytearray (stream); +         //Note: It is absolutely not possible to use at the same time as response.getwriter (), otherwise it will throw IllegalStateException exception!!!!!!!  A Response.getoutputstream (). write (BT); at          -     } -  -}
View Code

Operation Result:

Note: Io Gadget jar Package:

Servlet Request Header Response Application simple case

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.