我只想在 javaBean 类中运行一个方法(它不是 getter 或 setter 方法),它需要 2 个参数(用户名、密码)并返回一个布尔值(在从数据库中改变它之后(单独的 DAO 类))。
然后我想根据该布尔值做出决定以显示一些文本或重定向页面。我该怎么做。?我需要 JSTL 或 jsp:action(useBean 和设置/获取属性)标记还是足够了?
.jsp 页面中没有 SCRIPTLETS 或 java 代码。没有任何框架,如 struts 或 spring 任何实用代码?
索引.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Open-Pages</title>
</head>
<body>
<form method="post" action="/open-pages/comments.jsp">
User Name: <input type="text" name="userName">
<br />
Password: <input type="password" name="password">
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
评论.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
// out.println(request.getParameter("userName"));
// out.println(request.getParameter("userName"));
%>
<jsp:useBean id="verifyUser" class="entry.UserBean" scope="page" />
<jsp:setProperty name="verifyUser" property="userName" param="userName" />
<jsp:setProperty name="verifyUser" property="password" param="password" />
<jsp:getProperty name="verifyUser" property="userName" />
<jsp:getProperty name="verifyUser" property="password" />
</body>
</html>
用户Bean.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package entry;
import java.io.Serializable;
/**
*
* @author user1
*/
public class UserBean implements Serializable{
private String userName;
private String password;
private boolean validUser;
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
public boolean getValidUser(String user, String password) {
if(entry.userAuthDAO.verifyUserPass(user,password)){
return true;
}else {
return false;
}
}
public void setValidUser(boolean validUser) {
this.validUser = validUser;
}
}
UserAuthDAO
package entry;
import entry.Constants;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user1
*/
public class userAuthDAO {
static String authUser = "select userName, password "
+ "from users "
+ "where userName = ? and password = ?";
public static boolean verifyUserPass(String userName, String password){
try {
Connection con = DriverManager.getConnection(Constants.DBHost, userName, password);
PreparedStatement pStmt = con.prepareStatement(authUser);
pStmt.setString(1,userName);
pStmt.setString(2, password);
ResultSet rs = pStmt.executeQuery();
String rsUserName = null;
String rsPassword = null;
if(rs.next()){
rsUserName = rs.getString("userName");
rsPassword = rs.getString("password");
}
con.close();
if (rsUserName.equals(userName) && rsPassword.equals(password)){
return true;
}
} catch (SQLException ex) {
Logger.getLogger(userAuthDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
}