Response Common applications
Output Chinese data to client
Output in OutputStream and PrintWriter, respectively
Learn more: Use the <meta> tags in the HTML language to control browser behavior
Think: With OutputStream output 1, why does the user see not 1?
File download and download of Chinese files
Output random Pictures
Package com.hbsi.response;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Response3 extends HttpServlet {
private static final int width=130;
private static final int height=30;
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
1. Create a picture
BufferedImage image = new BufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);
2. Get the picture
Graphics g = image.getgraphics ();
3 Painting a picture
A. Setting the background color of a picture
SetBackground (g);
B. Setting a rectangle border
SetBorder (g);
c. Adding interference lines
Setrandomline (g);
G. Writing data to a drawing
Setrandomnum (g);
4. Write the picture to the browser
Response.setheader ("Content-type", "image/jpeg");
Response.setcontenttype ("Image/jpeg");
Tell the browser not to cache
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setintheader ("Expires",-1);
Imageio.write (Image, "JPEG", Response.getoutputstream ());
}
private void Setrandomnum (Graphics g) {
G.setcolor (Color.Black);
G.setfont (New Font ("Arial", font.bold,20));
Random random = new random ();
String base = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\ U5730\u53d7\u7cfb\u4ee4\u8df3\u975e\u4f55\u725b\u53d6\u5165\u5cb8\u6562\u6389\u5ffd\u79cd\u88c5\u9876\u6025\ U6797\u505c\u606f\u53e5\u533a\u8863\u822c\u62a5\u53f6\u538b\u6162\u53d4\u80cc\u7ec6 ";
int x=10;
for (int i=0;i<4;i++) {
int rand = Random.nextint (Base.length ()-1);
String str = Base.charat (RAND) + "";
G.setcolor (New Color (Random.nextint (255), Random.nextint (255), Random.nextint (255)));
int degree = Random.nextint ()%30;
((GRAPHICS2D) g). Rotate (degree*math.pi/180, X, 20);
g.DrawString (str, x, 20);
((GRAPHICS2D) g). Rotate (-degree*math.pi/180, X, 20);
x +=30;
}
}
private void SetRandomNum1 (Graphics g) {
G.setcolor (Color.Black);
G.setfont (New Font ("Arial", font.bold,20));
Random random = new random ();
int x=10;
for (int i=0;i<4;i++) {
Generate random numbers
String str = string.valueof (Random.nextint (10));
G.setcolor (New Color (Random.nextint (255), Random.nextint (255), Random.nextint (255)));
int degree = Random.nextint ()%30;
((GRAPHICS2D) g). Rotate (degree*math.pi/180, X, 20);
g.DrawString (str, x, 20);
((GRAPHICS2D) g). Rotate (-degree*math.pi/180, X, 20);
x +=30;
}
}
private void Setrandomline (Graphics g) {
G.setcolor (Color.green);
Random random = new random ();
for (int i=0;i<5;i++) {
int x1 = Random.nextint (WIDTH);
int y1 = Random.nextint (HEIGHT);
int x2 = Random.nextint (WIDTH);
int y2 = random.nextint (HEIGHT);
G.drawline (x1, y1, x2, y2);
}
}
private void SetBorder (Graphics g) {
G.setcolor (Color.Blue);
G.drawrect (1, 1, WIDTH-2, HEIGHT-2);
}
private void SetBackground (Graphics g) {
G.setcolor (Color.White);
G.fillrect (0,0, WIDTH, HEIGHT);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
Send HTTP header, control browser refresh webpage periodically (refresh)
Package com.hbsi.response;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Response4 extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String message = "<meta http-equiv= ' refresh ' content= ' 3;url=/servletdemo/index.jsp ' > Login successful, 3 seconds after automatic jump to the homepage, if not please point <a href= "> Hyperlinks </a>";
This.getservletcontext (). SetAttribute ("message", message);
This.getservletcontext (). Getrequestdispatcher ("/message.jsp"). Forward (request, response);
}
private void Test2 (HttpServletResponse response) throws IOException {
Suppose this is a servlet that handles landings, assuming the landing was successful.
Response.setcontenttype ("Text/html;charset=utf-8");
Response.setheader ("Refresh", "3;url=/servletdemo/index.jsp");
Response.getwriter (). Write ("Login successful, 3 seconds after automatic jump to the homepage, if not please click <a href= ' > Hyperlink </a>");
}
private void Test1 (HttpServletResponse response) throws IOException {
Response.setheader ("Refresh", "1");
Response.getwriter (). Write (New Java.util.Date (). toLocaleString ());
Response.setheader ("Refresh", "3;url=http://www.sina.com");
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
Send HTTP header, control browser disallow caching of current document content redirection
Package com.hbsi.response;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Features of redirection
1. The browser sends 2 requests to the server, with 2 response and request objects
2. The address bar is changed.
public class Response5 extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.getwriter (). Write ("aaaaaaa");
/*response.setstatus (302);
Response.setheader ("Location", "/servletdemo/index.jsp"); */
Response.sendredirect ("/servletdemo/index.jsp");
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
Servlet's Chinese output problem à principle
The Chinese character that the browser receives is not an English symbol itself, but a data set encoded by one of its characters.
The browser must be viewed with the correct character set encoding in order to display the data it receives as the correct Chinese characters.
When the servlet program only needs to output the response body in plain text format, you should typically call the Getwriter method of the Servletresponse object to return a PrintWriter object, and then use the PrintWriter object to write the text content to the client.
The word literals in the Java program exists in memory in the form of Unicode encoding, and PrintWriter objects need to be converted to some other character set encoded byte array when the output word literals.
The PrintWriter object returned by the Getwriter method of the Servletresponse object uses the iso8859-1 character set encoding to convert the Unicode string to a byte array by default, because there is no Chinese character in the iso8859-1 character set. Unicode-encoded Chinese characters are converted to invalid character encodings and output to the client.
Servlet's Chinese output problem à solution
The Servletresponse interface defines the setcharacterencoding, setContentType methods to specify the character set encoding used by the PrintWriter object returned by the Servletresponse.getwriter method.
Call the setContentType method defined in the Servletresponse interface to specify the character set encoding of the response body in the Content-type header field of the HTTP response message.
Using the <meta> tag to simulate the response message header
The response header field of the HTTP message allows the browser to do a variety of useful functions, but this needs to be done by writing a Web server-side program. What if ordinary HTML page makers who do not have server-side programming want to use the Response header field of an HTTP message to implement some special features?
The Http-equiv property of the <meta> tag is specifically defined in the HTML language to simulate the HTTP response message header in an HTML document, and when the browser reads the <meta> tag with the HTTP-EQUIV attribute in the HTML document, It is handled in the same way as the header of the response message sent by the Web server.
Example:
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Refresh" content= "0;url=http://www.it315.org" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
Request redirection is implemented through response.
Request redirection means that when a Web resource receives a client request, it notifies the client to access another Web resource, which is called a request redirection.
Application Scenario: User login.
Implementation method
Response.sendredirect ()
Implementation principle:
302 Status Code and location header for redirection
Response Details
The Getoutputstream and Getwriter methods are used to obtain the output binary data, output text data servletouputstream, PrintWriter objects respectively.
The two methods of Getoutputstream and getwriter are mutually exclusive, and any one of them is called, and no other method can be called.
The data written to the Servletoutputstream or PrintWriter object by the servlet program is fetched by the servlet engine from the response, and the servlet engine treats the data as the body of the response message. It is then combined with the response status line and each response header to output to the client.
After the Serlvet service method finishes, the Servlet engine checks whether the output stream object returned by the Getwriter or Getoutputstream method has already called the Close method, and if not, The servlet engine calls the Close method to close the output stream object.
Response common applications, response details, output random images, timed refresh pages