java 使用sendredirect在Java中保留会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13461838/
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-31 12:49:55  来源:igfitidea点击:

Preserving session in Java with sendredirect

javajspjakarta-eeservletsjspinclude

提问by

I am creating a Login Application in JAVA.I am making the presentation in JSP and all the logic (Database connectivity) in Servlet [this is not a right approach I know that]. I check the username Password in Servlet and then create a session variable. and add the session like this

我正在 JAVA 中创建一个登录应用程序。我在 JSP 中进行演示,在 Servlet 中进行所有逻辑(数据库连接)[这不是我知道的正确方法]。我在 Servlet 中检查用户名密码,然后创建一个会话变量。并像这样添加会话

sess.setAttribute("username",oName);

Then I redirect the user to its homepage say student.jsp

然后我将用户重定向到它的主页,比如 student.jsp

response.sendRedirect("student.jsp");

It removes the session variable.I need a way to preserve the session variable and move to student.jsp.I tried to use forwading but that didn't worked out.

它删除了会话变量。我需要一种方法来保留会话变量并移动到 student.jsp。我尝试使用转发但没有成功。

RequestDispatcher dispatcher =
                getServletContext()
                    .getRequestDispatcher("/student.jsp");

            if (dispatcher != null) {
                dispatcher.forward(request, response);
            }

It forward request but the Page address doesn't change to student.jsp which is not good. Any help in this regard will be appreciated Thank you

它转发请求,但页面地址没有更改为 student.jsp,这是不好的。在这方面的任何帮助将不胜感激谢谢

采纳答案by Akber Choudhry

For the redirected request to come back and attach to the same session, it needs a session ID, usually carried in a JSESSIONID (or another name) cookie or in the URL as a parameter.

对于重定向的请求返回并附加到同一个会话,它需要一个会话 ID,通常携带在 JSESSIONID(或其他名称)cookie 或 URL 中作为参数。

This cookie or URL parameter should be added by the servlet container and you should not have to add it yourself.

此 cookie 或 URL 参数应由 servlet 容器添加,您不必自己添加。

If you do not see the cookie in your browser, and you are not attaching the JSESSIONID to the URL, then it is creating a new session with each request, and not attaching to the same session.

如果您在浏览器中没有看到 cookie,并且您没有将 JSESSIONID 附加到 URL,那么它会为每个请求创建一个新会话,而不是附加到同一个会话。

回答by Benas

Try to edit your tomcat context.xmlfile and replace <Context>tag to <Context useHttpOnly="false">, this helped me.

尝试编辑您的 tomcatcontext.xml文件并将<Context>标记替换为<Context useHttpOnly="false">,这对我有帮助。

回答by Audrius Meskauskas

Some browsers like Chromium for instance do not allow cookies from localhost or IP addresses, so the session cannot be preserved and changes on every refresh. This can be easily checked with the browser developer tools that show all cookies of the request.

例如,某些浏览器(例如 Chromium)不允许来自本地主机或 IP 地址的 cookie,因此无法保留会话并在每次刷新时更改。这可以使用显示请求的所有 cookie 的浏览器开发人员工具轻松检查。

For development, set up your workstation to resolve some more serious name (like host.kitty.com) into localhosts. Under Linux, just add entry to /etc/hosts.

对于开发,设置您的工作站以将一些更重要的名称(如 host.kitty.com)解析为本地主机。在 Linux 下,只需将条目添加到 /etc/hosts。

回答by Sandeep1007

Use the RequestDispatcherand set your username variable using request.setAttribute(). In this case the dispatcher will not create a new request but the same request will be forwarded using the forward()method.

使用RequestDispatcher并使用设置您的用户名变量request.setAttribute()。在这种情况下,调度程序不会创建新请求,但将使用该forward()方法转发相同的请求。