1.簡介:
2.HttpServletResponse
response常見應用:
①向客戶端輸出中文數據:
OutoutStream:
//在servlet中用outputStream輸數據的問題,以及輸出中文問題。
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//程序以什麼碼錶輸出,就一定要告訴瀏覽器以什麼碼錶打開。
//細節:html: <meta>標籤可以模擬一個http響應頭
//如果要輸出數字,應該用字符串把數字包起來。
response.setHeader("Content-type", "text/html;charset=UTF-8");//;寫成 ,會下載
String data = "中國";
OutputStream out = response.getOutputStream();
//out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>");
out.write(data.getBytes("UTF-8"));//設置編碼格式
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
PrintWriter : 設置編碼表方式不同。
//通過response的write流輸出數據
//printwriter: 只是設置編碼表方式不同
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//設置response使用的碼錶,以控制response以什麼碼錶向瀏覽器寫出數據
response.setCharacterEncoding("UTF-8"); //設置編碼格式
//指定瀏覽器以什麼碼錶打開服務器發送的數據
response.setHeader("content-type", "text/html;charset=UTF-8");
//上面兩句的簡單表示形式:
// response.setContentType("text/html;cherset=UTF-8");
String data = "中國";
PrintWriter out = response.getWriter();
out.write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
②文件下載
//文件下載
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath(
"/download/秒速5釐米.jpg");
String filename = path.substring(path.lastIndexOf("\\") + 1);
//如果下載文件是中文文件,則文件名需要經過url編碼
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(path);
int len = 0;
byte[] buffer = new byte[11024];
out = response.getOutputStream();
while ((len = in.read(buffer)) != 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
new RuntimeException("讀寫失敗!");
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
// throw new RuntimeException("關閉失敗");
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
// throw new RuntimeException("關閉失敗");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
③輸出隨機圖片:
//輸出一張隨機圖片,類 BufferedImage,類ImageIO類
public class ResponseDemoP extends HttpServlet {
public static final int WIDTH = 120;
public static final int HEIGHT = 27;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 1.設置背景色
setBackGround(g);
// 2.設置邊框
setBorder(g);
// 4.寫隨機數
drawRandowNum((Graphics2D)g); //爲了旋轉
// 3.畫干擾線
drawRandowLine(g);
// 5.圖形寫給瀏覽器
response.setContentType("image/jpeg");
//控制瀏覽器不要緩存
response.setDateHeader("expirse", -1);
response.setHeader("cache-control", "no-cache");
response.setHeader("pragma", "no-cache");
ImageIO.write(image, "jpg", response.getOutputStream());
}
private void setBackGround(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
private void setBorder(Graphics g) {
g.setColor(Color.BLUE);
g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
}
private void drawRandowLine(Graphics g) {
g.setColor(Color.GREEN);
for (int i = 0; i < 5; i++) {
int x1 = new Random().nextInt(WIDTH-6)+3; //讓線條不和邊框重疊
int y1 = new Random().nextInt(HEIGHT-6)+3;
int x2 = new Random().nextInt(WIDTH-6)+3;
int y2 = new Random().nextInt(HEIGHT-6)+3;
g.drawLine(x1, y1, x2, y2);
}
}
private void drawRandowNum(Graphics2D g) {
g.setColor(Color.RED);
g.setFont(new Font("宋體",Font.BOLD,20));
//常用漢字
String base = "\u7684\u4e00\u662f\u4e86\u6211\u4e0d\u4eba\u5728\u4ed6\u6709\u8fd9\u4e2a\u4e0a\u4eec\u6765\u5230\u65f6\u5927\u5730\u4e3a\u5b50\u4e2d\u4f60\u8bf4\u751f\u56fd\u5e74\u7740\u5c31\u90a3\u548c\u8981\u5979\u51fa\u4e5f\u5f97\u91cc\u540e\u81ea\u4ee5\u4f1a\u5bb6\u53ef\u4e0b\u800c\u8fc7\u5929\u53bb\u80fd\u5bf9\u5c0f\u591a\u7136\u4e8e\u5fc3\u5b66\u4e48\u4e4b\u90fd\u597d\u770b\u8d77\u53d1\u5f53\u6ca1\u6210\u53ea\u5982\u4e8b\u628a\u8fd8\u7528\u7b2c\u6837\u9053\u60f3\u4f5c\u79cd\u5f00\u7f8e\u603b\u4ece\u65e0\u60c5\u5df1\u9762\u6700\u5973\u4f46\u73b0\u524d\u4e9b\u6240\u540c\u65e5\u624b\u53c8\u884c\u610f\u52a8\u65b9\u671f\u5b83\u5934\u7ecf\u957f\u513f\u56de\u4f4d\u5206\u7231\u8001\u56e0\u5f88\u7ed9\u540d\u6cd5\u95f4\u65af\u77e5\u4e16\u4ec0\u4e24\u6b21\u4f7f\u8eab\u8005\u88ab\u9ad8\u5df2\u4eb2\u5176\u8fdb\u6b64\u8bdd\u5e38\u4e0e\u6d3b\u6b63\u611f\u89c1\u660e\u95ee\u529b\u7406\u5c14\u70b9\u6587\u51e0\u5b9a\u672c\u516c\u7279\u505a\u5916\u5b69\u76f8\u897f\u679c\u8d70\u5c06\u6708\u5341\u5b9e\u5411\u58f0\u8f66\u5168\u4fe1\u91cd\u4e09\u673a\u5de5\u7269\u6c14\u6bcf\u5e76\u522b\u771f\u6253\u592a\u65b0\u6bd4\u624d\u4fbf\u592b\u518d\u4e66\u90e8\u6c34\u50cf\u773c\u7b49\u4f53\u5374\u52a0\u7535\u4e3b\u754c\u95e8\u5229\u6d77\u53d7\u542c\u8868\u5fb7\u5c11\u514b\u4ee3\u5458\u8bb8\u7a1c\u5148\u53e3\u7531\u6b7b\u5b89\u5199\u6027\u9a6c\u5149\u767d\u6216\u4f4f\u96be\u671b\u6559\u547d\u82b1\u7ed3\u4e50\u8272\u66f4\u62c9\u4e1c\u795e\u8bb0\u5904\u8ba9\u6bcd\u7236\u5e94\u76f4\u5b57\u573a\u5e73\u62a5\u53cb\u5173\u653e\u81f3\u5f20\u8ba4\u63a5\u544a\u5165\u7b11\u5185\u82f1\u519b\u5019\u6c11\u5c81\u5f80\u4f55\u5ea6\u5c71\u89c9\u8def\u5e26\u4e07\u7537\u8fb9\u98ce\u89e3\u53eb\u4efb\u91d1\u5feb\u539f\u5403\u5988\u53d8\u901a\u5e08\u7acb\u8c61\u6570\u56db\u5931\u6ee1\u6218\u8fdc\u683c\u58eb\u97f3\u8f7b\u76ee\u6761\u5462\u75c5\u59cb\u8fbe\u6df1\u5b8c\u4eca\u63d0\u6c42\u6e05\u738b\u5316\u7a7a\u4e1a\u601d\u5207\u600e\u975e\u627e\u7247\u7f57\u94b1\u7d36\u5417\u8bed\u5143\u559c\u66fe\u79bb\u98de\u79d1\u8a00\u5e72\u6d41\u6b22\u7ea6\u5404\u5373\u6307\u5408\u53cd\u9898\u5fc5\u8be5\u8bba\u4ea4\u7ec8\u6797\u8bf7\u533b\u665a\u5236\u7403\u51b3\u7aa2\u4f20\u753b\u4fdd\u8bfb\u8fd0\u53ca\u5219\u623f\u65e9\u9662\u91cf\u82e6\u706b\u5e03\u54c1\u8fd1\u5750\u4ea7\u7b54\u661f\u7cbe\u89c6\u4e94\u8fde\u53f8\u5df4\u5947\u7ba1\u7c7b\u672a\u670b\u4e14\u5a5a\u53f0\u591c\u9752\u5317\u961f\u4e45\u4e4e\u8d8a\u89c2\u843d\u5c3d\u5f62\u5f71\u7ea2\u7238\u767e\u4ee4\u5468\u5427\u8bc6\u6b65\u5e0c\u4e9a\u672f\u7559\u5e02\u534a\u70ed\u9001\u5174\u9020\u8c08\u5bb9\u6781\u968f\u6f14\u6536\u9996\u6839\u8bb2\u6574\u5f0f\u53d6\u7167\u529e\u5f3a\u77f3\u53e4\u534e\u8ae3\u62ff\u8ba1\u60a8\u88c5\u4f3c\u8db3\u53cc\u59bb\u5c3c\u8f6c\u8bc9\u7c73\u79f0\u4e3d\u5ba2\u5357\u9886\u8282\u8863\u7ad9\u9ed1\u523b\u7edf\u65ad\u798f\u57ce\u6545\u5386\u60ca\u8138\u9009\u5305\u7d27\u4e89\u53e6\u5efa\u7ef4\u7edd\u6811\u7cfb\u4f24\u793a\u613f\u6301\u5343\u53f2\u8c01\u51c6\u8054\u5987\u7eaa\u57fa\u4e70\u5fd7\u9759\u963f\u8bd7\u72ec\u590d\u75db\u6d88\u793e\u7b97\u4e49\u7adf\u786e\u9152\u9700\u5355\u6cbb\u5361\u5e78\u5170\u5ff5\u4e3e\u4ec5\u949f\u6015\u5171\u6bdb\u53e5\u606f\u529f\u5b98\u5f85\u7a76\u8ddf\u7a7f\u5ba4\u6613\u6e38\u7a0b\u53f7\u5c45\u8003\u7a81\u76ae\u54ea\u8d39\u5012\u4ef7\u56fe\u5177\u521a\u8111\u6c38\u6b4c\u54cd\u5546\u793c\u7ec6\u4e13\u9ec4\u5757\u811a\u5473\u7075\u6539\u636e\u822c\u7834\u5f15\u98df\u4ecd\u5b58\u4f17\u6ce8\u7b14\u751a\u67d0\u6c89\u8840\u5907\u4e60\u6821\u9ed8\u52a1\u571f\u5fae\u5a18\u987b\u8bd5\u6000\u6599\u8c03\u5e7f\u8716\u82cf\u663e\u8d5b\u67e5\u5bc6\u8bae\u5e95\u5217\u5bcc\u68a6\u9519\u5ea7\u53c2\u516b\u9664\u8dd1\u4eae\u5047\u5370\u8bbe\u7ebf\u6e29\u867d\u6389\u4eac\u521d\u517b\u9999\u505c\u9645\u81f4\u9633\u7eb8\u674e\u7eb3\u9a8c\u52a9\u6fc0\u591f\u4e25\u8bc1\u5e1d\u996d\u5fd8\u8da3\u652f\u6625\u96c6\u4e08\u6728\u7814\u73ed\u666e\u5bfc\u987f\u7761\u5c55\u8df3\u83b7\u827a\u516d\u6ce2\u5bdf\u7fa4\u7687\u6bb5\u6025\u5ead\u521b\u533a\u5965\u5668\u8c22\u5f1f\u5e97\u5426\u5bb3\u8349\u6392\u80cc\u6b62\u7ec4\u5dde\u671d\u5c01\u775b\u677f\u89d2\u51b5\u66f2\u9986\u80b2\u5fd9\u8d28\u6cb3\u7eed\u54e5\u547c\u82e5\u63a8\u5883\u9047\u96e8\u6807\u59d0\u5145\u56f4\u6848\u4f26\u62a4\u51b7\u8b66\u8d1d\u8457\u96ea\u7d22\u5267\u554a\u8239\u9669\u70df\u4f9d\u6597\u503c\u5e2e\u6c49\u6162\u4f5b\u80af\u95fb\u5531\u6c99\u5c40\u4f2f\u65cf\u4f4e\u73a9\u8d44\u5c4b\u51fb\u901f\u987e\u6cea\u6d32\u56e2\u5723\u65c1\u5802\u5175\u4e03\u9732\u56ed\u725b\u54ed\u65c5\u8857\u52b3\u578b\u70c8\u59d1\u9648\u83ab\u9c7c\u5f02\u62b1\u5b9d\u6743\u9c81\u7b80\u6001\u7ea7\u7968\u602a\u5bfb\u6740\u5f8b\u80dc\u4efd\u6c7d\u53f3\u6d0b\u8303\u5e8a\u821e\u79d8\u5348\u767b\u697c\u8d35\u5438\u8d23\u4f8b\u8ffd\u8f83\u804c\u5c5e\u6e10\u5de6\u5f55\u4e1d\u7259\u515a\u7ee7\u6258\u8d76\u7ae0\u667a\u51b2\u53f6\u80e1\u5409\u5356\u575a\u559d\u8089\u9057\u6551\u4fee\u677e\u4e34\u85cf\u62c5\u620f\u5584\u536b\u836f\u60b2\u6562\u9760\u4f0a\u6751\u6234\u8bcd\u68ee\u8033\u5dee\u77ed\u7956\u4e91\u89c4\u7a97\u6563\u8ff7\u6cb9\u65e7\u9002\u4e61\u67b6\u6069\u6295\u5f39\u94c1\u535a\u96f7\u5e9c\u538b\u8d85\u8d1f\u52d2\u6742\u9192\u6d17\u91c7\u6beb\u5634\u6bd5\u4e5d\u51b0\u65e2\u72b6\u4e71\u666f\u5e2d\u73cd\u7ae5\u9876\u6d3e\u7d20\u8131\u519c\u7591\u7ec3\u91ce\u6309\u72af\u62cd\u5f81\u574f\u9aa8\u4f59\u627f\u7f6e\u81d3\u5f69\u706f\u5de8\u7434\u514d\u73af\u59c6\u6697\u6362\u6280\u7ffb\u675f\u589e\u5fcd\u9910\u6d1b\u585e\u7f3a\u5fc6\u5224\u6b27\u5c42\u4ed8\u9635\u739b\u6279\u5c9b\u9879\u72d7\u4f11\u61c2\u6b66\u9769\u826f\u6076\u604b\u59d4\u62e5\u5a1c\u5999\u63a2\u5440\u8425\u9000\u6447\u5f04\u684c\u719f\u8bfa\u5ba3\u94f6\u52bf\u5956\u5bab\u5ffd\u5957\u5eb7\u4f9b\u4f18\u8bfe\u9e1f\u558a\u964d\u590f\u56f0\u5218\u7f6a\u4ea1\u978b\u5065\u6a21\u8d25\u4f34\u5b88\u6325\u9c9c\u8d22\u5b64\u67aa\u7981\u6050\u4f19\u6770\u8ff9\u59b9\u85f8\u904d\u76d6\u526f\u5766\u724c\u6c5f\u987a\u79cb\u8428\u83dc\u5212\u6388\u5f52\u6d6a\u542c\u51e1\u9884\u5976\u96c4\u5347\u7883\u7f16\u5178\u888b\u83b1\u542b\u76db\u6d4e\u8499\u68cb\u7aef\u817f\u62db\u91ca\u4ecb\u70e7\u8bef";
int x = 10;
//已有漢字區間:[\u4e00-\u9fa5]
for(int i=0; i<4; i++){
int degree = new Random().nextInt()%30; //控制隨機旋轉弧度,正反旋轉
String ch = base.charAt(new Random().nextInt(base.length()))+"";
g.rotate(degree*Math.PI/180,x+10,10);
g.drawString(ch, x, 21);//漢字的實際顯示長度是小於20的,所以顯示出來和上邊存在距離
g.rotate(-degree*Math.PI/180,x+10,10); //轉回去
x += 27;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
register.html:
<html>
<head>
<title>register.html</title>
// <script type="text/javascript">
//function changeImage(img){
//alert("更新已執行");
//img.src = img.src+"?"+new Date().getTime(); //讓地址發生變化,不取緩存,實現刷新
//}
// </script>
</head>
<body>
<form action="">
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
驗證碼:<input type="text" name="checkcode">
<img src="/day06/servlet/ResponseDemoP" onclick="this.src = this.src+'?'+new Date().getTime()" alt="換一張" style="cursor:pointer"><br>
<input type="submit" value="註冊">
</form>
</body>
</html>
④控制瀏覽器定時刷新網頁(響應頭:refersh):
//控制瀏覽器定時刷新
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test3(request,response); //實用的自動跳轉技術
}
public void test3(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
//假設這是一個用戶登陸的servlet
//程序到此運行到此,用戶登陸成功了
String message="<meta http-equiv='refresh' content='3;url=/day06/index.jsp'>登陸成功,在3秒後會跳到首頁。如果沒有跳轉,請點擊<a href=''>超鏈接</a>";
this.getServletContext().setAttribute("message", message);
this.getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
}
public void test2(HttpServletResponse response) throws IOException {
//假設這是一個用戶登陸的servlet
//程序到此運行到此,用戶登陸成功了
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("refresh", "3; url = '/day06/index.jsp' ");
response.getWriter().write("登陸成功,在3秒後會跳到首頁。如果沒有跳轉,請點擊<a href=''>超鏈接</a>");
}
public void test1(HttpServletResponse response) throws IOException {
response.setHeader("refresh", "3");
String data = new Random().nextInt(100000)+"";
response.getWriter().write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
message.jsp: 跳轉中間界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'message.jsp' starting page</title>
</head>
<body>
<%
String message = (String)application.getAttribute("message");
out.write(message);
%>
</body>
</html>
index.jsp:跳轉到的界面(首頁等)
⑤控制瀏覽器緩存:servlet數據一直不變的情形。
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="/day06/servlet/ResponseDemo1">查看圖書</a>
</body>
</html>
//控制瀏覽器緩存
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setDateHeader("expires", System.currentTimeMillis()+1000*3600); //緩存一小時
String data = "hahahahaah";
response.getWriter().write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
⑥實現請求重定向:一般不用。
登陸用,因爲重定向會使地址欄方式變化,告知用戶登陸成功。
購物車,用轉發的話會重新購買一次。
//實現請求重定向:
/*
特點:
1.瀏覽器會向服務器發送兩次,意味着就有兩個request\response
2.用重定向技術,瀏覽器地址欄會發生變化
用戶登錄和顯示購物車時,通常會用到重定向技術。
*/
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* response.setStatus(302);
response.setHeader("location", "/day06/index.jsp");*/
response.sendRedirect("/day06/index.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
response細節:
3.HttpServletRequest
URI: 請求一個資源 /news/html
URL: 請求一個互聯網上的資源 http://www.bai.com/news
示例:
①getRequestURI(),getRequestURL():
應用場景:權限攔截,統計頁面訪問次數等等。URI用的比較多
示例:
②request獲取請求頭和請求數據:
//獲取請求頭信息和請求數據。
public class RequestDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// test1(request);
test2(request);
}
//獲取請求數據
//一般來說,都要先檢查再使用(健壯性判斷)
public void test2(HttpServletRequest request) throws IOException {
//1.獲取請求數據
String value = request.getParameter("username");
if(value!=null && !value.trim().equals("")){
System.out.println(value);
}
//2.獲取請求數據集
String[] values = request.getParameterValues("username");
for(int i=0;values!=null && i<values.length;i++){
System.out.println(values[i]);
}
//3.獲取請求數據名集,再迭代獲取值
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
value = request.getParameter(name);
}
//4.獲取請求數據map集合(用的比較多)
Map<String,String[]> map = request.getParameterMap(); //可能值有多個,所以值是數組
User user = new User();
try {
BeanUtils.populate(user, map); //用map集合填充bean
// BeanUtils.copyProperties(user, formbeam); //bean的拷貝,formbean用來校驗
} catch (Exception e1) {
e1.printStackTrace();
}
//5.獲取請求數據(一般文件上傳用)。如果前面Servlet調用了getParameter系列方法,則這裏將失效。
InputStream in = request.getInputStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len=in.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
}
//獲取請求頭相關的方法
public void test1(HttpServletRequest request) {
//1.獲得請求頭信息
String headValue = request.getHeader("Accept-Encoding");
//2.獲得請求頭信息集
Enumeration e = request.getHeaders("Accept-Encoding");
while(e.hasMoreElements()){
String value = (String)e.nextElement();
}
//3.獲得請求頭名集,再迭代獲取值
e = request.getHeaderNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
String value = request.getHeader(name);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
test.html:
<!DOCTYPE html>
<html>
<head>
<title>test.html</title>
</head>
<body>
<a href="/day06/servlet/RequestDemo1?username=xxx">用戶名</a>
<form action="/day06/servlet/RequestDemo1" method="post">
用戶名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>
User.java:bean
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
上面示例中request.getInputStream()獲取數據失敗的原因:
根據Servlet規範,如果同時滿足下列條件,則請求體(Entity)中的表單數據,將被填充到request的parameter集合中(request.getParameter系列方法可以讀取相關數據):
1 這是一個HTTP/HTTPS請求
2 請求方法是POST(querystring無論是否POST都將被設置到parameter中)
3 請求的類型(Content-Type頭)是application/x-www-form-urlencoded
4 Servlet調用了getParameter系列方法
如果上述條件沒有同時滿足,則相關的表單數據不會被設置進request的parameter集合中,相關的數據可以通過request.getInputStream()來訪問。反之,如果上述條件均滿足,相關的表單數據將不能再通過request.getInputStream()來讀取。
常見應用
request的請求轉發
細節1 舉例:response已經將數據部分寫回了客戶端,無法再進行轉發
防止方案:每次跳轉就return
細節2 舉例:response緩存區中已經寫入了數據,但是還沒有發給客戶端時,仍可以實現轉發,但是緩存區中的響應體會被清除。
①防盜鏈
//防盜鏈
public class RequestDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String referer = request.getHeader("referer"); //查看來訪地址
if(referer==null || !referer.startsWith("http://localhost")){
response.sendRedirect("/day06/index.jsp"); //重定向
return;
}
// response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=gbk");
String data = "一篇日記";
response.getWriter().write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
看廣告<br/>
<a href="/day06/servlet/RequestDemo2">看日誌</a>
</body>
</html>
②通過表單收集客戶機數據:
form.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=UTF-8'>
<title>form.html</title>
</head>
<body>
<!-- url後面如果跟了中文數據,要編碼後再提交 -->
<form action="/day06/servlet/RequestDemo" method="post">
用戶名:<input type="text" name="username"><br />
密碼:<input type="password" name="password"><br />
性別:
<input type="radio" name="gender" value="male">男
<input type="radio" name="gender" value="female">女<br/>
所在地:
<select name="city">
<option value="shanghai">上海</option>
<option value="beijing">北京</option>
<option value="changsha">長沙</option>
</select><br/>
愛好:
<input type="checkbox" name="likes" value="sing">唱歌
<input type="checkbox" name="likes" value="dance">跳舞
<input type="checkbox" name="likes" value="basketball">籃球
<br/>
備註:
<textarea rows="6" clos="60" name="description"></textarea><br/>
大頭照:<input type="file" name="image"><br/>
<input type="hidden" name="id" value="123456">
<input type="submit" value="提交">
</form>
</body>
</html>
public class RequestDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); //解決中文亂碼問題
System.out.println(request.getParameter("username"));
System.out.println(request.getParameter("password"));
System.out.println(request.getParameter("gender"));
System.out.println(request.getParameter("city"));
String[] likes = request.getParameterValues("likes");
for(int i=0; i<likes.length && likes!=null;i++){
System.out.println(likes[i]);
}
System.out.println(request.getParameter("description")); //如果收到的是中文,需要解決亂碼問題
System.out.println(request.getParameter("id"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
③request亂碼:
//request亂碼問題
//也可以修改服務器的配置(不建議修改)
//①URIEncoding設置URI即get數據的編碼格式,tomcat8.0默認UTF-8
//②useBodyEncodingForURI設置URI的編碼即get是否和請求體的即post一致,默認爲flase
public class RequestDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1(request);
}
// 解決post提交的亂碼。tomcat8.0系統設置的請求體的默認編碼是iso-8859-1
public void test1(HttpServletRequest request)
throws UnsupportedEncodingException {
request.setCharacterEncoding("UTF-8"); // 只對post提交有用,將請求體的默認編碼改成UTF-8
String username = request.getParameter("username");
System.out.println(username);
}
// 解決get提交,超鏈接提交的亂碼(手工處理) //實測不處理,也輸出的是中文。因爲tomcat8.0系統設置的URI默認編碼是UTF-8
public void test2(HttpServletRequest request)
throws UnsupportedEncodingException {
String username = request.getParameter("username");
username = new String(username.getBytes("iso-8859-1"), "UTF-8"); // 手工處理。request底層用iso8859-1處理
System.out.println(username);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
form3.html:
<!DOCTYPE html>
<html>
<head>
<title>中文亂碼問題</title>
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="/day06/servlet/RequestDemo3" method="post">
用戶名:<input type="text" name="username"><br/>
<input type="submit" value="提交">
</form>
<form action="/day06/servlet/RequestDemo3" method="get">
用戶名:<input type="text" name="username"><br/>
<input type="submit" value="get提交">
</form>
<a href="/day06/servlet/RequestDemo3?username=中國">get</a>
</body>
</html>
亂碼問題總結:
1.對於post提交的中文數據:服務器在用request的getParameter()方法獲取客戶端傳來的數據(寫頁面時設置的UTF-8)時,服務器內部設置請求體的解碼錶是iso8859-1(在tomcat文檔中可以查看),所以處理方式只有:
①通過改變請求體的解碼方式:request.setCharacterEncoding("UTF-8");
2.對於get提交的中文數據:服務器在用request的getParameter()方法獲取客戶端傳來的數據(寫頁面時設置的UTF-8)時,tomcat8.0服務器內部設置URI的解碼錶是UTF-8,處理方式有:
①直接獲取,不用改變
如果其他版本服務器內部設置URI的解碼錶是iso8859-1,處理方式有:
①手工處理:new String(value.getBytes("iso8859-1"),"UTF-8");
②修改服務器的配置(不建議修改):兩種方式
URIEncoding屬性(設置URI即get數據的編碼格式):
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
然後直接獲取。
useBodyEncodingForURI屬性(設置URI的編碼即get是否和請求體即post一致,默認爲flase):
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" useBodyEncodingForURI="true"/>
然後當作請求體post來設置,通過直接改變請求體的解碼方式:
request.setCharacterEncoding("UTF-8");
再獲取。
④request實現請求轉發,MVC設計模式
特點:
1.客戶端只發一次請求,而服務器端有多個資源調用
2.客戶端瀏覽地址欄沒有變化。
之前學的請求轉發是通過ServletContext域獲得請求轉發對象的:
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/1.jsp");
rd.forward(request, response);
在學ServletContext域時存在的一個問題:用ServletContext域帶數據來轉發,可能會出現多個servlet對象覆蓋原有的數據的安全問題:通過request域來解決,不會導致數據共享。
示例:
String data = "abacsbd";
//數據帶給1.jsp(不能通過context域,要通過request域)
this.getServletContext().setAttribute("data", data);
<%
String data = (String) application.getAttribute("data");
out.write(data);
%>
request也是一個域對象,目前爲止第二個。
//請求轉發,以及使用request域對象把數據帶給資源
//mvc: ( model (javabean) view(jsp) cotroller(servlet) )
public class RequestDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data ="aaaa";
request.setAttribute("data", data);
//request也可以實現轉發
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
message.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'message.jsp' starting page</title>
</head>
<body>
${data}
<%
String message =(String)request.getAttribute("data");
out.write(message);
%>
</body>
</html>
RequestDispatcher請求轉發對象除了有轉發方法forward()方法外,還有實現包含其他頁面方法include()
//用include實現頁面包含
public class RequestDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/public/head.jsp").include(request, response);
response.getWriter().write("hahahahaha<br/>");
request.getRequestDispatcher("/public/foot.jsp").include(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
head.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
head<br>
foot.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
foot<br>
顯示:
head
hahahaahhaa
foot
4.web工程中各類地址的寫法:
”/“開頭地址原則:如果是給服務器的用的,就是當前應用目錄;如果的給瀏覽器用的,就是網站,網站下面都多個web應用。
”\“:獲取硬盤資源用的
request.getRequestDispatcher("/form.html").forward(request,response);//轉發給服務器
response.sendRedirect("/day01/form.html");//重定向給瀏覽器
this.getServletContext().getRealPath("/form1.html"); //給服務器
this.getServletContext().getResourceAsStream("/form.html"); //給服務器
<a href="/day06/form.html">點</a> //給瀏覽器
<form action="/day06/form.html"></form> //給瀏覽器