JavaWeb會話技術(shù)詳解與案例
1.什么是會話:

2.會話技術(shù)有哪些:

什么是Cookie?
Cookie,有時也用其復(fù)數(shù)形式 Cookies。類型為“小型文本文件”,是某些網(wǎng)站為了辨別用戶身份,進行Session跟蹤而儲存在用戶本地終端上的數(shù)據(jù)(通常經(jīng)過加密),由用戶客戶端計算機暫時或永久保存的信息。
3.cookie學(xué)習(xí)案例:
cookie:是數(shù)組,中每個元素有:名稱和值,
可以通過名稱找到名稱對應(yīng)的元素。
重要:
Cookie[] cookies = req.getCookies(); //創(chuàng)建cookie對象,獲得瀏覽器所有的cookie數(shù)組
Cookie cookie = CookieUtils.findCookie(cookies,"lasttime");//通過:cookies:一個名稱“l(fā)asttime”,找到數(shù)組里面對應(yīng)的cookies,返回這個cookie:值
String value = cookie.getValue();
resp.getWriter().println(value); //value:獲取cookie(這個)的值,
打印在瀏覽器上面
Cookie c = new Cookie("lasttime", wer); //創(chuàng)建一個cookie,
名稱為:lasttime,值為:wer變量對應(yīng)的值
resp.addCookie(c); //在瀏覽器添加:增加這個名稱的cookie值,
可以
創(chuàng)建:HelloServlet4 extends HttpServlet 這個類
package com.example.demo16;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HelloServlet4 extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
this.getServletContext().setAttribute("name","張三");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doGet(req, resp);
Cookie[] cookies = req.getCookies();
Cookie cookie = CookieUtils.findCookie(cookies,"lasttime");
System.out.println(cookie);
if (cookie==null) {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().println("<h1>您好,歡迎來到本網(wǎng)站!</h1>");
} else {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/htm/UTF-8");
resp.getWriter().println("你上一次訪問時間是:"); //出現(xiàn)了中文錯誤
cookie = CookieUtils.findCookie(cookies, "lasttime");
String value = cookie.getValue();
resp.getWriter().println(value);
Date d = new Date(); //變化的
SimpleDateFormat sc=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String wer=sc.format(d); //字符串
System.out.println(wer);
//其實就是要設(shè)置:時間,時間值為java, 變化的時間
// Cookie c = new Cookie("lasttime","11111"); 這個可以顯示
// Cookie c = new Cookie("lasttime","1-1-1-1-1");
// Cookie c = new Cookie("lasttime","2021-11-14");//2021-11-14 20:14不能反映,中文也不能
// Cookie c = new Cookie("lasttime","2021:11:14");// 空格不行
Cookie c = new Cookie("lasttime", wer);
resp.addCookie(c);
}
/*resp.getWriter().println("helloworld");
resp.setStatus(302);
//resp.setHeader("Location","/demo16_war/helloo");
resp.setHeader("Refresh","5,url=/demo16_war/helloo");
*/
// String name= (String) this.getServletContext().getAttribute("name");
// System.out.println(name);
//text();
text();
}
protected void text() throws IOException {
//Properties properties=new Properties();//創(chuàng)建文件對象
//InputStream is=this.getServletContext().getResourceAsStream("WEB-INF/dp.properties");//這里的路徑,
// properties.load(is);
//String driverClassName=properties.getProperty("driverClassName");
// String url=properties.getProperty("url");
//String password=properties.getProperty("password");
// String usernane=properties.getProperty("username");
// System.out.println(driverClassName);
//System.out.println(url);
//System.out.println(password);
//System.out.println(usernane);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(toString());
System.out.println(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
創(chuàng)建CookieUtils類:
package com.example.demo16;
import javax.servlet.http.Cookie;
public class CookieUtils {
public static Cookie findCookie(Cookie[] cookies, String name){
if(cookies==null){
return null;
}else {
for(Cookie cookie:cookies){
if(name.equals(cookie.getName())) { //找到相等的name名稱
return cookie;
}
}
return null;
}}}
效果圖:

點擊刷新頁面:

4. 使用cookie注意出現(xiàn)的問題:
Cookie c = new Cookie(“l(fā)asttime”,“2021 11 14”);// 空格不行,
返回:亂碼
如果:cookievalue值為:中文,就要設(shè)置:
resp.setCharacterEncoding(“utf-8”); 響應(yīng)設(shè)置為:utf-8,解決中文亂碼
下面:
String werr="我是誰";
Cookie c = new Cookie("lasttime", werr);
resp.addCookie(c);

還有:
可以在 resp.getWriter().println:放html標(biāo)簽
需要
resp.setContentType(“text/html;charset=UTF-8”);//來解析
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().println("<h1>你上一次訪問時間是:</h1>");
到此這篇關(guān)于JavaWeb會話技術(shù)詳解與案例的文章就介紹到這了,更多相關(guān)JavaWeb 會話技術(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA:Git stash 暫存分支修改的實現(xiàn)代碼
這篇文章主要介紹了IDEA:Git stash 暫存分支修改的實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
SpringCloud整合Nacos實現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud整合Nacos實現(xiàn)流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Springboot整合微信支付(訂單過期取消及商戶主動查單)
本文主要介紹了Springboot整合微信支付(訂單過期取消及商戶主動查單),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
如何解決websocket開啟多個頁面訪問同一個連接會失效的問題
使用WebSocket時,若多個頁面訪問同一個WebSocket連接可能會導(dǎo)致連接失效,遇到這個問題時,可以通過在SpringBoot中使用@ServerEndpoint注解并添加@Component來解決,出現(xiàn)連接錯誤通常是因為WebSocket連接接收到的是一個GET請求2024-09-09

