sambot sambot -3 years ago 105
Java Question

In struts 1.3 how to retrieve data from database and display it using DAO

*M new to struts. I am making simple login page that display username and password by retrieving it from database. I m using DAO.
I have, and Displaydata.jsp pages. *

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

return false;


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();

return mapping.findForward("success");}
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.

Answer Source

Right. Some time ago I built an application with Struts 1.x and MySql with login.


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);


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 ( {
            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();
    return user;

In my case I have a table user with mail as a primary key. There are various forms.

More examples:

e.g. For show the name of variable user in session scope from the database:


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


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="" prefix="bean"%>

<bean:write scope="session" name="user" property="name" filter="false" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download