使用sendredirect在Java中保留会话 [英] Preserving session in Java with sendredirect

查看:287
本文介绍了使用sendredirect在Java中保留会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

sess.setAttribute("username",oName);

然后我将用户重定向到其主页上说student.jsp

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

response.sendRedirect("student.jsp");

它删除会话变量。我需要一种方法来保存会话变量并转移到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.

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

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

它转发请求但页面地址不会更改为student.jsp不好。
在这方面的任何帮助将被赞赏
谢谢

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

推荐答案

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

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.

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

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

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

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.

这篇关于使用sendredirect在Java中保留会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆