## HTTP protocol:
1, the request message: a data server to a client
* Data Format:
1, the request line
2, the request header
3, blank lines request
4, the request body
2, a response message: The server sends the data to the client
* Data Format:
1, response line
1, consists of: Protocol / version response status codes Status Code Description
2, the response status code: the server tells the client browser to the current state of a request and response
1, is a three digit status code
2, classification:
1,1xx: the server receiving the client message, but does not receive complete until after a period of time, multi-state transmission 1xx code
2,2xx: success. Representative: 200
3,3xx: Redirect. Representative: 302 (redirect); 304 (cache access)
4,4xx: client error.
* 404: no corresponding resource request path
* 405: doXxx embodiment there is no corresponding request method
5,5xx: server-side error. Representative: 500 (internal server abnormal)
2, in response to the first
1, the format: first name: Value
2, the common response headers:
1, Content-type: the server tells the client and the volume data format in response to this encoding format
2, Content-disposition: server tells the client to open the body in response to what data format
* Value:
* In-line: defaults, open in the current page
* Attachment; filename-xxx: Open Response body attachment. document dowload
3, blank lines in response
4, in response to the body: the data transmission
* Response string format:
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Content-Length: 101
Date: Wed, 06 Jun 2018 07:08:42 GMT
<html>
<head>
<title>$Title$</title>
</head>
<body>
hello , response
</body>
</html>
## Response Object
1, Function: Setup Response message
1, the line setting response
1, the format: HTTP / 1.1 200 ok
2, sets a Status Code: setStatus (int sc)
2 is provided in response to the head: setHeader (String name, String value)
3, setting response member
* Step:
1, obtain an output stream:
* Character output stream: PrintWrite getWrite ()
* Output stream of bytes: ServletOutPutSream get OutputStream ()
2, using a book stream, outputs the data to the client browser
2, case
1, redirection
* Redirect: Resource jump way
* Code:
package com.Response.demo01; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Redirect */ @WebServlet("/responseDemo01") public class ResponseDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println ( "Go to demo2222" ); / * //. 1, sets a Status Code resp.setStatus(302); 2 //, in response to the first set resp.setHeader("location","/responseDemo02");*/ // more simple redirect method resp.sendRedirect ( "/ responseDemo02" ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
* Redirect features:
1, the address bar coding occurs
2, redirection can access other sites (server) resources
3, the redirection request twice, the object can not be used to share data request
* Forwarding features:
1, the same path forwarding address bar
2, forwarding only have access to resources at the current server
3, a request is forwarded, the request object may be used to share data
* Path wording:
1, the path Category:
* Relative path: it can not determine the unique resources relative path
* Such as: ./ index.html
* Does not begin with / to. Beginning
* Rules: Find the relative position relationship between the current source and target resources
* ./:Current directory
* ../: Back-level directory
* Absolute path: the only resource can be determined by absolute path
A path beginning with /
* Rules: determine the path is defined to whom use? Analyzing request issued in the future from where
* To the client browser to use: need to add (access path project) virtual directory
* It is recommended to obtain dynamic virtual directory: request.getContextPath ()
* <a> <form> Redirect
* Server to use: does not require virtual directory
* Forwarding path
2, server output character data to the browser
* Step:
1. Get the character stream output
2, the output data
* Note:
* Garbage problem:
1, PrintWriter pw = resp.getWriter (); default encoding stream obtained is ISO-8859-1
2, the default settings of the encoded stream
3, the response code tells the browser to use the body
package com.Response.demo02; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Output Number of characters */ @WebServlet("/responseDemo03") public class ResponseDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // set the response code set resp.setCharacterEncoding ( "UTF-. 8" ); resp.setHeader("content-type","text/html;charset=utf-8"); // simple form provided the encoding (to be provided before the acquisition stream) resp.setContentType ( "text / HTML; charset = UTF-. 8" ); // 1, to obtain a character output stream PrintWriter pw = resp.getWriter (); // 2, output data pw.write ( "You okay Feng Jun boat!" ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
3, a book-byte data server to the browser
* Step:
1, obtaining an output stream of bytes
2, the output data
4, codes
1, nature: Photo
2. Objective: To prevent malicious registration form
The basic logic:
package com.Response.demo02; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; /** * Verification Code */ @WebServlet("/checkCode") public class checkCode extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int width = 200 is ; int height 100 = ; // create an object in memory shoe (CAPTCHA objects) the BufferedImage Image = new new the BufferedImage (width, height, BufferedImage.TYPE_3BYTE_BGR); // 2, landscape image // 2.1 filling the background color Graphics G = image.getGraphics (); g.setColor (Color.PINK); // Set brush color g.fillRect (0, 0 , width, height); // 2.2 Draw Border g.setColor (Color.BLUE); g.drawRect(0, 0, width, height); // 3.2 write codes String STR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ; // generating a random angle scaling the Random = R & lt new new the Random (); for ( int I =. 1; I <=. 4; I ++ ) { int index = r.nextInt (STR .length ()); // Get the character char CH = str.charAt (index); // random characters System.out.println (ch); g.drawString(ch + "", width / 5 * i, height / 2); } /*g.drawString("A",20,25); g.drawString("B",40,25); g.drawString("C",60,25); g.drawString("D",80,25);*/ //3.4 画干扰线 g.setColor(Color.green); for (int i = 0; i < 10; i++) { int x1 = r.nextInt(width); int x2 = r.nextInt(width); int y1 = r.nextInt(height); int y2 = r.nextInt(height); g.drawLine(x1,x2,y1,y2); } //g.drawLine(1,1,30,30); // 3, the picture is output to the page ImageIO.write (Image, "JPG" , resp.getOutputStream ()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
Click on the image to generate verification codes
<! DOCTYPE HTML > < HTML lang = "EN" > < head > < Meta charset = "UTF-. 8" > < title > codes </ title > < Script > the window.onload = function () { // click image switch codes the let code = document.getElementById ( " checkCode " ); / * code.onclick = function () { code.src = "/checkCode?" + new Date().getTime(); }*/ let link = document.getElementById("link"); link.onclick = function () { code.src = "/checkCode?" + new Date().getTime(); } } </script> </head> <body> <img src="/checkCode" id="checkCode"> <a href="" id="link">看不清楚图片换一张?</a> </body> </html>