gpt4 book ai didi

java - 在 struts 1.3 中如何从数据库中检索数据并使用 DAO 显示它

转载 作者:搜寻专家 更新时间:2023-10-30 23:15:28 25 4
gpt4 key购买 nike

*我是 struts 的新手。我正在制作简单的登录页面,通过从数据库中检索来显示用户名和密码。我正在使用 DAO。 我有 LoginDAO.java、LoginAction.java 和 Displaydata.jsp 页面。 *

LoginDAO.java

 public boolean login(String user,String pass) throws SQLException
{
Connection con = getConnection();

Statement st;
try {
st = con.createStatement();



st.executeQuery("select * from login where Username='" + user + "' and Password='" + pass + "'");


return true;
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

return false;

}

LoginAction.java

public class LoginAction extends Action

{

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {



DynaValidatorForm rf= (DynaValidatorForm) form;

String username = rf.get("username").toString();
String password = rf.get("password").toString();
HttpSession session=request.getSession();
session.setAttribute("user", username);

Login dao= new Login();

if(dao.login(username,password))
{
System.out.println("GOT");
return mapping.findForward("success");}
else
{System.out.println("NOT");
return mapping.findForward("failure");
}
}
}

and also what do i write in Dislpaydata.jsp to display username and password in it dont want any java code in it. Thankyou

最佳答案

没错。前段时间我使用 Struts 1.x 和 MySql 构建了一个带有登录的应用程序。

登录 Action

public ActionForward login( ... ) throws Exception {
String forward;
final String mail = PropertyUtils.getProperty(form, "mail");
final String password = PropertyUtils.getProperty(form, "password");
if (LoginService.getInstance().validate(mail, password)) {
// Do something e.g. put name of user in session
forward = SUCCESS;
} else {
forward = ERROR;
}
return mapping.findForward(forward);
}

登录服务

public boolean validate(final String mail, final String password) 
throws ServiceException {
try {
final boolean valid;

// Validate null and empty

// Validate with DB
final UserDAO dao = new UserDAO();
final User user = dao.findByPk(mail);
if (user == null) {
valid = false;
} else {
if (password.equals(user.getPassword())) {
valid = true;
} else {
valid = false;
}
}
return valid;
} catch (DAOException e) {
throw new ServiceException("Error validating user and password.", e);
}
}

UserDAO

private static final String FIND_BY_PK_SQL 
= "SELECT mail, name, password, admin FROM user WHERE mail = ?";

public User findByPk(final String mail) throws DAOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getConnection();
ps = conn.prepareStatement(FIND_BY_PK_SQL);
ps.setString(1, mail); // PK, NOT NULL
rs = ps.executeQuery();
if (rs.next()) {
return fill(rs);
}
return null;
} catch (final SQLException e) {
throw new DAOException(e);
} finally {
// Close DB resources
}
}

private User fill(final ResultSet rs) throws SQLException {
final User user = new User();
user.setMail(rs.getString("mail"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setAdmin(rs.getBoolean("admin"));
return user;
}

在我的例子中,我有一个表 user,其中 mail 作为主键。有多种形式。

更多例子:


例如从数据库中显示 session 范围内变量用户的名称:

登录 Action

if (LoginService.getInstance().validate(mail, password)) {
final HttpSession session = request.getSession();
final User user = UserService.getInstance().getUser(mail);
session.setAttribute("user", user);
forward = SUCCESS;
}

首页.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

Welcome,
<bean:write scope="session" name="user" property="name" filter="false" />

关于java - 在 struts 1.3 中如何从数据库中检索数据并使用 DAO 显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14716416/

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