HttpServletRequest獲取請求頭信息
(1)獲取客戶機請求頭
String getHeader(String name)
Enumeration<String> getHeaders(String name)
Enumeration<String> getHeaderNames()
(2)獲取具體類型客戶機請求頭
int getIntHead(String name)
long getDateHead(String name) 日期對應毫秒
(3)常見的請求頭
referer 記住當前網頁的內容,用於防盜鏈
User-Agent 判斷瀏覽器(MSIE IE瀏覽器,Firefox 火狐瀏覽器,Chrome 谷歌瀏覽器)
if-modified-since 控制緩存
防盜鏈案例
- Enumeration<String> names = request.getHeaderNames();
- while (names.hasMoreElements()) {
- String name = names.nextElement();
- System.out.println(name + ":" + request.getHeader(name));
- }
- System.out.println("-----------------------------------------");
-
- System.out.println("您使用瀏覽器:" + request.getHeader("user-agent"));
-
- String referer = request.getHeader("referer");
-
- if (referer != null && referer.startsWith("http://localhost/day6")) {
-
- response.setContentType("text/html;charset=utf-8");
- response.getWriter().println("機密信息");
- } else {
-
- response.setContentType("text/html;charset=utf-8");
- response.getWriter().println("您的請求是盜鏈");
- }
HttpServletRequest獲取請求參數(以及POST或GET請求亂碼解決方法)
String getParameter(String name) 經過name獲取一個值
String[] getParameterValues(String name) 經過name獲取多個值 checkbox
Enumeration getParameterNames() 獲取全部請求參數的名稱
Map<String,String[]> getParameterMap()
能夠提交請求的兩種方式
1>.使用<form>
執行form的submit,提交form表單。
經常使用表單元素
<input type=[text | password | radio | checkbox | submit ]>
<select>…<option>
<textarea>
2>.使用URL連接
<a href="http://www.163.com?name=abc&password=123"> 點擊連接 </a>
這樣經過?能夠提交參數,&用來分隔多個參數,
效果和<form>的GET方式相同
解決亂碼問題:
(1)POST請求亂碼
request.setCharacterEncoding("UTF-8");
(2)GET請求亂碼
解決方案一:修改tomcat/conf/server.xml
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
* 必須有修改tomcat服務器配置文件權限
解決方案二:逆向編碼解碼
username = URLEncoder.encode(username,"ISO-8859-1");
username = URLDecoder.decode(username,"UTF-8");
解決方案三:簡寫的方式(推薦使用)
username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
request獲取中文數據亂碼(總結:)
post提交 設置request緩衝區的編碼 request.setCharacterEncoding("utf-8");
get提交 * String構造方法 username = new String(username.getBytes("ISO-8859-1"),"utf-8");
非空有效校驗
if (username != null && username.trim().length() > 0) {
System.out.println("username 有效");
}else{
// username 無效
}
URL特殊字符轉義規則:
經常使用轉義規則
空格換成加號(+)
+換成%2B
正斜槓(/)分隔目錄和子目錄 換成%2F
問號(?)分隔URL和查詢 換成%3F
百分號(%)制定特殊字符 換成%25
#號指定書籤 換成%23
&號分隔參數 換成%26
java.net.URLEncoder和 java.net.URLDecoder
URL編碼和解碼
1) 瀏覽器提交一次請求,請求中中文字符,自動進行URL編碼 (由瀏覽器完成)
2) web服務器接收到內容以後,自動URL解碼
URLEncoder 完成URL編碼 , URLDecoder 完成URL解碼
服務器端接收請求,請求url : http://localhost/day6/request3?name=aaa+bbb
問題:在服務器端執行 request.getParameter("name"); 值 aaa bbb
HttpServletRequest利用請求域傳遞對象
域對象:所知道的已經有兩個域對象了
ServletContext:服務器一啓動,爲每一個web應用建立一個ServletContext對象,全部servlet實例共享對象。
request:一次請求的範圍。
request對象同時也是一個域對象,開發人員經過request對象在實現轉發時,把數據經過request對象帶給其它web資源處理
setAttribute方法
getAttribute方法
removeAttribute方法
getAttributeNames方法
request對象提供了一個getRequestDispatcher方法,該方法返回一個RequestDispatcher對象,調用這個對象的forward方法能夠實現請求轉發,從而共享請求中的數據
請求轉發傳遞數據
請求轉發forward 和 請求重定向redirect 區別 ???
1) 轉發一次請求 一次響應; 重定向兩次請求 兩次響應
2) 轉發URL地址 不變,重定向URL地址改變第二個資源地址
3) 轉發只能轉發給同一個網站內部資源,重定向能夠定向到任何網站
4) 轉發中/屬於服務器內部路徑 不寫工程名,重定向/來自客戶端必需要寫工程名
建立RequestServlet4 RequestServlet5
* 使兩個Servlet連續執行 1) 重定向 2) 轉發
最佳應用:Servlet處理數據獲得數據處理結果,經過請求轉發,將處理結果傳遞JSP顯示
* Servlet處理數據得到結果,JSP負責數據顯示
request.setAttribute傳遞數據,必需要結合 request.getRequestDispatcher().forward 轉發一塊兒使用!!!
請求重定向和請求轉發的具體區別:
- RequestDispatcher.forward方法只能將請求轉發給同一個WEB應用中的組件;而HttpServletResponse.sendRedirect方法還能夠重定向到同一個站點上的其它應用程序中的資源,甚至是使用絕對URL重定向到其它站點的資源。
- 若是傳遞給HttpServletResponse.sendRedirect方法的相對URL以"/"開頭,它相對於服務器的根目錄;若是建立ResquestDispatcher對象時指定的相對URL以"/"開頭,它是相對於當前WEB應用程序的根目錄。
- 調用HttpServletResponse.sendRedirect方法重定向的訪問過程結束後,瀏覽器地址欄中顯示的URL會發生變化,由初始的URL地址變成重定向的目的URL;調用RequestDispatcher.forward方法的請求轉發過程結束後,瀏覽器地址欄保持初始的URL地址不變。
- HttpServletResponse.sendRedirect方法對瀏覽器的請求直接做出響應,響應的結果就是告訴瀏覽器去從新發出對另一個URL的訪問請求;RequestDispatcher.forward方法在服務器端內部將請求轉發給另一個資源,瀏覽器只知道發出了請求並獲得了響應結果,並不知道在服務器程序內部發生了轉發行爲。
- RequestDispatcher.forward方法的調用者與被調用者之間共享相同的request對象和response對象,它們屬於同一個訪問請求和響應過程;而HttpServletResponse.sendRedirect方法調用者與被調用者使用各自的request對象和response對象,它們屬於兩個獨立的訪問請求和響應過程。
代碼案例:
項目的總體目錄以下:
web.xml
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
-
- <form action="/day10/login" method="post">
- <table border="1" width="50%">
- <tr>
- <td>輸入姓名</td>
- <td>
- <input type="text" name="username" />
- </td>
- </tr>
- <tr>
- <td>輸入密碼</td>
- <td>
- <input type="password" name="password" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="登錄"/>
- </td>
- </tr>
- </table>
- </form>
-
- </body>
- </html>
refresh.html
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <meta http-equiv="refresh" content="5;url=/day10/response/login.html">
- <title>頁面定時跳轉(讀秒)</title>
- </head>
- <body onload="run()">
-
- <h2>頁面將在<span id="spanId">5</span>秒後跳轉</h2>
-
- </body>
- <script type="text/javascript">
- /*
- 讀秒的操做
- 頁面一加載,加載事件 onload
- 執行讀秒的操做,每隔一秒變一次。
- 每隔一秒,js的定時器
- */
- var x = 5;
- function run(){
- var span = document.getElementById("spanId");
- span.innerHTML = x;
- x--;
- window.setTimeout("run()", 1000);
- }
-
- </script>
-
-
- </html>
checkcode.html
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
-
- <form action="/day10/login" method="post">
- <table border="1" width="70%">
- <tr>
- <td>輸入姓名</td>
- <td>
- <input type="text" name="username" />
- </td>
- </tr>
- <tr>
- <td>輸入密碼</td>
- <td>
- <input type="password" name="password" />
- </td>
- </tr>
- <tr>
- <td>驗證碼</td>
- <td>
- <input type="text" name="code" />
- <img id="imgId" src="/day10/checkcode">
- <a href="#" onclick="run()">看不清,換一張</a>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="登錄"/>
- </td>
- </tr>
- </table>
- </form>
- </body>
- <script type="text/javascript">
- // 看不清,換一張,時間戳
- function run(){
- // 獲取圖片
- var image = document.getElementById("imgId");
- image.src = "/day10/checkcode?"+new Date().getTime();
- }
-
- </script>
-
- </html>
reg.html
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form action="/day10/reg" method="get">
- <table border="1" width="50%">
- <tr>
- <td>輸入姓名</td>
- <td>
- <input type="text" name="username" />
- </td>
- </tr>
- <tr>
- <td>輸入密碼</td>
- <td>
- <input type="password" name="password" />
- </td>
- </tr>
- <tr>
- <td>選擇性別</td>
- <td>
- <input type="radio" name="sex" value="nan" />男
- <input type="radio" name="sex" value="nv" />女
- </td>
- </tr>
- <tr>
- <td>選擇愛好</td>
- <td>
- <input type="checkbox" name="love" value="lq" />籃球
- <input type="checkbox" name="love" value="zq" />足球
- <input type="checkbox" name="love" value="pq" />排球
- </td>
- </tr>
- <tr>
- <td>選擇城市</td>
- <td>
- <select name="city">
- <option value="none">--請選擇--</option>
- <option value="bj">北京</option>
- <option value="sh">上海</option>
- <option value="gz">廣州</option>
- </select>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="登錄"/>
- </td>
- </tr>
- </table>
-
- </form>
-
- </body>
- </html>
login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
-
- ${msg}
- <form action="/day10/login2" method="post">
- <table border="1" width="50%">
- <tr>
- <td>輸入姓名</td>
- <td>
- <input type="text" name="username" />
- </td>
- </tr>
- <tr>
- <td>輸入密碼</td>
- <td>
- <input type="password" name="password" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="登錄"/>
- </td>
- </tr>
- </table>
- </form>
-
- </body>
- </html>
CEPServlet.java (禁用瀏覽器緩存)
- package cn.itcast.response;
-
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class CEPServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
- response.setHeader("Cache-Control","no-cache");
- response.setHeader("Pragma","no-cache");
- response.setDateHeader("Expires", -1);
-
-
-
- Date date = new Date();
-
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String str = sdf.format(date);
-
- response.getWriter().write(str);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
TestServlet.java (驗證碼熱身)
- package cn.itcast.response;
-
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
-
- 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 TestServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- BufferedImage image = new BufferedImage(300, 500, BufferedImage.TYPE_INT_RGB);
-
-
- Graphics g = image.getGraphics();
-
- g.setColor(Color.RED);
-
- g.drawRect(100, 100, 150, 100);
-
- g.setColor(Color.YELLOW);
-
- String str = "abc";
- g.drawString(str, 80, 80);
-
-
- ImageIO.write(image, "jpg", response.getOutputStream());
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
CheckcodeServlet.java(實現驗證碼流程)
- package cn.itcast.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;
-
- /**
- * 驗證碼
- * @author Administrator
- *
- */
- public class CheckcodeServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- /**
- * 在內存中生成圖片(紙),沒有設置背景顏色,畫填充的矩形,而且和紙的大小相同,矩形有顏色。
- * 獲取筆的對象(設置顏色,設置字體,畫字符串,畫矩形)
- * 先準備好數據,隨機生成4個字符,把字符畫到紙上
- * 畫干擾線
- * 把內存中的圖片輸出到客戶端上
- */
- int width = 120;
- int height = 30;
- // 在內存中生成圖片
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- // 先獲取畫筆對象
- Graphics2D g = (Graphics2D) image.getGraphics();
- // 設置灰色
- g.setColor(Color.GRAY);
- // 畫填充的矩形
- g.fillRect(0, 0, width, height);
- // 設置顏色
- g.setColor(Color.BLUE);
- // 畫邊框
- g.drawRect(0, 0, width-1, height-1);
- // 準備數據,隨機獲取4個字符
- // String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
- String words = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a\u53c8\u53ef\u5bb6\u5b66\u53ea\u4ee5\u4e3b\u4f1a\u6837\u5e74\u60f3\u751f\u540c\u8001\u4e2d\u5341\u4ece\u81ea\u9762\u524d\u5934\u9053\u5b83\u540e\u7136\u8d70\u5f88\u50cf\u89c1\u4e24\u7528\u5979\u56fd\u52a8\u8fdb\u6210\u56de\u4ec0\u8fb9\u4f5c\u5bf9\u5f00\u800c\u5df1\u4e9b\u73b0\u5c71\u6c11\u5019\u7ecf\u53d1\u5de5\u5411\u4e8b\u547d\u7ed9\u957f\u6c34\u51e0\u4e49\u4e09\u58f0\u4e8e\u9ad8\u624b\u77e5\u7406\u773c\u5fd7\u70b9\u5fc3\u6218\u4e8c\u95ee\u4f46\u8eab\u65b9\u5b9e\u5403\u505a\u53eb\u5f53\u4f4f\u542c\u9769\u6253\u5462\u771f\u5168\u624d\u56db\u5df2\u6240\u654c\u4e4b\u6700\u5149\u4ea7\u60c5\u8def\u5206\u603b\u6761\u767d\u8bdd\u4e1c\u5e2d\u6b21\u4eb2\u5982\u88ab\u82b1\u53e3\u653e\u513f\u5e38\u6c14\u4e94\u7b2c\u4f7f\u5199\u519b\u5427\u6587\u8fd0\u518d\u679c\u600e\u5b9a\u8bb8\u5feb\u660e\u884c\u56e0\u522b\u98de\u5916\u6811\u7269\u6d3b\u90e8\u95e8\u65e0\u5f80\u8239\u671b\u65b0\u5e26\u961f\u5148\u529b\u5b8c\u5374\u7ad9\u4ee3\u5458\u673a\u66f4\u4e5d\u60a8\u6bcf\u98ce\u7ea7\u8ddf\u7b11\u554a\u5b69\u4e07\u5c11\u76f4\u610f\u591c\u6bd4\u9636\u8fde\u8f66\u91cd\u4fbf\u6597\u9a6c\u54ea\u5316\u592a\u6307\u53d8\u793e\u4f3c\u58eb\u8005\u5e72\u77f3\u6ee1\u65e5\u51b3\u767e\u539f\u62ff\u7fa4\u7a76\u5404\u516d\u672c\u601d\u89e3\u7acb\u6cb3\u6751\u516b\u96be\u65e9\u8bba\u5417\u6839\u5171\u8ba9\u76f8\u7814\u4eca\u5176\u4e66\u5750\u63a5\u5e94\u5173\u4fe1\u89c9\u6b65\u53cd\u5904\u8bb0\u5c06\u5343\u627e\u4e89\u9886\u6216\u5e08\u7ed3\u5757\u8dd1\u8c01\u8349\u8d8a\u5b57\u52a0\u811a\u7d27\u7231\u7b49\u4e60\u9635\u6015\u6708\u9752\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03\u5973\u4efb\u4ef6\u611f\u51c6\u5f20\u56e2\u5c4b\u79bb\u8272\u8138\u7247\u79d1\u5012\u775b\u5229\u4e16\u521a\u4e14\u7531\u9001\u5207\u661f\u5bfc\u665a\u8868\u591f\u6574\u8ba4\u54cd\u96ea\u6d41\u672a\u573a\u8be5\u5e76\u5e95\u6df1\u523b\u5e73\u4f1f\u5fd9\u63d0\u786e\u8fd1\u4eae\u8f7b\u8bb2\u519c\u53e4\u9ed1\u544a\u754c\u62c9\u540d\u5440\u571f\u6e05\u9633\u7167\u529e\u53f2\u6539\u5386\u8f6c\u753b\u9020\u5634\u6b64\u6cbb\u5317\u5fc5\u670d\u96e8\u7a7f\u5185\u8bc6\u9a8c\u4f20\u4e1a\u83dc\u722c\u7761\u5174\u5f62\u91cf\u54b1\u89c2\u82e6\u4f53\u4f17\u901a\u51b2\u5408\u7834\u53cb\u5ea6\u672f\u996d\u516c\u65c1\u623f\u6781\u5357\u67aa\u8bfb\u6c99\u5c81\u7ebf\u91ce\u575a\u7a7a\u6536\u7b97\u81f3\u653f\u57ce\u52b3\u843d\u94b1\u7279\u56f4\u5f1f\u80dc\u6559\u70ed\u5c55\u5305\u6b4c\u7c7b\u6e10\u5f3a\u6570\u4e61\u547c\u6027\u97f3\u7b54\u54e5\u9645\u65e7\u795e\u5ea7\u7ae0\u5e2e\u5566\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";
-
- // 設置顏色
- g.setColor(Color.YELLOW);
- // 設置字體
- g.setFont(new Font("隸書", Font.BOLD, 20));
-
- Random random = new Random();
- int x = 20;
- int y = 20;
- for(int i=0;i<4;i++){
-
- // void rotate(double theta, double x, double y)
- // theta 弧度
- // hudu = jiaodu * Math.PI / 180;
- // 獲取正負30之間的角度
- int jiaodu = random.nextInt(60)-30;
- double hudu = jiaodu * Math.PI / 180;
- g.rotate(hudu, x, y);
- // 獲取下標
- int index = random.nextInt(words.length());
- // 返回指定下標位置的字符,隨機獲取下標
- char ch = words.charAt(index);
- // 寫字符串
- g.drawString(""+ch, x, y);
-
- g.rotate(-hudu, x, y);
- x += 20;
- }
-
- // 設置顏色
- g.setColor(Color.GREEN);
- int x1,x2,y1,y2;
- // 畫干擾線
- for(int i=0;i<4;i++){
- x1 = random.nextInt(width);
- y1 = random.nextInt(height);
- x2 = random.nextInt(width);
- y2 = random.nextInt(height);
- g.drawLine(x1, y1, x2, y2);
- }
-
- // 輸出到客戶端
- ImageIO.write(image, "jpg", response.getOutputStream());
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- public static void main(String[] args) {
- System.out.println("\u9fa5");
- System.out.println("\u9fa4");
- System.out.println("\u9fa3");
- System.out.println("\u9fa2");
- }
-
- }
DownloadServlet.java 簡單實現文件下載(中文名亂碼)
- package cn.itcast.response;
-
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URLEncoder;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class DownloadServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
- String path = getServletContext().getRealPath("/img/外國美女.jpg");
-
-
- String filename = null;
-
- int index = path.lastIndexOf("\\");
- if(index != -1){
- filename = path.substring(index+1);
- }
-
-
-
- String agent = request.getHeader("User-Agent");
-
- byte[] bytes = agent.contains("MSIE") ? filename.getBytes() : filename
- .getBytes("UTF-8");
- filename = new String(bytes, "ISO-8859-1");
-
-
- if(filename != null){
-
- response.setHeader("Content-Disposition", "attachment;filename="+filename);
-
- System.out.println(request.getRemoteAddr());
-
- System.out.println(path);
- InputStream in = new FileInputStream(path);
-
- OutputStream os = response.getOutputStream();
-
- byte [] b = new byte[1024];
- int len = 0;
- while((len = in.read(b)) != -1){
- os.write(b, 0, len);
- }
- in.close();
-
- }
-
-
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
OutServlet.java (處理HttpServletResponse 響應輸出中文的亂碼問題 包括字節和字符)
- package cn.itcast.response;
-
- import java.io.IOException;
- import java.io.OutputStream;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class OutServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- run2(response);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
-
- public void run2(HttpServletResponse response) throws IOException{
-
-
-
-
-
-
- response.setContentType("text/html;charset=UTF-8");
- response.getWriter().write("哈羅臥得");
- }
-
-
- public void run1(HttpServletResponse response) throws IOException{
-
- response.setHeader("Content-Type", "text/html;charset=UTF-8");
-
- OutputStream os = response.getOutputStream();
-
- os.write("哈羅個人".getBytes("UTF-8"));
- }
-
- }
RefreshServlet.java (頁面自動定時跳轉)
- package cn.itcast.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 RefreshServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html;charset=UTF-8");
- response.getWriter().write("<h1>頁面將在5秒後跳轉</h1>");
-
- response.setHeader("refresh", "5;url=/day10/response/login.html");
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
LoginServlet.java
- package cn.itcast.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 LoginServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
-
- String username = request.getParameter("username");
-
- String password = request.getParameter("password");
-
- if("admin".equals(username) && "admin".equals(password)){
-
-
-
- response.sendRedirect("/day10/response/refresh.html");
- }else{
-
-
-
-
-
-
-
- response.sendRedirect("/day10/response/login.html");
- }
-
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
RequestServlet1.java (獲取客戶機內容)
- package cn.itcast.request;
-
- 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 RequestServlet1 extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
- String ip = request.getRemoteAddr();
-
- String method = request.getMethod();
-
- String path = request.getContextPath();
- System.out.println("IP地址:"+ip);
- System.out.println("請求方式:"+method);
- System.out.println("虛擬路徑名稱:"+path);
-
-
- String referer = request.getHeader("referer");
-
- String agent = request.getHeader("user-agent");
-
-
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
RegServlet.java (解決請求亂碼 GET和POST請求亂碼)
- package cn.itcast.request;
-
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.Map;
- import java.util.Set;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class RegServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
-
-
-
-
-
-
- String username = request.getParameter("username");
-
- username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
-
- String password = request.getParameter("password");
-
- String sex = request.getParameter("sex");
-
- String city = request.getParameter("city");
-
- String [] loves = request.getParameterValues("love");
-
- System.out.println("用戶名:"+username);
- System.out.println("密碼:"+password);
- System.out.println("性別:"+sex);
- System.out.println("城市:"+city);
- System.out.println("愛好:"+Arrays.toString(loves));
-
- System.out.println("===================================================================");
-
- Map<String, String []> map = request.getParameterMap();
-
- Set<String> keys = map.keySet();
- for (String key : keys) {
- String [] values = map.get(key);
- System.out.println(Arrays.toString(values));
- }
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
RequestDemo3.java (轉發和重定向)
- package cn.itcast.request;
-
- 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 RequestDemo3 extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
- request.setAttribute("name", "鳳姐");
-
-
-
-
-
- request.getRequestDispatcher("/request4").forward(request, response);
-
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
RequestDemo4.java (測試request域)
- package cn.itcast.request;
-
- 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 RequestDemo4 extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
- String value = (String) request.getAttribute("name");
-
- response.setContentType("text/html;charset=UTF-8");
- response.getWriter().write("訪問到了demo4... "+value);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
使用轉發效果以下:能獲取request域中的值
使用重定向效果以下:是不能獲取到request域中的值的
Login2Servlet.java (使用request域屬性和轉發 實現登陸的錯誤信息提示)
- package cn.itcast.request;
-
- 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 Login2Servlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
-
- String username = request.getParameter("username");
-
- String password = request.getParameter("password");
-
- if("admin".equals(username) && "admin".equals(password)){
-
-
- response.sendRedirect("/day10/response/refresh.html");
- }else{
-
- request.setAttribute("msg", "用戶名或者密碼錯誤!");
-
-
-
- request.getRequestDispatcher("/request/login.jsp").forward(request, response);
- }
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- }
from: http://www.tk4479.net/u013087513/article/details/54601326