使用sendredirect在Java中保留会话

编程入门 行业动态 更新时间:2024-10-24 06:26:36
本文介绍了使用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中保留会话

本文发布于:2023-07-15 10:26:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:sendredirect   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!