日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

会话技术Session

發(fā)布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 会话技术Session 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.Session的概念
  • Session是依賴于Cookie的,
  • 每次請求時,會將特殊標(biāo)識帶到服務(wù)器端,根據(jù)這個標(biāo)識來找到對應(yīng)的內(nèi)存空間,從而實(shí)現(xiàn)數(shù)據(jù)共享!是Servlet規(guī)范中四大域?qū)ο笾坏臅捰驅(qū)ο蟆?/li>
  • 作用:是Servlet規(guī)范中四大域?qū)ο笾坏臅捰驅(qū)ο蟆?梢詫?shí)現(xiàn)數(shù)據(jù)共享
2.Session設(shè)置共享數(shù)據(jù)
//Session的基本使用 @WebServlet("/ServletDemo1") public class ServletDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//設(shè)置請求字符編碼,防止亂碼req.setCharacterEncoding("utf-8");//設(shè)置瀏覽器響應(yīng)編碼[html類型的文本,字符集為utf-8]resp.setContentType("text/html;charset=utf-8");//1.獲取請求的用戶名String username = req.getParameter("username");//2.獲取HttpSession的對象HttpSession session = req.getSession(true);//默認(rèn)為true,表示沒有session則自動創(chuàng)建//[驗(yàn)證是否為同一Session]System.out.println(session);System.out.println(session.getId());//3.將用戶名信息添加到共享數(shù)據(jù)中session.setAttribute("username",username);//2.解決Cookie禁用方法2:重寫url"<a href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemo1</a>"// resp.getWriter().write("<a href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemoO</a>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }
3.Session獲取共享數(shù)據(jù)
//Session獲取共享數(shù)據(jù)[同一個session] @WebServlet("/ServletDemo2") public class ServletDemo2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//設(shè)置瀏覽器響應(yīng)編碼[html類型的文本,字符集為utf-8]resp.setContentType("text/html;charset=utf-8");//1.獲取HttpSession對象HttpSession session = req.getSession();//[驗(yàn)證是否為同一Session]System.out.println(session);System.out.println(session.getId());//2.獲取共享數(shù)據(jù)String username = (String) session.getAttribute("username");//3.將響應(yīng)數(shù)據(jù)給瀏覽器resp.getWriter().write(username);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }
4.Cookie的禁用解決方法
//Cookie的禁用 @WebServlet("/ServletDemo3") public class ServletDemo3 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.解決方法1:提示HttpSession session = req.getSession(false);System.out.println(session);if (session==null){resp.setContentType("text/html;charset=utf-8");resp.getWriter().write("為了不影響使用,請不要禁用瀏覽器的Cookie");}//2.解決方法2:重寫url"<a 【需到第一個界面重寫】href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemo1</a>"// resp.getWriter().write("<a href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemoO</a>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

總結(jié)

以上是生活随笔為你收集整理的会话技术Session的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。