本网站response.sendRedirect跳转到别人网站的时候,会被本网站的Filter拦截

Java重定向技巧
本文介绍了一种使用Java在Web应用中实现页面重定向的方法,通过输出HTML和JavaScript代码来间接完成重定向,避免了在过滤器中直接使用response.sendRedirect导致的潜在死循环问题。
  private static void sendRedirect(HttpServletResponse response,String url){
        //Filter里面避免用resopnse.redirect+filter造成的跳转死循环
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.println("<html>");

            out.println("<script type=text/javascript>");

            out.println("window.location.href='"+url+"'");

            out.println("</script>");

            out.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录页面</title> <style> .login-box { width: 300px; margin: 100px auto; padding: 20px; border: 1px solid #eee; border-radius: 8px; } input { width: 100%; padding: 8px; margin: 8px 0; box-sizing: border-box; } .btn { background: #0084ff; color: white; border: none; border-radius: 4px; cursor: pointer; } .error { color: red; text-align: center; } </style> </head> <body> <div class="login-box"> <form action="loginCheck.jsp" method="post"> 用户名: <input type="text" name="username" required placeholder="请输入用户名"><br> 密码: <input type="password" name="password" required placeholder="请输入密码"><br> <input type="submit" class="btn" value="登录"> <% String error = request.getParameter("error"); %> <% if (error != null && "1".equals(error)) { %> <div class="error">用户名或密码错误</div> <% } %> </form> </div> </body> </html><%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.*" %> <%@ page import="org.mindrot.jbcrypt.BCrypt" %> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) { response.sendRedirect("login.jsp?error=1"); return; } username = username.trim(); password = password.trim(); // ########## 请修改这里的数据库配置(改为你的 MySQL 信息)########## String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/yourdb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; String dbUser = "root"; // 你的 MySQL 用户名(默认 root) String dbPwd = "123456"; // 你的 MySQL 密码(安装时设置的) // ########################################################### Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, dbUser, dbPwd); String sql = "SELECT password FROM users WHERE username = ?"; ps = conn.prepareStatement(sql); ps.setString(1, username); rs = ps.executeQuery(); if (rs.next()) { String dbPwdHash = rs.getString("password"); if (BCrypt.checkpw(password, dbPwdHash)) { session.setAttribute("username", username); session.setMaxInactiveInterval(30 * 60); response.sendRedirect("welcome.jsp"); } else { response.sendRedirect("login.jsp?error=1"); } } else { response.sendRedirect("login.jsp?error=1"); } } catch (ClassNotFoundException e) { System.err.println("数据库驱动加载失败:" + e.getMessage()); response.sendRedirect("login.jsp?error=1"); } catch (SQLException e) { System.err.println("数据库错误:" + e.getMessage()); response.sendRedirect("login.jsp?error=1"); } catch (Exception e) { System.err.println("系统错误:" + e.getMessage()); response.sendRedirect("login.jsp?error=1"); } finally { if (rs != null) try { rs.close(); } catch (SQLException e) {} if (ps != null) try { ps.close(); } catch (SQLException e) {} if (conn != null) try { conn.close(); } catch (SQLException e) {} } %><%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>欢迎页面</title> <style> .welcome { text-align: center; margin-top: 100px; } a { color: #0084ff; text-decoration: none; } a:hover { text-decoration: underline; } </style> </head> <body> <% String username = (String) session.getAttribute("username"); if (username == null || username.trim().isEmpty()) { response.sendRedirect("login.jsp"); return; } %> <div class="welcome"> <h1>欢迎回来,<%= username %>!</h1> <br> <a href="logout.jsp">安全注销</a> </div> </body> </html><%@ page contentType="text/html;charset=UTF-8" language="java" %> <% session.invalidate(); response.sendRedirect("login.jsp"); %>
最新发布
11-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值