java 为 JSP 创建一个注销链接?

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

Creating a link to logout for JSP?

javajspservlets

提问by Venkata

when a user logins to my application, he submits a form to be processed through the Servlet. The servlet creates a session for the user. How would I create a link so the user can logout? I cannot seem to directly link to a Servlet. How would I delete the session and link back to the homepage?

当用户登录到我的应用程序时,他提交了一个表单以通过 Servlet 进行处理。servlet 为用户创建一个会话。我将如何创建链接以便用户可以注销?我似乎无法直接链接到 Servlet。我将如何删除会话并链接回主页?

HttpSession session = request.getSession(false);
if(session != null)
session.invalidate();
request.getRequestDispatcher("/index.jsp").forward(request,response);

回答by Subhrajyoti Majumder

Make a link <a href="/logout.jspx">Logout</a>where there will be a logout Servletmap to this url and you must invalidate the session this will remove session from server and redirect to homepage and server will create a new session for that.

创建一个链接<a href="/logout.jspx">Logout</a>,其中将有一个Servlet指向此 url的注销映射,您必须使会话无效,这将从服务器中删除会话并重定向到主页,服务器将为此创建一个新会话。

回答by sainath reddy

I suggest you to write a method like logout.do

我建议你写一个像 logout.do 这样的方法

@RequestMapping("/logout.do")
 public ModelAndView logout(HttpSession session){
      session.invalidate();
      return new ModelAndView("/logout.jsp");//if you have two differenet web pages for login and logout else you can redirect to login.jsp
 }

and from your headerFile.jsp link it to

并从您的 headerFile.jsp 链接到

<a href="/logout.do">Logout</a>