1

我有一个 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);

4

2 回答 2

1

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

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

于 2012-09-08T06:14:13.253 回答
0

您想要的是 HTTP 会话的角色之一。
您可以将其视为客户端和服务器之间的对话。
只要“会话”(HTTP 会话)是打开的并且处于活动状态,您就可以在 HTTP 会话上设置变量,并从将在同一会话上发送的不同请求访问它们。
将此视为在“对话时间”中存在的某种“共享记忆”。
您可以在互联网上找到许多有关如何执行此操作的示例。
这是会话跟踪的示例。

于 2012-09-08T06:18:06.977 回答