gpt4 book ai didi

java - 登录失败后在登录页面中显示错误消息 - 如果条件不起作用

转载 作者:行者123 更新时间:2023-11-30 08:13:49 24 4
gpt4 key购买 nike

我对网络开发还很陌生。我有一个 login.jsp 和一个过滤器 servlet 。如果登录失败,该消息必须显示在同一页面上。

登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/home.css" rel="stylesheet">
</head>
<body class="home">
<header class="heading">
<label class="headfont">Digital Market Place</label>
</header>

<form action="./servlet1" method="POST">
<div class="form-group">
<div class="login">
<label class="loginMsg">Log in to your seller account</label>
</div>
<div class="input">
UserName: <input type="text" name="name" /><br>
<br>
</div>
<div class="input">
Password: <input type="password" name="password" /><br>
<br>
</div>

<button type="submit" id="loginbtn">Log In</button>

<%
out.println(request.getAttribute("loginResult"));

if(request.getAttribute("loginResult") != null && "true".equals(request.getAttribute("loginResult"))) {
out.println("inide if");


%>
<p style="color: red">Login Failed. Please try again.</p>
<%
}

%>
<div class="signup">
<label class="signMsg">Don&#039t have a seller account?</label>
<span style="white-space:nowrap;padding-left: 20px;"><a title="register" href="registration.jsp"> Sign up now</a></span>
</div>
</div>
</form>
</body>

我的过滤器 servlet 如下所示:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.*;

public class MyFilter implements Filter{

FilterConfig filterConfig;

public void init(FilterConfig arg0) throws ServletException {
setFilterConfig(arg0);
}


public void setFilterConfig(FilterConfig config) {
this.filterConfig = config;
}

public FilterConfig getFilterConfig() {
return filterConfig;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

PrintWriter out = response.getWriter();


String username = request.getParameter("name");
String password = request.getParameter("password");
Properties properties = new Properties();
properties.load(getFilterConfig().getServletContext().getResourceAsStream("/conf/user.conf"));
if(username.equals(properties.getProperty("username")) && password.equals(properties.getProperty("password"))){
chain.doFilter(request, response);//sends request to next resource
}
else {
request.setAttribute("loginResult", true);

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
requestDispatcher.forward(request, response);
}

}
public void destroy() {

}

}

request.getAttribute("loginResult") 在登录失败时打印“true”。但似乎控件并未进入 if 条件。

谁能帮帮忙

最佳答案

我认为你的问题是因为 request.getAttribute(String arg) 将返回 Object type 值。并且您正在调用 equals 方法来与 String 对象进行比较,因此它将始终返回 false

您需要使用toString()方法将值更改为String

将代码更改为

 if(request.getAttribute("loginResult") != null && "true".equals(request.getAttribute("loginResult").toString())) {
out.println("inide if");

关于java - 登录失败后在登录页面中显示错误消息 - 如果条件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29943725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com