当前位置: 首页 > 知识库问答 >
问题:

注销阻止后退按钮java不起作用我已经尝试了一整天,但仍然无法得到结果,我错过了什么?

晏卓君
2023-03-14

这是我的Java课程

package login.example;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.persistence.Id;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

    private static final long serialVersionUID = -7250065889869767422L;

    @Id
    private long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(long id) {
        this.id = id;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        // String userRePass = request.getParameter("userRePassWord");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/any", "root", "jaygurudev1@");
            PreparedStatement ps = con.prepareStatement("Select id,name from user where id=? and name=?");
            ps.setLong(1, id);
            ps.setString(2, name);
            ResultSet rs = ps.executeQuery();
            // HttpServletRequest request = null;
            if (rs.next()) {
                HttpSession session = request.getSession(true);
                session.setAttribute("id", id);
                session.setAttribute("name", name);
                ServletContext context = request.getServletContext();

                response.setHeader("Cache-control", "no-cache"); // HTTP 1.1
                response.setHeader("Cache-control", "no-store");
                response.setHeader("Pragma", "no-cache"); // HTTP 1.0
                response.setDateHeader("Expire", 0);
                RequestDispatcher dispatcher = context.getRequestDispatcher("/admin.jsp");
                dispatcher.forward(request, response);
                // or you can write whole thing in one line as ........

                // getServletContext().getRequestDispatcher("/success").forward(request,
                // response);

            } else {
                // request.setAttribute("wrongUser", userName);

                ServletContext context = request.getServletContext();

                response.setHeader("Cache-control", "no-cache"); // HTTP 1.1
                response.setHeader("Cache-control", "no-store");
                response.setHeader("Pragma", "no-cache"); // HTTP 1.0
                response.setDateHeader("Expire", 0);

                RequestDispatcher dispatcher = context.getRequestDispatcher("/login.jsp");
                dispatcher.forward(request, response);

                // or you can write whole thing in one line as ........
                // getServletContext().getRequestDispatcher("/fail").forward(request,
                // response);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

这是我的login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <div style="text-align: center">
        <h1>admin login</h1>
        <%
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
            response.setHeader("Pragma", "no-cache"); // HTTP 1.0
            response.setDateHeader("Expires", 0); // Proxies.
        %>
        <form action="/UserOne" method="get">
            <label for="id">Id</label> <input name="id" /> <label for="name">Name</label>
            <input type="text" name="name" />
            <button type="submit">Login</button>
        </form>

    </div>
</body>
</html>

这是我的管理员。jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.PrintWriter"%>
<!DOCTYPE html>
<html>
<head>
<style>
body {
    background-image: url(/images/admin.jpg);
    background-repeat: repeat;
    background-size: 640px 640px;
}
</style>
</head>
<body>

    <%
        response.setHeader("Cache-control", "no-cache"); // HTTP 1.1
        response.setHeader("Cache-control", "no-store");
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0
        response.setDateHeader("Expire", 0);
    %>
    <a href="/userone">get all users</a>
    <h3>view user</h3>

    getMobileInfo
    <a href="/indexone">get all mobile details</a>


    <form action="/LogoutServlet" method="get">
        <input type="submit" value="logout">
    </form>

</body>
</html>

这是注销servlet

package login.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "LogoutServlet", urlPatterns = { "/LogoutServlet" })
public class LogoutServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public LogoutServlet() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        HttpSession session = request.getSession();
        session.removeAttribute("id");
        session.removeAttribute("name");
        request.getSession(false);
        session.invalidate();
        out.println("Thank you,you are successfully logged out");
        // response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");

        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
        httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
        httpResponse.setDateHeader("Expires", 0); // Proxies.

        // request.getRequestDispatcher("login.jsp").include(request, response);
        response.sendRedirect("login.jsp");
    }

}

我应该添加或删除什么以获得所需的输出,例如在我注销后它不会显示上一页?。。我的代码怎么了?我是否需要依赖或其他任何东西来实现这一点?如何解决这个问题?提前谢谢。。我的url就像http://localhost:8080/home.html然后http://localhost:8080/login.jsp?http://localhost:8080/UserOne?id=1


共有1个答案

严元白
2023-03-14

当你点击后退按钮的网址(/UserOne? id=1

ie:登录。jsp提交方法应该是post

 类似资料: