servlet 中session的使用方法(創建,使用)


創建:

1 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
2     req.setCharacterEncoding("utf-8");
3     resp.setContentType("text/html;charset=utf-8");
4     //創建session
5     HttpSession session=req.getSession();
6     session.setAttribute("name", "周博");
7 }

使用:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
 {
    req.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");
    HttpSession session=req.getSession();
    PrintWriter out=resp.getWriter();
        out.println(session.getAttribute("name"));
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM