简体   繁体   English

单击“重定向”按钮,终止我的会话并重定向到登录页面

[英]Kill my session and redirect to login page on click of Redirect button

This is my Java code and HTML code. 这是我的Java代码和HTML代码。

When the user clicks on the Redirect button, I want to redirect him to the log-in page and kill the current session. 当用户单击“重定向”按钮时,我想将他重定向到登录页面并终止当前会话。 I have succeeded in the redirection part but I do not know how to kill the session. 我已经成功完成了重定向部分,但是我不知道该如何终止会话。 I would like to use the session.invalidate in the action= in HTML. 我想在HTML的action =中使用session.invalidate。 Can this be done?If not then what the best alternatives? 可以做到吗?如果没有,那么最好的选择是什么? Thx. 谢谢。

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.*;

@WebServlet("/test1")
public class test1 extends HttpServlet{

public void init(){

        System.out.println("init is called");
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{

        res.setContentType("text/html");

        PrintWriter pw=res.getWriter();// used to type in the browser

        String s=req.getParameter("username");
        String s2=req.getParameter("pwd");

        // Create a session object if it is already not  created.
          HttpSession session = req.getSession(true);



        if(s.equals("ace")&& s2.equals("123"))
        {
            //pw.println("<h1>"+"Welcome to the world of servlets "+s+"</h1>");



            String title= "Welcome to the world of servlets: "+s;

            String s3="The session id is: "+session.getId();

            String docType =
                      "<!doctype html public \"-//w3c//dtd html 4.0 " +
                      "transitional//en\">\n";

                pw.println(docType +
                                "<html>\n" +
                                "<head><title>" + title + "</title></head>\n" +
                                "<body bgcolor=\"#f0f0f0\">\n" +
                                "<h1 align=\"center\">" + title + "</h1>\n" +
                                "<h1 align=\"center\">" + s3 + "</h1>\n"+
                                "<ul>\n" +
                                "<input type=\"button\" onclick=\"location.href('http://localhost:8080/AkshayServlet2/test.html');\" value=\"Redirect\">"+ 

                                "</ul>\n" +
                                "</body></html>");

                session.invalidate();

                //System.out.println.print("You have logged out");


        }
        else
        {

            pw.println("Please enter the correct username and password");
            req.getRequestDispatcher("test.html").include(req, res);;

        } 


    }

    public void destroy(){

        System.out.println("calling destroy method");

    }

}

HTML code: HTML代码:

<html>
<body> 

<form action="test1" method=get>  <!-- we use xy because we used xy in the url in web.xml. We have to map it to the url -->

Enter UserName: <input type=text name=username><br>
Enter Password: <input type=password name=pwd><br>

<input type=submit value="Login">

</form>  

</body>

</html>

You have to call the HttpServletRequest::getSession method so that a new session is created to replace the previous invalidated session. 您必须调用HttpServletRequest :: getSession方法,以便创建一个新会话来替换以前的无效会话。

Add the following code after the session invalidation line: 在会话无效行之后添加以下代码:

req.getSession(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM