简体   繁体   English

如何将会话从一个servlet转发到另一个?

[英]how to forward session from one servlet to another?

Hey guys i'm working on admin module for my project. 大家好,我正在为我的项目开发管理模块。 When a person logs-in, a request is sent to login servlet. 一个人登录时,将发送一个请求以登录servlet。 When it further ask for some other report by clicking other options a request for the report is sent to other servlet which gives the result on the page which is shown at the time of user which is of normal type. 当通过单击其他选项进一步请求其他报告时,报告请求将发送到其他servlet,该请求将在页面上显示结果,这是普通用户时显示的结果。 The session is lost between two servlets. 会话在两个servlet之间丢失。

I am trying to navigate the generated report on some other page but for that i need to know user type in second servlet. 我试图在其他页面上导航生成的报告,但是为此,我需要知道第二个servlet中的用户类型。 This can be done by fetching value of user_type from login module bean class. 这可以通过从登录模块bean类中获取user_type的值来完成。

How to handle this situation? 如何处理这种情况? thanks 谢谢

My login servlet is : 我的登录servlet是:

LoginService user = new LoginService();
                 user.setUserName(request.getParameter("username"));
                 user.setPassword(request.getParameter("password"));

                 user = UserDAO.login(user);

                 if (user.isValid())
                 {

                      HttpSession session = request.getSession(true);       
                      session.setAttribute("currentSessionUser",user); 



                      if(user.getUser_type().equalsIgnoreCase("admin")){

                          response.sendRedirect("administrator/homepage.jsp");
                      }else{

                      response.sendRedirect("homepage.jsp"); //logged-in page
                      }
                 }

                 else 
                      response.sendRedirect("invalidlogin.jsp"); //error page 
            } 

i tried using this in second servlet:- 我尝试在第二个servlet中使用它:-

LoginService session = (LoginService)request.getAttribute("currentSessionUser");

            String drake = session.getUser_type();
            System.out.println("usertype = " +drake);

Here LoginService is the bean class of login module. 这里LoginService是登录模块的bean类。 i'm get a nullpointer exception here. 我在这里得到一个nullpointer异常。

I think you're trying to do stuff that your web container should handle for you... A session should automatically be maintained over the course of multiple servlet calls from the same client session. 我认为您正在尝试做您的Web容器应为您处理的工作...在从同一客户端会话进行的多个servlet调用过程中,应自动维护会话。 Methods from HttpServlet are given a HttpServletRequest . HttpServlet方法被赋予HttpServletRequest You can obtain the corresponding HttpSession using one of the getSession methods of that class. 您可以使用该类的getSession方法之一获取相应的HttpSession

You can bind stuff to the HttpSession using setAttribute and getAttribute . 您可以使用setAttributegetAttribute将内容绑定到HttpSession

EDIT : I'm taking this from the Servlet spec 2.5: 编辑 :我从Servlet规范2.5中获取此信息:

A servlet can bind an object attribute into an HttpSession implementation by name. Servlet可以按名称将对象属性绑定到HttpSession实现中。 Any object bound into a session is available to any other servlet that belongs to the same ServletContext and handles a request identified as being a part of the same session. 绑定到会话中的任何对象对于属于同一ServletContext并处理标识为同一会话一部分的请求的任何其他Servlet都可用。

I think you're better off getting the HttpSession object from the HttpServletRequest (at least assuming it's a HttpServlet) and setting/getting attributes through that. 我认为您最好从HttpServletRequest中获取HttpSession对象(至少假设它是HttpServlet)并通过它设置/获取属性。 If you choose a proper name (it follows the same convention as Java package naming) for your attribute, you can be sure the returned object, as long as it's not null, can be cast to whatever type you put in there. 如果为属性选择一个适当的名称(它遵循与Java包命名相同的约定),则可以确保返回的对象(只要它不为null)可以强制转换为您在其中放置的任何类型。 Setting and getting attributes on the request itself isn't gonna help, I don't think stuff will get carried over from one servlet call to the next unless you call one servlet from the other with a RequestDispatcher , but that's not what you're after here. 在请求本身上设置和获取属性无济于事,除非您使用RequestDispatcher从另一个servlet调用另一个servlet,否则我认为东西不会从一个servlet调用转移到另一个servlet调用。在这里之后。

So in your second code sample, do (LoginService)request.getSession().getAttribute("currentSessionUser"); 因此,在第二个代码示例中,执行(LoginService)request.getSession().getAttribute("currentSessionUser"); , that ought to work. ,那应该起作用。 Make sure to check for nulls and maybe choose an attribute name that uses your project's package name convention (like com.mycompany... ). 确保检查是否为空,并可能选择使用项目的程序包名称约定的属性名称(例如com.mycompany... )。

I wouldn't mind a second opinion here since I'm not much of an EE/web developer. 我不介意第二种意见,因为我不是一位EE / Web开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM