北京行——JSP入門與Servlet精通

Servlet技術 用來動態生成 網頁數據資源
Servlet生成HTML 頁面數據時,所有內容都是通過 response.getWriter response.getOutputStream 向瀏覽器輸出的

<html>
<head>
</head>
<body>
Hello
</body>
</html>

用Servlet 輸出流打印網頁信息
response.getWriter().print("<html>");

Servlet生成網頁缺點
1、通過print輸出網頁 開發很不方便
2、美工人員經常對頁面進行CSS修飾,美工人員在java代碼中調試CSS
3、無法使用開發工具對頁面代碼進行調試 JS CSS

98前后 主流網頁設計技術 ASP PHP , sun推出Servlet,sun參考ASP語法推出JSP
* 什么是 JSP ? JSP 和Servlet技術一樣,都是動態網頁開發技術 ?Java Server Page

* JSP 和 Servlet 關系? JSP在運行時,需要先翻譯為Servlet才能運行 ------ JSP 就是 Servlet ,這個工作是由編譯器給搞定了?
Servlet2.5 版本 ---- JavaEE 5.0 ---- JSP 版本2.1

編寫第一個JSP程序
1、JSP位于WebRoot下 --- 不能放入WEB-INF(不能訪問)
2、修改JSP默認編碼 window --- preferences --- JSP 修改編碼 utf-8
3、修改JSP 默認編輯器 window --- preferences --- general ---- editor ---- File Associations 將 JSP編輯器改為 JSP Editor
4、在WebRoot下 新建 JSP ---- hello.jsp
5、寫JSP過程和編寫HTML一樣,可以通過<%%> 嵌入java代碼

* 有人比喻 Servlet:嵌入HTML的java文件 ; JSP :嵌入Java的HTML文件

JSP運行原理:
1、客戶端訪問hello.jsp
2、服務器讀取hello.jsp內容到內存
3、服務器根據hello.jsp內容生成Servlet程序 ----保存在哪? tomcat/work
4、Servlet編譯運行

JSP翻譯Servlet 固定包名 :org.apache.jsp
hello.jsp ---- hello_jsp.java

JSP中 HTML代碼 <%%> 代碼都會被翻譯Servlet 中 _jspService
翻譯規則:
1、JSP 中 HTML 翻譯 out.write
2. JSP中java代碼 不會翻譯

JSP中腳本元素 中都是使用%
1、聲明 <%! %> --- 定義內容將會被翻譯Servlet類 成員變量和方法
2、表達式 <%= %> ---- 用于向頁面輸出內容 等價于 out.print()
3、代碼塊<% %> --- 在<%%> 之間編寫任何java代碼
* 代碼塊可以和html嵌套使用

開發網頁程序,使用Servlet? 使用JSP呢?
Java代碼多 --- Servlet
HTML代碼多 ---- JSP

JSP運行時 總要翻譯Servlet ,效率很低 ?
* 只有第一次訪問JSP 進行翻譯 ,以后訪問,如果沒有修改 JSP 不會重新翻譯

EL 全名為Expression Language ---- 表達式語言
語法:${標識符}
${applicationScope.name } 等價于 <%=getServletContext().getAttribute("name") %>
${requestScope.address } 等價于 <%=request.getAttribute("address") %>

JSTL (JSP Standard Taglib Liberary) --- JSP 標準標簽庫
JSTL干嘛用的? 簡化頁面<%%> 與 HTML嵌套寫法 ----- 簡化JSP開發

今天學習目標: <c:forEach> <c:if> ----- 用來替換頁面中for循環 和 if條件判斷

在頁面內使用JSTL 簡化 if 和 for
1、導入jstl的jar包 --- MyEclipse中存在 javaee5 類庫 ---存在 jstl1.2.jar
2、在頁面最上方 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
* 定義了很多標簽,為標簽指定名稱空間 uri ,在使用標簽前 引用名稱空間
JSTL+EL 簡化 <%%> 腳本代碼

------------------------------------------------------------------------------------------------------
什么是會話? 用戶打開瀏覽器,訪問站點,連續進行多次操作,關閉瀏覽器 ,整個過程稱為會話 。

管理HTTP協議會話狀態 :Cookie 和Session
Cookie:將用戶相關數據,保存客戶端 , 用戶每次訪問服務器自動攜帶cookie數據 。
Session : 將用戶相關數據 保存服務器端,為每個客戶端生成一個獨立Session數據對象,通過對象唯一編號,區分哪個瀏覽器對應 哪個Session

Cookie快速入門 案例:上次訪問時間
1、通過服務器向客戶端寫cookie
Cookie cookie = new Cookie(name,value);
response.addCookie(cookie);
* 在HTTP協議響應頭信息中 Set-Cookie: last=1339556457609

2、當客戶端存在cookie之后,以后每次請求自動攜帶 HTTP協議請求頭信息 Cookie: last=1339556456859
服務器端獲得需要cookie數據
Cookie[] cookies = request.getCookies(); ---- 獲得客戶端所有cookie
if(cookies==null){} 判斷cookie是否存在

遍歷cookie獲得需要信息
for (Cookie cookie : cookies) {
// 獲得每個cookie
if (cookie.getName().equals("last")) {
// 找到了需要cookie
}
}

代碼附上:

public class CookieServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Cookie[] cookies = request.getCookies();response.setContentType("text/html;charset=utf-8");if (cookies == null) {long now = System.currentTimeMillis();response.addCookie(new Cookie("lastTime", now + ""));response.getWriter().write("歡迎首次來購物!");} else {for (Cookie cookie : cookies) {if (cookie.getName().equals("lastTime")) {String lastTime = cookie.getValue();System.out.println("第二次");Long time = Long.parseLong(lastTime);System.out.println(time);Date date = new Date(time);// 沒想到在這個時間上費了很大的力氣SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");response.getWriter().write("上次訪問時間是:" + dateFormat.format(date));}}}long now = System.currentTimeMillis();response.addCookie(new Cookie("lastTime", now + ""));}// 添加了這個后會更新上次的訪問時間

?

?

?

CookieAPI 詳解
1、將cookie寫回客戶端 response.addCookie(cookie);
2、讀取請求中 cookie信息 request.getCookies
3、Cookie對象創建 new Cookie(name,value )
* cookie的name 不允許改變 ----- getName 、getValue 、setValue

4、什么是會話cookie ,什么是持久cookie ?
cookie信息默認情況 保存在瀏覽器內存中 ------ 會話cookie
會話cookie 會在關閉瀏覽器時 清除

持久Cookie,cookie數據保存客戶端硬盤上
通過setMaxAge 設置Cookie為持久Cookie
* 在JavaAPI 中所有與時間相關參數,int 類型 單位秒, long類型 單位毫秒

5、訪問cookie有效路徑path
默認情況下,生成cookie時,產生默認有效訪問路徑 (默認生成cookie程序路徑)
http://localhost/day07/lastvisit --- 生成cookie --- path 默認值: /day07
http://localhost/day07/servlet/path ---- 生成cookie ---- path 默認值:/day07/servlet

第二次訪問程序攜帶cookie信息,如果訪問路徑與path不一致,不會攜帶cookie 信息
company cookie : path --- /day07/serlvet
last cookie : path --- /day07

訪問 :http://localhost/day07/servlet/path ---- 同時滿足/day07/serlvet、/day07 攜帶 company 和 last兩個cookie信息
訪問 :http://localhost/day07/lastvisit ---- 滿足 /day07 不滿足/day07/serlvet 攜帶 last 一個cookie 信息
* 以后程序開發,盡量設置path ---- setPath方法 ? ?主要是為了統一,好管理 這是我認為的 ?主要是為了方便管理cookie在哪里,什么時候能全部用到

6、第一方cookie 和 第三方cookie
通過setDomain 設置cookie 有效域名
訪問google時,生成cookie過程中 cookie.setDomain(".baidu.com") ---- 生成百度cookie ------ 第三方cookie
* 第三方cookie 屬于不安全 ----- 一般瀏覽器不接受第三方cookie

訪問google時,生成cookie,cookie.setDomain(.google.con) ---- 生成google的cookie ------ 第一方cookie

案例:刪除上次訪問時間
原理:設置cookie MaxAge為 0
* 刪除cookie時 必須設置path 與cookie的 path一致


案例:商品瀏覽記錄 ---- 可以通過Cookie實現

代碼附上:

Goods.jsp

<body>
<h1>請選擇你想購買的商品</h1><a href="/day7/BuyHistory?id=1">洗衣機</a><a href="/day7/BuyHistory?id=2">冰箱</a><a href="/day7/BuyHistory?id=3">茅臺</a><a href="/day7/BuyHistory?id=4">手機</a><a href="/day7/BuyHistory?id=5">電腦</a><a href="/day7/BuyHistory?id=6">電視</a><br><h1>你上次瀏覽的商品是:</h1><br><a href="/day7/clearServlet">清除瀏覽記錄</a><br>
<%String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };Cookie[] cookies = request.getCookies();Cookie cookie = CookieUntil.findCookie(cookies,"history");String[] idsArr = cookie.getValue().split(",");for(String string :idsArr){out.print(goods[Integer.parseInt(string)-1]+"<br>");}
%>
</body>

Buyhistory.java

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };String id = request.getParameter("id");response.getWriter().write("你現在正在訪問的商品是:" + goods[Integer.parseInt(id) - 1]);response.getWriter().write("<a href=\"/day7/Goods.jsp\">點擊返回首頁</a>");Cookie[] cookies = request.getCookies();Cookie cookie = CookieUntil.findCookie(cookies, "history");if (cookie == null) {cookie = new Cookie("history", id);cookie.setMaxAge(60 * 60 * 60);cookie.setPath("/day7");response.addCookie(cookie);} else {String[] idsArr = cookie.getValue().split(",");String str = null;for (String string : idsArr) {if (string.equals(id)) {// 比較字符串一定要用equals方法return;} else {str = cookie.getValue() + "," + id;}}cookie.setValue(str);cookie.setMaxAge(60 * 60 * 60);cookie.setPath("/day7");response.addCookie(cookie);}}

CookieUntils.java

public class CookieUntil {public static Cookie findCookie(Cookie[] cookies, String name) {if (cookies == null) {return null;} else {for (Cookie cookie : cookies) {if (cookie.getName().equals(name)) {return cookie;}}}return null;}
}

ClearServlet.java

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Cookie cookie = new Cookie("history", "");cookie.setMaxAge(-1);cookie.setPath("/day7");response.addCookie(cookie);}

?

?

-----------------------------------------------------------------------------------------------------------------------

Session 是服務器端會話管理技術,服務器會為每個瀏覽器創建一個單獨Session對象,保存該瀏覽器(會話)操作相關信息。
與Cookie區別:cookie是保存在客戶端,Session保存在服務器端

為什么有Cookie技術?還需要Session ?
Cookie 存在客戶端 ----存在安全問題
Session將數據存在服務器,數據更加安全
* Session將數據保存在服務器端,占用服務器內存資源 ,Cookie不會占用服務器資源
例如:京東購物車信息 保存Cookie 、淘寶購物車信息保存Session

Session共享
實驗:用IE6 向Session保存一個數據,用另一個IE6讀取數據?

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession httpSession = request.getSession();httpSession.setAttribute("name", "kongbin");response.setContentType("text/html;charset=utf-8");response.getWriter().write("已經寫入到了Session");}
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");response.getWriter().print(request.getSession().getAttribute("name"));}

?


用第一個IE6保存session數據,當前瀏覽器可以獲得,但是第二個IE6 無法獲得第一個瀏覽器保存Session 數據

IE8以上 或 火狐瀏覽器:當打開多個瀏覽器窗口,之間Session共享

原因:IE6 cookie 中保存jsessionId 默認會話級別 ;IE8或者火狐 保存cookie中 jsessionId 默認持久cookie

問題:如何讓多個IE6 共享同一個Session ---- 將session id 持久化

問題:如果客戶端關閉瀏覽器,是否就刪除了Session ?
沒有,Session保存在服務器端

問題:IE6 保存Session,關閉瀏覽器,再次打開,數據丟失了?
IE6默認jsessionId保存會話Cookie中,關閉瀏覽器,會話cookie就會被刪除,客戶端丟失jsessionid 無法找到服務器對應Session對象
* 服務器Session對象還存在

Cookie cookie = new Cookie("JSESSIONID", httpSession.getId());cookie.setMaxAge(60 * 60);cookie.setPath("/day7");

?

?

Session購物車案例
1、商品列表,用戶點擊列表中商品,將商品添加購物車
2、查看購物車中商品,以及商品購買件數

product.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>
<h1>請選擇你想購買的商品</h1><a href="/day7/shoppingcart?id=1">洗衣機</a><a href="/day7/shoppingcart?id=2">冰箱</a><a href="/day7/shoppingcart?id=3">茅臺</a><a href="/day7/shoppingcart?id=4">手機</a><a href="/day7/shoppingcart?id=5">電腦</a><a href="/day7/shoppingcart?id=6">電視</a><br><h1>你上次瀏覽的商品是:</h1><br><a href="/day7/clearServlet">查看購物車</a></body>
</html>

ShopingCart.java

package cn.binbin.com;import java.io.IOException;
import java.util.HashMap;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;public class ShoppingCart extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };String id = request.getParameter("id");HttpSession httpSession = request.getSession();Map<String, Integer> map = (Map<String, Integer>) httpSession.getAttribute("map");// 這個地方是個重點,這個地方用到了一個比較好的思想if (map == null) {map = new HashMap<String, Integer>();map.put(id, 1);} else {if (map.containsKey(id)) {// 這個地方是添加的判斷map.put(id, map.get(id) + 1);} else {map.put(id, 1);// 第一次老是出現一件商品,這就是沒有添加的緣故
            }}httpSession.setAttribute("map", map);// 忘記了寫回了response.setContentType("text/html;charset=utf-8");response.getWriter().write("你購買的商品是:   " + goods[Integer.parseInt(id) - 1] + "    ");response.getWriter().write("<a href = '/day7/cart.jsp'>" + "    查看購物車" + "</a>");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}

car.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<!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>
<%  String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };HashMap <String ,Integer> map = (HashMap<String,Integer>)request.getSession().getAttribute("map");for(String name :map.keySet()){out.print(goods[Integer.parseInt(name)-1]);out.println(map.get(name)+"<br>");}out.print("<a href='/day7/product.jsp'>"+"返回購物界面"+"</a>");
%>
</body>
</html>

?

?

禁用Cookie后,Session還能否使用?
可以,可以對url進行重寫,原理在url;jsessionid=xxx ---- 在程序中可以調用response.encodeURL進行URL重寫
* 如果服務器進行URL重寫,所有路徑都必須重寫
* 不要讓用戶禁用cookie

Cookie生命周期
Cookie 對象何時創建,何時銷毀
創建 : Cookie cookie = new Cookie(name,value) ; response.addCookie(cookie);
銷毀 : 會話cookie會在瀏覽器關閉時銷毀,持久cookie會在cookie過期(MaxAge)后銷毀

Session生命周期
創建:request.getSession() 創建Session
銷毀:三種 1、服務器關閉時銷毀 2、session過期時銷毀 3、手動調用session.invalidate

設置session過期時間
1、配置web.xml
<session-config>單位是分鐘:連續30分鐘不使用session
<session-timeout>30</session-timeout>
</session-config>
2、調用session對象 setMaxInactiveInterval(int interval) 單位是秒
HttpSession session = request.getSession();
session.setMaxInactiveInterval(60*60); 設置session過期時間1小時

上述是經典面試題

?

案例:Session經常用來完成系統權限和認證功能
權限:在用戶身份認證后,根據權限知道 --- 你能做什么
認證:用戶登陸 --- 你是誰

將登陸用戶信息保存到Session 有什么作用 ? ---- 如果session中沒有用戶信息 未登陸


案例:通過session實現一次性驗證碼
* 驗證碼從session獲取后,馬上刪除 ---- 驗證碼只能使用一次

CheckImg.java

package cn.binbin.com;import java.awt.Color;
import java.awt.Font;
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;
import javax.servlet.http.HttpSession;public class CheckImg extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 1、在內存中緩沖一張圖片int width = 120;int height = 30;BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);// 2、設置背景色Graphics2D graphics = (Graphics2D) image.getGraphics();graphics.setColor(Color.YELLOW);graphics.fillRect(0, 0, width, height);// 3、畫邊框
        graphics.setColor(Color.BLUE);graphics.drawRect(0, 0, width - 1, height - 1);// 5、開始找字庫了String content = "ASDFGHJKJKLQWERTYUIOZXCVBNM1234567890";// 6、設置字體graphics.setFont(new Font("宋體", Font.BOLD, 20));// 如果驗證碼是中文,必須用中文的字庫Random random = new Random();int x = 10;int y = 20;StringBuffer str = new StringBuffer("");for (int i = 0; i < 4; i++) {int index = random.nextInt(content.length());char c = content.charAt(index);str.append(c);double angle = random.nextInt(60) - 30;// 這個地方是因為randomo不能生產負數double theta = angle / 180 * Math.PI;graphics.rotate(theta, x, y);graphics.drawString(c + "", x, y);graphics.rotate(-theta, x, y);x += 20;}String checkImg = str.toString();HttpSession httpSession = request.getSession();httpSession.setAttribute("checkImg", checkImg);// 7、繪制干擾線int x1, x2, y1, y2;graphics.setColor(Color.LIGHT_GRAY);for (int i = 0; i < 10; i++) {x1 = random.nextInt(width);x2 = random.nextInt(width);y1 = random.nextInt(height);y2 = random.nextInt(height);graphics.drawLine(x1, y1, x2, y2);}// 7、添加旋轉,用的是graphic2D來畫// 4、把內存中的圖片輸出到瀏覽器ImageIO.write(image, "jpg", response.getOutputStream());}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}

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>
<script type="text/javascript">function change(){document.getElementById("img").src="/day7/CheckImg";}
</script>
</head>
<body>
<h2 style="color: red">${requestScope.msg}</h2>
<form action="/day7/LoginServlet">姓名:<input type="text" name="username">密碼:<input type="password" name = "password">驗證碼:<input type="text" name = "checkimg"><img src="/day7/CheckImg" οnclick="change()"id = "img" style="cursor: pointer"><input type="submit" value="提交"> 
</form>
</body>
</html>

LoginServlet.java

package cn.binbin.com;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession httpSession = request.getSession();String name = request.getParameter("username");String password = request.getParameter("password");String checkImg = request.getParameter("checkimg");String checkImg2 = (String) httpSession.getAttribute("checkImg");httpSession.removeAttribute("checkImg");// 這個地方的銷毀是為了進行一次驗證// 這個地方驗證是有先后順序的if (checkImg.equals(checkImg2)) {if (name.equals("kongbin") && password.equals("kongbin")) {httpSession.setAttribute("name", name);response.sendRedirect("/day7/welcome.jsp");} else {request.setAttribute("msg", "用戶名或密碼錯誤");request.getRequestDispatcher("/login.jsp").forward(request,response);}} else {request.setAttribute("msg", "驗證碼錯誤");request.getRequestDispatcher("/login.jsp").forward(request,response);}// if ((name.equals("kongbin") && password.equals("kongbin"))// && (checkImg.equals(checkImg2))) {// httpSession.setAttribute("name", name);// response.sendRedirect("/day7/welcome.jsp");// } else {// response.sendRedirect("/day7/login.jsp");// }
}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}

Welcome.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>
<h1>歡迎回來<%=request.getSession().getAttribute("name") %></h1>
</body>
</html>

?

?

---------------------------------------------------------------------------------------
Servlet 三種數據范圍
ServletContext
HttpServletRequest
HttpSession

三種數據范圍,每個對象各自維護類似map集合結構,具備相同幾個方法
setAttribute 存入一個屬性
getAttribute 取出一個屬性
removeAttribute 移除一個屬性

三種數據范圍對象 在哪些情況使用?
ServletContext 服務器啟動時創建,服務器關閉銷毀 所有Servlet共享 ---- 保存一些全局數據
例如:數據庫連接池、工程配置屬性、配置文件內容 ----- 一般不建議使用,服務器占用內存過多

HttpSession 在request.getSession時創建,在三種情況下銷毀 ----- 保存與用戶相關數據
例如:系統登陸信息、購物車數據

HttpServletRequest 在客戶端發起請求時,服務器創建對象,在響應結束時 對象銷毀 ----- 保存Servlet向JSP傳輸數據信息
例如:執行某個操作,Servlet將操作結果傳遞JSP (Servlet將登陸錯誤信息傳遞JSP )、Servlet將數據庫查詢結果傳遞JSP

用request的時候是比較多的

?

以上三種數據范圍 ServletContext > HttpSession > HttpServletRequest
* 宗旨 優先使用生命周期短的,降低服務器的壓力


總結 :
day5
1、編寫一個Servlet --- HelloWorld response.getWriter().print()
2、設置response編碼集,向瀏覽器輸入中文信息 response.setContentType("text/html;charset=utf-8");
3、將九九乘法表打印瀏覽器頁面 ----- 添加for循環
4、編寫頁面讓用戶輸入一個數字,在Servlet根據用戶輸入數字 打印乘法表
5、用戶輸入一段字母內容,在瀏覽器上輸入 每個字符出現幾次
6、網站訪問次數 (選做)
7、讀取web工程中一個文件 (getServletContext().getRealPath())


理論:
1、Servlet生命周期
2、ServletConfig和ServletContext
3、url-pattern 三種寫法

day6
1、重定向 ---- response.setStatus response.setHeader
2、自動跳轉 ---- refresh
3、禁用緩存 ---- 三個頭信息
4、文件下載 (復雜值得做)
5、驗證碼程序 整理一下 (不用重寫)
6、獲得當前訪問資源路徑 request.getRequestURI().substring(request.getContextPath().length());
7、獲得客戶端 IP request.getRemoteAddr()
8、防盜鏈 request.getHeader(referer)
9、獲得請求參數 亂碼問題 post get 亂碼解決 (昨天重點**** )

理論:
1、200、302 、304 、404 、500 意思
2、refrer refresh Content-Type Content-Disposition 禁止緩存三個頭 ----- 含義
3、get和post請求方式區別
4、亂碼發生原因 (掌握)

day7
1、記錄上次訪問時間 cookie
2、商品瀏覽記錄 cookie
3、商品購物車 session
4、登陸一次性驗證碼 session

理論:
1、cookie和session區別
2、會話cookie 和持久cookie
3、第一方cookie和第三方cookie
4、關閉瀏覽器是否清除cookie ?
5、持久cookie刪除
6、關閉瀏覽器,重新打開瀏覽器session還能繼續使用 ?原理 ?
7、瀏覽器禁用cookie,session還能否使用
8、session銷毀三種情況
9、一次性驗證碼原理
10、Servlet三種數據范圍區別

轉載于:https://www.cnblogs.com/kongbin/articles/3031202.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376724.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376724.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376724.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

可以參考一波&#xff1a;https://stackoverflow.com/questions/16573332/jsondecodeerror-expecting-value-line-1-column-1-char-0 1、json格式不對引起的錯誤 加上if json_rep.content:判空操作 json_rep requests.post(url monitor_url, headers monitor_header,json …

WINDOWS系統Eclipse+NDK+Android + OpenCv

WINDOWS系統EclipseNDKAndroid OpenCv 參考文檔博客 1 NDK環境搭建 http://jingyan.baidu.com/article/5d6edee22d908799eadeec9f.html 2 官方文檔 Android.mk與Application.mk如何編寫&#xff0c;OpenCV庫如何調用 http://docs.opencv.org/trunk/doc/tutorials/introduction…

ural 1910. Titan Ruins: Hidden Entrance(Titan Ruins系列題目)

這是Titan Ruins系列第一道題&#xff0c;以后慢慢更新。 赤裸裸滴閱讀理解題&#xff0c;大意就是找到三個連在一起的數&#xff0c;使其之和最大&#xff0c;輸出的第一個數是這三個數的和&#xff0c;第二個數是中間那個數所在的位置。水題一道&#xff0c;很簡單。 1 #incl…

python OSError: [Errno 24] Too many open files | HTTPConnectionPool(host=‘‘, port=80): Max retries e

對于問題&#xff1a;python OSError: [Errno 24] Too many open files 原因:超出了進程同一時間最多可開啟的文件數. 解決方案P: 使用ulimit -n查看進程同一時間最多可開啟的文件數 mac默認是256&#xff0c;linux是1024 修改 sudo vim /etc/security/limits.conf 這個文件的最…

Android 之視頻監控

Android 視頻監控已經有示例了&#xff0c;如http://www.open-open.com/lib/view/open1346400423609.html完全可以實現簡單的監控功能。但是&#xff0c;如果想要在手機上監控另外一個手機就需要做一些改動了。 其中&#xff0c;手機A實現的功能和上文中的一樣&#xff0c;主要…

Max retries exceeded with url 解決方案

目錄問題解決方案keep alive 與close使用場景問題解決方案 在上一篇問題解決中&#xff1a;python OSError: [Errno 24] Too many open files | HTTPConnectionPool(host‘‘, port80): Max retries e 有提到修改本地進程最大文件數來避免Max retries exceeded with url 報錯&…

正確理解JavaScript

過去幾年我注意到技術圈一個很奇怪的現象&#xff0c;有太多程序員將那些他們只是有過非常淺顯的了解&#xff0c; 但其實根本就不懂的技術寫到他們的簡歷中&#xff0c;這個現象幾乎每種語言都有&#xff0c;但這其中最嚴重的就要數javascript了。 你不知道你不懂 出現這種狀況…

醫療機構遠程視頻監控集中管理,貝銳蒲公英提供一站式解決方案

上海某企業專業致力于醫療軟件、家居智能化研發、設計、銷售、集成及實施&#xff0c;企業主營業務之一為醫療軟件&#xff0c;涉及PACS/RIS/WEB/HIS、示教系統等方面的醫院信息化建設。 在實際應用、部署過程中&#xff0c;需要實現各地區分院與總院間的數據庫互相訪問、視頻數…

py腳本:linux系統下定時清理文件

linux清空文件內容的三種方法&#xff1a; 1.使用vi/vim命令打開文件后&#xff0c;輸入"%d"清空&#xff0c;后保存即可。但當文件內容較大時&#xff0c;處理較慢&#xff0c;命令如下&#xff1a; vim file_name :%d :wq2.使用cat命令情況&#xff0c;命令如下&a…

Process類:啟動和停止本地系統進程

Process.Start 方法 (String, String) 通過指定應用程序的名稱和一組命令行參數來啟動一個進程資源。&#xff08;并將該資源與新的 Process 組件相關聯&#xff09; 如果沒有啟動資源&#xff0c;則返回null Process.Start("d:\\0.jpg"); Process.StartInfo 屬性 獲…

NYOJ2括號配對問題

括號配對是最基本的棧的問題&#xff0c;它是棧入門的經典題目&#xff0c;思路是&#xff0c;如果是左括號直接進棧&#xff0c;如果是右括號&#xff0c;這時就要比較棧頂的元素與他是否匹配&#xff0c;如果匹配則出棧&#xff0c;否則進棧&#xff0c;下面是代碼的實現&…

SVC編碼簡單了解

基本概念 可伸縮視頻編碼 SVC&#xff08;Scalable Video Coding&#xff09; 作為 H.264 標準的一個擴展,可用于生成不同幀率、分辨率和質量等可分層的視頻流。 基本層編碼最低層的時域、空域和質量流&#xff1b; 增強層以基本層作為起始點&#xff0c;對附加信息進行補充&a…

程序集系統重構機房收費系統——起步

這段間時一直在查找程序集系統之類的問題,在現正好有機會和大家同享一下. 開始機房收費系統重構也半個月過去了。但看自己的系統連一個簡略的登錄還沒有實現。自己不免有些羞愧了。近來自己的習學態狀總的說來還是不錯的。但是由于自己對來原的識知把握的欠好所以致導了自己不知…

ie下的透明度,用濾鏡filter:alpha

.box{ width:100px; height:100px; background-color:#000; filter:alpha(Opacity50); opacity: 0.5; } 轉載于:https://www.cnblogs.com/liujinyu/p/3931087.html

mysql 修改字段類型

修改字段類型&#xff1a; alter table 表名 modify column 字段名 類型。 --如 alter table province_quality modify column quality float(8,2); mysql> describe province_quality ; --------------------------------------------------- | Field | Type |…

Ubuntu硬盤優化,降低硬盤溫度(Acer V5 親測成功 )

1.安裝laptop-mode-tools sudo apt-get install laptop-mode-tools2.修改電源管理配置: laptop-mode.conf sudo vim /etc/laptop-mode/laptop-mode.conf修改配置文件&#xff1a; #有關參數的說明請參照文件里的注釋說明 ENABLE_LAPTOP_MODE_ON_AC1 #當筆記本使用交流電時也開啟…

XmlSerializer 對象的Xml序列化和反序列化,XMLROOT別名設置

這篇隨筆對應的.Net命名空間是System.Xml.Serialization&#xff1b;文中的示例代碼需要引用這個命名空間。 為什么要做序列化和反序列化&#xff1f;.Net程序執行時&#xff0c;對象都駐留在內存中&#xff1b;內存中的對象如果需要傳遞給其他系統使用&#xff1b;或者在關機時…

C++智能指針使用指南 part2:智能指針本身的方法以及使用建議

目錄往期文章智能指針本身的方法對于unique_ptr對于shared_ptr對于weak_ptr使用建議1、使用工廠函數而非new構造對象2、在類內部調用其他類的方法3、在某類內部將當前對象指針共享給其他對象4、 智能指針只能管理堆對象&#xff0c;不能管理棧上對象5、不能將this指針直接托管給…

“/”應用程序中的服務器錯誤。

運行時錯誤 說明: 服務器上出現應用程序錯誤。此應用程序的當前自定義錯誤設置禁止遠程查看應用程序錯誤的詳細信息(出于安全原因)。但可以通過在本地服務器計算機上運行的瀏覽器查看。 詳細信息: 若要使他人能夠在遠程計算機上查看此特定錯誤消息的詳細信息&#xff0c;請在位…

JAVA_Collection容器

因為項目的需要&#xff0c;今天抽時間把JAVA中的容器復習了一下&#xff0c;為了以后的不時之需&#xff0c;現在把它記下來。 容器有其名&#xff0c;知其意&#xff0c;用來盛放數據的集合&#xff0c;JAVA中為我們提供了三種容器類&#xff1a;set、list、map&#xff0c;三…