Web階段:第十章:Servlet下
HttpServletRequest類
a)HttpServletRequest類有什么作用。
HttpServletRequest類它的作用是。每次只要有請(qǐng)求進(jìn)來 。Tomcat服務(wù)器就會(huì)把請(qǐng)求的協(xié)議內(nèi)容封裝到HttpServletRequest對(duì)象中。
我們主要是從Request類中獲取請(qǐng)求的信息。它表示了請(qǐng)求的全部信息。
b)HttpServletRequest類的常用方法
getRequestURI() 獲取請(qǐng)求請(qǐng)求資源地址
getRequestURL() 獲取請(qǐng)求的絕對(duì)路徑(又叫統(tǒng)一資源定位符)
getRemoteHost() 獲取客戶端的ip地址
localhost做為ip訪問得到0:0:0:0:0:0:0:1
127.0.0.1 做為ip訪問得到 127.0.0.1
getHeader() 獲取請(qǐng)求頭
getParameter() 獲取請(qǐng)求的參數(shù)
getParameterValues() 獲取請(qǐng)求參數(shù)(多個(gè)值)
getMethod() 獲取請(qǐng)求的方式GET或POST
域?qū)ο?br> setAttribute(key, value); 保存數(shù)據(jù)
getAttribute(key); 獲取數(shù)據(jù)
getRequestDispatcher() 獲取請(qǐng)求轉(zhuǎn)發(fā)對(duì)象
getCookies() 獲取Cookie對(duì)象
getSession() 獲取Session對(duì)象
reqeust常用API:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// i.getRequestURI() 獲取請(qǐng)求請(qǐng)求資源地址
System.out.println(request.getRequestURI());
// ii.getRequestURL() 獲取請(qǐng)求的絕對(duì)路徑
System.out.println(request.getRequestURL());
// iii.getRemoteHost() 獲取客戶端的ip地址
System.out.println(request.getRemoteHost());
// iv.getHeader() 獲取請(qǐng)求頭
System.out.println(request.getHeader("User-Agent"));
System.out.println(request.getHeader("Host"));
// vii.getMethod() 獲取請(qǐng)求的方式GET或POST
System.out.println(request.getMethod());
}
c)如何獲取請(qǐng)求參數(shù)(重要)
頁面的表單:
- <body>
- <form action="http://localhost:8080/day07/parameterServlet" method="get">
- 用戶名:<input type="text" name="username" /><br/>
- 密碼:<input type="password" name="password" /><br/>
- 興趣愛好:
- <input type="checkbox" name="hobby" value="cpp">C++
- <input type="checkbox" name="hobby" value="C">C
- <input type="checkbox" name="hobby" value="VB">Visual Basic
- <input type="checkbox" name="hobby" value="js">JavaScript
- <br/>
- <input type="submit" />
- </form>
- </body>
Servlet程序的代碼:
public class ParameterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 獲取請(qǐng)求參數(shù),用戶名信息
String username = request.getParameter("username");
// 獲取請(qǐng)求參數(shù),密碼信息
String password = request.getParameter("password");
// 獲取興趣愛好
String[] hobbies = request.getParameterValues("hobby");
System.out.println("用戶名:" + username);
System.out.println("密碼:" + password);
// Arrays.asList 把數(shù)組轉(zhuǎn)換對(duì)應(yīng)的List集合
if (hobbies != null) {
System.out.println("興趣愛好:" + Arrays.asList(hobbies));
}
}
}
d)GET請(qǐng)求的中文亂碼解決
第一種方案:
1、先把收到的請(qǐng)求參數(shù)以iso-8859-1進(jìn)行編碼
2、再以UTF-8進(jìn)行解碼
// 獲取請(qǐng)求參數(shù),用戶名信息
String username = request.getParameter("username");
// 1、先把收到的請(qǐng)求參數(shù)以iso-8859-1進(jìn)行編碼
byte[] bytes = username.getBytes("iso-8859-1");
// 2、再以UTF-8進(jìn)行解碼
username= new String(bytes, "UTF-8");
第二種方案:
找到Tomcat服務(wù)器的配置文件server.xml配置文件,找到Connector標(biāo)簽,添加URIEncoding=“UTF-8”
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
1
e)POST請(qǐng)求的中文亂碼解決
第一種方案(不推薦使用):
// 1、先把收到的請(qǐng)求參數(shù)以iso-8859-1進(jìn)行編碼
byte[] bytes = username.getBytes("iso-8859-1");
// 2、再以UTF-8進(jìn)行解碼
username= new String(bytes, "UTF-8");
第二種方案(推薦使用):
// 解決post中文亂碼終極方案
// 一定要在調(diào)用getParameter方法之前才有效
request.setCharacterEncoding("UTF-8");
f)請(qǐng)求的轉(zhuǎn)發(fā)(在jsp的時(shí)候使用)
g)Base標(biāo)簽的作用
h)Web中的相對(duì)路徑和絕對(duì)路徑
相對(duì)路徑:
./ 當(dāng)前目錄
…/ 上一級(jí)目錄
資源名 相當(dāng)于./資源名 ./可以省略
絕對(duì)路徑:
http://ip:port/工程名/資源名
在實(shí)際項(xiàng)目開發(fā)中,不允許簡單的使用相對(duì)路徑?。?!
實(shí)際項(xiàng)目開發(fā)中,只能使用絕對(duì)路徑 或 base+相對(duì)
i)web中/斜杠的不同意義
斜杠/ 就是表示一個(gè)絕對(duì)路徑。
/ 在html頁面上,被瀏覽器解析得到是:http://ip:prot/
/ 在服務(wù)器代碼上。表示: http://ip:port/工程名/
在web.xml中配置url-pattern /hello ====>>>> http://ip:port/工程名/hello
請(qǐng)求轉(zhuǎn)發(fā)request.getRequestDispatcher("/a/b/c.html") 表示http://ip:port/工程名/a/b/c.html
servletContext.getRealPath(“/”); =====>>>>>> 表示到http://ip:prot/工程名/
服務(wù)器代碼中的特殊情況:
response.sendRedirect(“/”); =====>>>> 表示到端口號(hào)http://ip:prot/
6.HttpServletResponse類
a)HttpServletResponse類的作用
httpServletResponse類,表示響應(yīng)。所有響應(yīng)的http協(xié)議都可以通過HttpServletResponse類去進(jìn)行設(shè)置。
每次請(qǐng)求進(jìn)來都會(huì)創(chuàng)建一個(gè)Request對(duì)象,也會(huì)創(chuàng)建一個(gè)Response對(duì)象。Tomcat服務(wù)器負(fù)責(zé)創(chuàng)建。
b)兩個(gè)輸出流的說明。
響應(yīng)有兩種流,一種是字符流,一種是字節(jié)流。
response.getOutputStream() 字節(jié)流 文件下載使用
response.getWriter() 字符流 回傳字符串信息(使用頻率最高)
你使用了字節(jié)流,就不能使用字符流,
你使用字符流,就不能使用字節(jié)流。
兩個(gè)流同時(shí)使用就會(huì)出現(xiàn)如下異常:
c)如何往客戶端回傳數(shù)據(jù)
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 往客戶端回傳字符串?dāng)?shù)據(jù)
// 1、先獲取字符流
PrintWriter writer = response.getWriter();
// 2、調(diào)用字符流的write方法輸出字符串
writer.write("this is the content of response!!!");
}
d)響應(yīng)的亂碼解決
第一種方案(不推薦使用):
// 設(shè)置響應(yīng)的字符集為UTF-8,設(shè)置了服務(wù)器支持中文輸出
response.setCharacterEncoding("UTF-8");
// ISO-8859-1 是默認(rèn)的字符集,它不支持中文
System.out.println( response.getCharacterEncoding() );
// 設(shè)置響應(yīng)頭,Content-Type,
// text/html; 表示返回的是html內(nèi)容。
// charset=UTF-8 告訴客戶端使用UTF-8字符集查看
response.setHeader("Content-Type", "text/html; charset=UTF-8");
第二種方案是(推薦使用):
// 同時(shí)設(shè)置服務(wù)器和客戶端都使用UTF-8字符集,
// 并且還設(shè)置了響應(yīng)頭
// 必須在獲取流對(duì)象之前調(diào)用
response.setContentType("text/html; charset=UTF-8");
e)如何設(shè)置響應(yīng)頭和響應(yīng)狀態(tài)碼
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("請(qǐng)求進(jìn)來 了,這是Response1");
// 設(shè)置響應(yīng)狀態(tài)碼302
response.setStatus(302);
// 設(shè)置響應(yīng)頭
response.setHeader("Location", "http://localhost:8080/day07/response2");
}
f)請(qǐng)求重定向
//也是請(qǐng)求重定向(推薦)
response.sendRedirect("http://localhost:8080/day07/response2");
請(qǐng)求重定向的特點(diǎn):
請(qǐng)求轉(zhuǎn)發(fā)和重定向的對(duì)比