• session会话


    jsp会话篇session:

    package com.log;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class LoginServlet
     */
    @WebServlet("/LoginServlet.do")
    public class LoginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public LoginServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           //首先设置响应和请求页面的编码格式:
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html charset='utf-8'");
           //获取表单控件的数据
            Enumeration<String> nameEnu = request.getParameterNames();
            //创建list对象
            List<Object> list = new ArrayList<>();
            while(nameEnu.hasMoreElements())
            {
                   list.add(request.getParameter(nameEnu.nextElement()));
            }
            //把数据放在session对象
            HttpSession session = request.getSession();
            if(list.size()>0){
            session.setAttribute("uName", list.get(0));
            }
            
            //第二种方式,通过Cookie保存用户信息
            Cookie cook1 = new Cookie("userName", list.get(0).toString());
            cook1.setMaxAge(3*3600);
            response.addCookie(cook1);
            
    //        request.getRequestDispatcher("/main.jsp").forward(request, response);
            response.sendRedirect(request.getContextPath()+"/welcome.jsp");
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
  • 相关阅读:
    Mybatis动态SQL
    自己动手写一个持久层框架
    最长公共子串算法(Longest Common Substring)
    【SpringCloud】08.客户端负载均衡器:Ribbon
    ESP32 (idf-esp-v4.1)重新生成nvs分区
    IDEA导入新的springboot项目出错
    springboot集成mybatis出现问题/连接数据库出错
    Java学习周记2
    2020.8.6_Java学习日记
    9.23笔试总结
  • 原文地址:https://www.cnblogs.com/waarp/p/7233547.html
Copyright © 2020-2023  润新知