瀏覽器能夠直接訪問webContent中的文件css
html
其中的index1.jsp就在WebContent目錄中;java
可是WEB-INF中的文件 沒法經過客戶端(瀏覽器)直接訪問,只能經過請求轉發來訪問web
注意:並非任何的內部跳轉都能訪問WEB-INF;緣由是跳轉有兩種方式:請求轉發、重定向api
jsp <->Servlet瀏覽器
a.將tomcat/lib中的servlet-api.jar加入項目的構建路徑(只加一個)緩存
b.右鍵項目 -> Build Path -> Add library - Server Runtime(加一堆jar)【推薦】tomcat
在servers面板新建一個tomcat實例,再在該實例中部署項目(右鍵-add)安全
注意:通常建議將eclipse中的tomcat與本地tomcat保持一致;服務器
將eclipse中的tomcat設置爲託管模式:【第一次】建立tomcat實例以後,雙擊,選擇Server Location的第二個
設計jsp文件的編碼(jsp文件中的pageEncodeing屬性):jsp -> java
設置瀏覽器讀取jsp文件的編碼(jsp文件中content屬性)
通常將上述設置成一致的編碼,推薦使用UTF-8
i.將整個Eclipse中的文件統一設置(之後的jsp編碼都會utf-8)【推薦】
ii.設置某一項目(右鍵文件-properties)
iii.設置單獨文件
HTML java代碼(腳本Scriptlet) 指令 註釋
i.
1 <% 2 局部變量、java語句 3 %>
ii.
1 <%! 2 全局變量、定義方法 3 %>
iii.
1 <%= 2 輸出表達式 3 %>
修改web.xml、配置文件、java須要重啓tomcat服務,可是若是修改Jsp/html/js/css代碼不須要重啓
注意:out.println()不能回車;要想回車:<br\>
,即out.print() <%= %>
能夠直接解析html代碼
page指令
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" import="java.util.Date"%>
屬性:
language:jsp頁面使用的腳本語言
import:導入類
pageEnconding:jsp文件自身編碼 jsp -> java
contentType:瀏覽器解析自身的編碼
html註釋
1 <!--能夠被客戶經過瀏覽器查看源碼所觀察到-->
java註釋
1 // 2 /*...*/
jsp註釋
1 <%-- --%>
(自帶,無需new也能使用的對象)
String getParameter(String name); 根據請求的字段名key(input標籤的name屬性),返回字段值value(input標籤的value屬性)
String[] getParameterValues(String name); 根據請求的字段名key,返回多個字段值value(checkbox)
void setCharacterEncoding("編碼格式utf-8"); 設置post請求編碼(tomcat7之前默認iso-8859-1,tomcat8之後改爲了utf-8)
getRequestDispartcher("b.jsp").forward(request,response); 請求轉發的方式跳轉頁面 A -> B
ServletContext getServerContext(); 獲取項目的ServletContext對象
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="show.jsp"> 11 用戶名:<input type="text" name="uname"/><br/> 12 密 碼:<input type="password" name="upwd"/><br/> 13 年 齡:<input type="text" name="uage"/><br/> 14 愛 好:<br/> 15 <input type="checkbox" name="uhobbies" value="足球"/>足球 16 <input type="checkbox" name="uhobbies" value="籃球"/>籃球 17 <input type="checkbox" name="uhobbies" value="乒乓球"/>乒乓球<br/> 18 <input type="submit" value="註冊"> 19 </form> 20 </body> 21 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 //設置編碼 12 request.setCharacterEncoding("utf-8"); 13 String name = request.getParameter("uname"); 14 int age = Integer.parseInt(request.getParameter("uage")); 15 String pwd = request.getParameter("upwd"); 16 17 String[] hobbies = request.getParameterValues("uhobbies"); 18 %> 19 註冊成功,信息以下:<br/> 20 姓名:<%=name %><br/> 21 年齡:<%=age %><br/> 22 密碼:<%=pwd %><br/> 23 <% 24 if(hobbies !=null){ //控制沒有愛好則不顯示 25 out.print("愛好:"); 26 for(String hobby:hobbies){ 27 out.print(hobby + " "); 28 } 29 } 30 %> 31 </body> 32 </html>
鏈接/文件?參數名1=參數值1&參數名2=參數值2&參數名3=參數值3
get提交方式:method="get"和地址欄、超連接(<a href="xx">
)請求方式默認都屬於get提交方式
a.get方式在地址欄顯示請求信息(可是地址欄可以容納的信息有限,4-5KB;若是請求數據存在大文件)
b.文件上傳操做,必須是post【推薦】
void addCookie(Cookie cookie); 服務端向客戶端增長cookie對象
void sendRedirect(String location) throws IOException; 頁面跳轉的一種方式(重定向)
void setContentType(String type);設置服務端響應的代碼(設置服務端的contentType類型)
login.jsp -> check.jsp -> success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="check.jsp" method="post"> 11 用戶名:<input type="text" name="uname"><br/> 12 密碼:<input type="password" name="upwd"><br/> 13 <input type="submit" value="登陸"><br/> 14 </form> 15 </body> 16 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 request.setCharacterEncoding("utf-8"); 12 String name = request.getParameter("uname"); 13 String pwd = request.getParameter("upwd"); 14 if(name.equals("zs")&&pwd.equals("abc")){ 15 //經過重定向跳轉,結果致使數據丟失 16 //response.sendRedirect("success.jsp"); 17 //請求轉發跳轉:能夠獲取到數據,而且地址欄沒有改變(仍然保留轉發時的頁面) 18 request.getRequestDispatcher("success.jsp").forward(request, response); 19 }else{ 20 //登陸失敗 21 out.print("用戶名或密碼錯誤!"); 22 } 23 %> 24 </body> 25 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 登錄成功!<br/> 11 歡迎您: 12 <% 13 String name = request.getParameter("uname"); 14 out.print(name); 15 %> 16 </body> 17 </html>
請求轉發 | 重定向 | |
---|---|---|
地址欄是否改變 | 不變(check.jsp) | 改變(success.jsp) |
是否保留第一次請求時的數據 | 保留 | 不保留 --4種範圍對象 |
請求的次數 | 1 | 2 |
跳轉發生的位置 | 服務端 | 客戶端發起的第二次跳轉 |
轉發、重定向:
轉發:張三(客戶端) -> 【服務窗口A(服務器) -> 服務窗口B】
重定向:張三(客戶端) -> 服務窗口A(服務端) -> 去找B
張三(客戶端) ->服務窗口B(服務端) -> 結束
Cookie是由服務端生成的,再發給客戶端保存
至關於本地緩存的做用:客戶端(hello.jsp)->服務端(hello.mp4;zs/abc)
Cookie:key=value
javax.servlet.http.Cookie
public Cookie(String name,String value)
String getName() 獲取name
String getValue() 獲取value
void setMaxAge(int expiry); 最大有效期(s)
服務器準備Cookie:
response.addCookie(Cookie cookie)
頁面跳轉(轉發、重定向)
客戶端獲取Cookie:request.getCookies();
a.服務端增長Cookie:response對象;客戶端獲取對象:request對象
b.不能直接獲取某一個單獨對象,只能一次性將所有的Cookie拿到
經過F12能夠發現,除了本身設置的Cookie對象外,還有一個name爲JSESSIONID的cookie
建議cookie中只保存英文、數字,不然須要進行編碼、解碼處理
使用Cookie實現記住用戶名操做
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <%! 11 String uname; 12 %> 13 <% 14 Cookie[] cookies = request.getCookies(); 15 for(Cookie cookie:cookies){ 16 if(cookie.getName().equals("uname")){ 17 uname = cookie.getValue(); 18 } 19 } 20 %> 21 <form action="check.jsp" method="post"> 22 用戶名:<input type="text" name="uname" value="<%=(uname==null?"":uname)%>"><br/> 23 密碼:<input type="password" name="upwd"><br/> 24 <input type="submit" value="登陸"><br/> 25 </form> 26 </body> 27 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 request.setCharacterEncoding("utf-8"); 12 String name = request.getParameter("uname"); 13 String pwd = request.getParameter("upwd"); 14 15 //將用戶名加入到Cookie中 16 //Cookie cookie = new Cookie("key","value"); 17 Cookie cookie = new Cookie("uname",name); 18 response.addCookie(cookie); 19 20 response.sendRedirect("result.jsp"); 21 22 /* if(name.equals("zs")&&pwd.equals("abc")){ 23 //經過重定向跳轉,結果致使數據丟失 24 //response.sendRedirect("success.jsp"); 25 //請求轉發跳轉:能夠獲取到數據,而且地址欄沒有改變(仍然保留轉發時的頁面) 26 request.getRequestDispatcher("success.jsp").forward(request, response); 27 }else{ 28 //登陸失敗 29 out.print("用戶名或密碼錯誤!"); 30 } */ 31 %> 32 </body> 33 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 僅供測試使用 11 </body> 12 </html>
pageContext(後面講)
application(後面講)
config(後面講)
page(後面講)
exception(後面講)
若是出現亂碼的解決方法:
new String(舊編碼,新編碼)
1 name = new String(name.getBytes("iso-8859-1"),"utf-8")
建議使用tomcat時,首先在server.xml中統一get方式的編碼
1 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/> 2 <!--添加URIEncoding="UTF-8"之後全部的get方式都是utf-8-->
1 <% 2 //設置編碼 3 request.setCharacterEncoding("utf-8"); 4 %>
tomcat7(iso-8859-1)
tomcat8(utf-8)