Java 声明一个会话变量golbaly,以便在sevlet中从DoGet和DoPost访问

Java 声明一个会话变量golbaly,以便在sevlet中从DoGet和DoPost访问,java,jakarta-ee,servlets,Java,Jakarta Ee,Servlets,我有一个servlet,我需要在其中声明一个可以从doGet和doPost接受的会话,我应该怎么做? 我已经做了 @WebServlet(name = "LoginLogout", urlPatterns = {"/LoginLogout.do"})public class LoginLogout extends HttpServlet {//For Session HttpSession session = request.getSession(true); protected void p

我有一个servlet,我需要在其中声明一个可以从doGet和doPost接受的会话,我应该怎么做? 我已经做了

@WebServlet(name = "LoginLogout", urlPatterns = {"/LoginLogout.do"})public class LoginLogout extends HttpServlet {//For Session
HttpSession session = request.getSession(true);

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String status = request.getParameter("status");
    System.out.println(status);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {
        String loginId = request.getParameter("login_id");
        String password = request.getParameter("password");

        System.out.println(loginId);

        //Inserting value to the Pogo named "newLoginPogo"
        loginData newLoginPogo = new loginData();
        newLoginPogo.setLoginId(loginId);
        newLoginPogo.setPassword(password);

        //Creating a obj of ModelLogin to send the loginId and Password via a method which is in ModelLogin class
        ModelLogin loginBis = new ModelLogin();
        loginData userData = loginBis.checkUser(newLoginPogo);
        String userExist = userData.getUserExist();
        System.out.println(userExist);
        if ("yes".equals(userExist)) {
            System.out.println("In while loop of Servlet");

            String firstName = userData.getFirstName();
            String userId = userData.getUserId();
            boolean IsSu = userData.getIsSu();
            //conveting boolean to string
            String superuser = new Boolean(IsSu).toString();

            //Creating a session 

            session.setAttribute("firstName", firstName);
            session.setAttribute(userId, "userId");
            session.setAttribute(superuser, "IsSu");
            //==============================================================================================================
            //If user does exist show the Success Message and forward Dashboard 
            //==============================================================================================================

            //Session for success message
            String succmsg = "Login Successful";
            session.setAttribute("succmsg", succmsg);

            getServletConfig().getServletContext().getRequestDispatcher("/WEB-INF/ViewPages/dashboard/dashboard.jsp").forward(request, response);

        } //==============================================================================================================
        //If user does not exist show the Error Message  
        //==============================================================================================================
        else if ("no".equals(userExist)) {
            //Session for success message
            System.out.println("inside NO");
            String emsg = "Login Error";
            session.setAttribute("errmsg", emsg);
            getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        } else {
        }
        /*
        //===============================================================================================================    
        //code for Logout
        //===============================================================================================================
        String status = request.getParameter("status");
        if ("logout".equals(status)) {
            //clearing the session
            session.invalidate();
            //forwarding to index page
            getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        }
        */
    } finally {
    }
}}
但是它说

Can Not find Symbol

在这一行中,HttpSession session=request.getSession(true)

您不需要在servlet中将会话变量作为字段。一般来说,这是一种常见的错误。只有一个servlet实例为大量请求提供服务,除非您将其声明为单线程,否则请求将被并发处理


HttpSession将通过请求对象预先存在于doGet和doPost中。Servlet容器将保证这一点。因此,只需在doGet/doPost中获取对会话的引用,然后做任何您想做的事情。

您不需要在servlet中将会话变量作为字段。一般来说,这是一种常见的错误。只有一个servlet实例为大量请求提供服务,除非您将其声明为单线程,否则请求将被并发处理


HttpSession将通过请求对象预先存在于doGet和doPost中。Servlet容器将保证这一点。因此,只需在doGet/doPost中获取对会话的引用,然后做任何您想做的事情。

您想要的是HTTP会话的角色之一
您可以将其视为客户端和服务器之间的对话
只要“会话”(HTTP会话)打开且处于活动状态,就可以在HTTP会话上设置变量,并从将在同一会话上发送的不同请求中访问这些变量
将其视为“对话时间”中存在的某种“共享内存”
你可以在互联网上找到很多这样做的例子

下面是一个例子

您想要的是HTTP会话的角色之一
您可以将其视为客户端和服务器之间的对话
只要“会话”(HTTP会话)打开且处于活动状态,就可以在HTTP会话上设置变量,并从将在同一会话上发送的不同请求中访问这些变量
将其视为“对话时间”中存在的某种“共享内存”
你可以在互联网上找到很多这样做的例子

下面是一个例子

查看我的评论,我对其进行了编辑,并添加了指向示例的链接查看我的评论,我对其进行了编辑,并添加了指向示例的链接