JavaWeb會(huì)話技術(shù)詳解與案例
1.什么是會(huì)話:
2.會(huì)話技術(shù)有哪些:
什么是Cookie?
Cookie,有時(shí)也用其復(fù)數(shù)形式 Cookies。類型為“小型文本文件”,是某些網(wǎng)站為了辨別用戶身份,進(jìn)行Session跟蹤而儲(chǔ)存在用戶本地終端上的數(shù)據(jù)(通常經(jīng)過加密),由用戶客戶端計(jì)算機(jī)暫時(shí)或永久保存的信息。
3.cookie學(xué)習(xí)案例:
cookie:是數(shù)組,中每個(gè)元素有:名稱和值,
可以通過名稱找到名稱對(duì)應(yīng)的元素。
重要:
Cookie[] cookies = req.getCookies(); //創(chuàng)建cookie對(duì)象,獲得瀏覽器所有的cookie數(shù)組 Cookie cookie = CookieUtils.findCookie(cookies,"lasttime");//通過:cookies:一個(gè)名稱“l(fā)asttime”,找到數(shù)組里面對(duì)應(yīng)的cookies,返回這個(gè)cookie:值
String value = cookie.getValue();
resp.getWriter().println(value);
//value:獲取cookie(這個(gè))的值,
打印在瀏覽器上面
Cookie c = new Cookie("lasttime", wer); //創(chuàng)建一個(gè)cookie, 名稱為:lasttime,值為:wer變量對(duì)應(yīng)的值 resp.addCookie(c); //在瀏覽器添加:增加這個(gè)名稱的cookie值, 可以
創(chuàng)建:HelloServlet4 extends HttpServlet
這個(gè)類
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("你上一次訪問時(shí)間是:"); //出現(xiàn)了中文錯(cuò)誤 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í)就是要設(shè)置:時(shí)間,時(shí)間值為java, 變化的時(shí)間 // Cookie c = new Cookie("lasttime","11111"); 這個(gè)可以顯示 // 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)建文件對(duì)象 //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; }}}
效果圖:
點(diǎn)擊刷新頁(yè)面:
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>你上一次訪問時(shí)間是:</h1>");
到此這篇關(guān)于JavaWeb會(huì)話技術(shù)詳解與案例的文章就介紹到這了,更多相關(guān)JavaWeb 會(huì)話技術(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA報(bào)java:?java.lang.OutOfMemoryError:?Java?heap?space錯(cuò)誤
這篇文章主要給大家介紹了關(guān)于IDEA報(bào)java:?java.lang.OutOfMemoryError:?Java?heap?space錯(cuò)誤的解決辦法,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Spring中@Configuration注解的Full模式和Lite模式詳解
這篇文章主要介紹了Spring中@Configuration注解的Full模式和Lite模式詳解,準(zhǔn)確來說,Full?模式和?Lite?模式其實(shí)?Spring?容器在處理?Bean?時(shí)的兩種不同行為,這兩種不同的模式在使用時(shí)候的表現(xiàn)完全不同,今天就來和各位小伙伴捋一捋這兩種模式,需要的朋友可以參考下2023-09-09在SpringBoot項(xiàng)目中解決依賴沖突問題的方法
在SpringBoot項(xiàng)目中,依賴沖突是一個(gè)常見的問題,特別是當(dāng)項(xiàng)目引入多個(gè)第三方庫(kù)或框架時(shí),依賴沖突可能導(dǎo)致編譯錯(cuò)誤、運(yùn)行時(shí)異常或不可預(yù)測(cè)的行為,本文給大家介紹了如何在SpringBoot項(xiàng)目中解決以來沖突問題的方法,需要的朋友可以參考下2024-01-01IDEA創(chuàng)建Java Web項(xiàng)目不能及時(shí)刷新HTML或JSP頁(yè)面問題
這篇文章主要介紹了IDEA創(chuàng)建Java Web項(xiàng)目不能及時(shí)刷新HTML或JSP頁(yè)面問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作
這篇文章主要介紹了Springboot開發(fā)OAuth2認(rèn)證授權(quán)與資源服務(wù)器操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06idea配置多環(huán)境啟動(dòng)方式dev、test、prod
這篇文章主要介紹了idea配置多環(huán)境啟動(dòng)方式dev、test、prod,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09spring redis 如何實(shí)現(xiàn)模糊查找key
這篇文章主要介紹了spring redis 如何實(shí)現(xiàn)模糊查找key的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Springboot和bootstrap實(shí)現(xiàn)shiro權(quán)限控制配置過程
這篇文章主要介紹了Springboot和bootstrap實(shí)現(xiàn)shiro權(quán)限控制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04