欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaWeb會話技術(shù)詳解與案例

 更新時間:2021年11月16日 17:10:07   作者:yyy言者  
會話技術(shù):在Web開發(fā)中,服務(wù)器跟蹤用戶信息的奇數(shù)稱為會話技術(shù)。會話:指的是一個客戶端與服務(wù)器發(fā)生的一系列請求和響應(yīng)的過程。由于請求包含的信息,在請求被銷毀后也就不存在,多次讓用戶輸入賬號密碼,會影響用戶的使用體驗感,基于此,產(chǎn)生了cookie和session技術(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標簽
需要
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)文章

  • java使用xpath和dom4j解析xml

    java使用xpath和dom4j解析xml

    XPath是一門在XML文檔中查找信息的語言,下面介紹一下java使用xpath和dom4j解析xml的示例,大家參考使用吧
    2014-01-01
  • IDEA報java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤解決辦法

    IDEA報java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤

    這篇文章主要給大家介紹了關(guān)于IDEA報java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤的解決辦法,文中將解決的辦法介紹的非常詳細,需要的朋友可以參考下
    2024-01-01
  • Spring中@Configuration注解的Full模式和Lite模式詳解

    Spring中@Configuration注解的Full模式和Lite模式詳解

    這篇文章主要介紹了Spring中@Configuration注解的Full模式和Lite模式詳解,準確來說,Full?模式和?Lite?模式其實?Spring?容器在處理?Bean?時的兩種不同行為,這兩種不同的模式在使用時候的表現(xiàn)完全不同,今天就來和各位小伙伴捋一捋這兩種模式,需要的朋友可以參考下
    2023-09-09
  • 在SpringBoot項目中解決依賴沖突問題的方法

    在SpringBoot項目中解決依賴沖突問題的方法

    在SpringBoot項目中,依賴沖突是一個常見的問題,特別是當(dāng)項目引入多個第三方庫或框架時,依賴沖突可能導(dǎo)致編譯錯誤、運行時異常或不可預(yù)測的行為,本文給大家介紹了如何在SpringBoot項目中解決以來沖突問題的方法,需要的朋友可以參考下
    2024-01-01
  • IDEA創(chuàng)建Java Web項目不能及時刷新HTML或JSP頁面問題

    IDEA創(chuàng)建Java Web項目不能及時刷新HTML或JSP頁面問題

    這篇文章主要介紹了IDEA創(chuàng)建Java Web項目不能及時刷新HTML或JSP頁面問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java生成指定范圍隨機數(shù)的多種代碼

    java生成指定范圍隨機數(shù)的多種代碼

    今天在寫代碼的時候需要用到一個生成指定范圍隨機數(shù)的函數(shù),百度了一下,發(fā)現(xiàn)了很多種方法,這里簡單為大家整理一下,方便需要的朋友
    2017-08-08
  • Springboot開發(fā)OAuth2認證授權(quán)與資源服務(wù)器操作

    Springboot開發(fā)OAuth2認證授權(quán)與資源服務(wù)器操作

    這篇文章主要介紹了Springboot開發(fā)OAuth2認證授權(quán)與資源服務(wù)器操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • idea配置多環(huán)境啟動方式dev、test、prod

    idea配置多環(huán)境啟動方式dev、test、prod

    這篇文章主要介紹了idea配置多環(huán)境啟動方式dev、test、prod,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • spring redis 如何實現(xiàn)模糊查找key

    spring redis 如何實現(xiàn)模糊查找key

    這篇文章主要介紹了spring redis 如何實現(xiàn)模糊查找key的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Springboot和bootstrap實現(xiàn)shiro權(quán)限控制配置過程

    Springboot和bootstrap實現(xiàn)shiro權(quán)限控制配置過程

    這篇文章主要介紹了Springboot和bootstrap實現(xiàn)shiro權(quán)限控制,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04

最新評論