gpt4 book ai didi

java - Struts2登录MySQL错误

转载 作者:行者123 更新时间:2023-11-28 23:36:36 24 4
gpt4 key购买 nike

我已经为 Struts2 创建了一个简单的登录应用程序,但我无法使用本地 MySQL 数据库登录。我的代码如下:

登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<s:form action="loginprocess">
<s:textfield name="username" label="Username"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit cssStyle="float:left;" value="Login"></s:submit>
</s:form>
</body>
</html>

登录错误.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</title>
</head>
<body>
Sorry username or password error!
<jsp:include page="login.jsp"></jsp:include>
</body>
</html>

ma​​in.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>My Page</title>
</head>
<body>
<h2>Welcome!</h2>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="loginprocess" class="com.login.struts2.LoginTest">
<result name="success">main.jsp</result>
<result name="error">loginerror.jsp</result>
</action>
</package>
</struts>

LoginDao.java

package com.login.struts2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginDao {
public static boolean validate(String username, String password){
boolean status=false;
Connection conn = null;
try
{
String URL = "jdbc:mysql://localhost:3306/struts_register2";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, "root", "1235");
PreparedStatement ps=conn.prepareStatement("select * from test where username=? AND password=?");
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
status = rs.next();
}
catch(Exception e){e.printStackTrace();}
return status;
}
}

LoginTest.java

package com.login.struts2;

public class LoginTest {
private String username,password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}

public String getUserpass() {
return password;
}
public void setUserpass(String password) {
this.password = password;
}

public String execute(){
if(LoginDao.validate(username, password)){
return "success";
}
else{
return "error";
}
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

如果我尝试更改 LoginDao.java 中的查询,使用 OR 而不是 AND,它会将我重定向到 主.jsp。我找不到解决方案。我的数据库中包含用户名和密码为 “test”(VARCHAR(10))的字段。有什么想法吗?

最佳答案

密码输入字段的名称为password

<s:password name="password" label="Password"></s:password>  

那么setter应该同名。

public void setPassword(String password) {  
this.password = password;
}

以这种方式,参数将被填充,您将不会有 null 值。

关于java - Struts2登录MySQL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587948/

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