Declare a session variable golbaly to access from DoGet and DoPost in a sevlet
我有一个servlet,我需要在其中声明一个可以接受的会话,形式doGet和doPost都应该怎么做?
我已经完成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | @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 { } }} |
但是它说
1 | Can Not find Symbol |
此行HttpSession session = request.getSession(true);
您不需要在servlet中将会话变量作为字段。总的来说-这是一种常见的错误。 servlet只能处理大量请求,除非您将其声明为单线程,否则请求将被并发处理。
HttpSession将通过请求对象在doGet和doPost中预先存在。 Servlet容器将对此进行保证。因此,只需在doGet / doPost中获取对会话的引用,然后执行所需的任何操作即可。
您想要的是HTTP会话的角色之一。
您可以将其视为客户端和服务器之间的对话。
只要"会话"(HTTP会话)处于打开状态并且处于活动状态,您就可以在HTTP会话中设置变量,并从将在同一会话中发送的不同请求中访问它们。
将其视为在"对话时间"中存在的某种"共享内存"。
您可以找到许多有关如何通过Internet进行操作的示例。
这是会话跟踪的示例。