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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7958272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:11:20  来源:igfitidea点击:

how to forward session from one servlet to another?

javajspservlets

提问by Dhruv

Hey guys i'm working on admin module for my project. When a person logs-in, a request is sent to login 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. The session is lost between two servlets.

嘿伙计们,我正在为我的项目开发管理模块。当一个人登录时,一个请求被发送到登录 servlet。当它通过单击其他选项进一步请求其他报告时,对报告的请求将发送到其他 servlet,该 servlet 在页面上给出结果,该页面在用户时显示为正常类型。两个 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. This can be done by fetching value of user_type from login module bean class.

我试图在其他页面上导航生成的报告,但为此我需要知道第二个 servlet 中的用户类型。这可以通过从登录模块 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. i'm get a nullpointer exception here.

这里 LoginService 是登录模块的 bean 类。我在这里得到一个空指针异常。

回答by G_H

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. Methods from HttpServletare given a HttpServletRequest. You can obtain the corresponding HttpSessionusing one of the getSessionmethods of that class.

我认为你正在尝试做你的 web 容器应该为你处理的事情......应该在来自同一客户端会话的多个 servlet 调用过程中自动维护一个会话。来自HttpServlet的方法被赋予一个HttpServletRequest. 您可以HttpSession使用getSession该类的方法之一获得相应的。

You can bind stuff to the HttpSessionusing setAttributeand getAttribute.

您可以将东西绑定到HttpSessionusingsetAttributegetAttribute

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. 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.

servlet 可以通过名称将对象属性绑定到 HttpSession 实现中。任何绑定到会话中的对象都可用于属于同一 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. 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. 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.

我认为您最好从 HttpServletRequest 获取 HttpSession 对象(至少假设它是一个 HttpServlet)并通过它设置/获取属性。如果您为您的属性选择了一个正确的名称(它遵循与 Java 包命名相同的约定),您可以确保返回的对象,只要它不为空,就可以转换为您放入的任何类型。设置和获取请求本身的属性不会有帮助,我不认为东西会从一个 servlet 调用转移到下一个 servlet 除非你用 a 从另一个调用一个 servlet RequestDispatcher,但这不是你所追求的这里。

So in your second code sample, do (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...).

所以在你的第二个代码示例中, do(LoginService)request.getSession().getAttribute("currentSessionUser");应该可以工作。确保检查空值,并可能选择使用项目包名称约定的属性名称(如com.mycompany...)。

I wouldn't mind a second opinion here since I'm not much of an EE/web developer.

我不介意在这里提出第二意见,因为我不是 EE/Web 开发人员。