HTTP/1.1 200 OK Content-Type: text/html;charset=UTF-8 Content-Length: 101 Date: We02d, 06 Jun 2018 07:08:42 GMT <html> <head> <title>$Title$</title> </head> <body> hello , response </body> </html>
設置響應頭:setHeader(String name, String value)html
package cn.testRep.web.servlet; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /* 完成重定向 */ @WebServlet("/responseDemo1") public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException { //訪問/responseDemo1,會自動跳轉responseDemo2 System.out.println("-------------------demo1-----------------"); //設置狀態碼 response.setStatus(302); //設置響應頭 response.setHeader("location","/testResp/responseDemo2"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException { this.doPost(request,response); } } ----------------------------------------------- package cn.testRep.web.servlet; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /* 完成重定向 */ @WebServlet("/responseDemo2") public class ResponseDemo2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException { System.out.println("-------------------demo2-----------------"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException { this.doPost(request,response); } }
forward 和 redirect 區別java
簡單的形式,設置編碼,是在獲取流以前設置web
response.setContentType("text/html;charset=utf-8");瀏覽器
response.setContentType("text/html;charset=utf-8"); //獲取字節輸出流 ServletOutputStream sos = response.getOutputStream(); //輸出數據 sos.write("hello".getBytes("utf-8"));
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> /* 分析: 點擊超連接或者圖片,須要換一張 1.給超連接和圖片綁定單擊事件 2.從新設置圖片的src屬性值 */ window.onload = function(){ //1.獲取圖片對象 var img = document.getElementById("checkCode"); //2.綁定單擊事件 img.onclick = function(){ //加時間戳 var date = new Date().getTime(); //加參,欺騙服務器 img.src = "/testResp/checkCodeServlet?"+date; } } </script> </head> <body> <img id="checkCode" src="/testResp/checkCodeServlet" /> <a id="change" href="">看不清換一張?</a> </body> </html>
package cn.testRep.web.servlet; 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.util.Random; @WebServlet("/checkCodeServlet") public class CheckCodeServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width = 100; int height = 50; //1.建立一個對象,在內存中畫圖 BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //2.美化圖片 //填充背景色 Graphics graphics = image.getGraphics();//畫筆對象 graphics.setColor(new Color(200,200,169));//設置填充顏色 graphics.fillRect(0,0,width,height);//填充 //畫邊框 graphics.setColor(Color.darkGray);//設置邊框顏色 graphics.drawRect(0,0,width-1,height-1);//畫邊框 //寫驗證碼 String str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; for (int i = 0; i < 4 ; i++) { //生成隨機角標 Random ran = new Random(); int index = ran.nextInt(str.length()); char ch = str.charAt(index); graphics.drawString(ch+"",width/5*(i+1),ran.nextInt(height-8)+5); } //畫干擾線 graphics.setColor(new Color(131,175,155)); //隨機生成干擾線座標點 graphics.drawLine(20,30,10,10); //3.輸出到頁面展現 ImageIO.write(image,"jpg",response.getOutputStream()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
方法:String getRealPath(String path)
String b = context.getRealPath("/b.txt");//訪問 web目錄下資源
System.out.println(b);緩存
String c = context.getRealPath("/WEB-INF/c.txt");//訪問WEB-INF目錄下的資源
System.out.println(c);安全
String a = context.getRealPath("/WEB-INF/classes/a.txt");//訪問 src目錄下的資源
System.out.println(a);服務器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="/testResp/downloadServlet?filename=刺客伍六七.jpg">圖片1</a> </body> </html>
package utils; import sun.misc.BASE64Encoder; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class DownLoadUtils { public static String getFileName(String agent, String filename) throws UnsupportedEncodingException { if (agent.contains("MSIE")) { // IE瀏覽器 filename = URLEncoder.encode(filename, "utf-8"); filename = filename.replace("+", " "); } else if (agent.contains("Firefox")) { // 火狐瀏覽器 BASE64Encoder base64Encoder = new BASE64Encoder(); filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else { // 其它瀏覽器 filename = URLEncoder.encode(filename, "utf-8"); } return filename; } }
package cn.testRep.web.servlet; import utils.DownLoadUtils; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; @WebServlet("/downloadServlet") public class DownloadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取請求參數,文件名稱 String filename = request.getParameter("filename"); //找到文件真實路徑 ServletContext servletContext = this.getServletContext(); String realPath = servletContext.getRealPath("/img/" + filename); //用字節流進行關聯,加載進內存 FileInputStream fis = new FileInputStream(realPath); //解決下載文件中文亂碼 String agent = request.getHeader("user-agent"); filename = DownLoadUtils.getFileName(agent,filename); //設置響應頭類型,彈出對話框 String mimeType = servletContext.getMimeType(filename); response.setHeader("content-type",mimeType); //設置響應頭打開方式,filename在此爲下載文件名 response.setHeader("content-disposition","attachment;filename="+filename); //將輸入流數據寫入輸出流中 ServletOutputStream outputStream = response.getOutputStream(); byte[] buffer = new byte[1024*8]; int len =0; while ((len=fis.read(buffer)) != -1){ outputStream.write(buffer,0,len); } fis.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }