java - In struts 1.3 how to retrieve data from database and display it using DAO -
*m new struts. making simple login page display username , password retrieving database. m using dao. have logindao.java, loginaction.java , displaydata.jsp pages. *
logindao.java
public boolean login(string user,string pass) throws sqlexception { connection con = getconnection(); statement st; seek { st = con.createstatement(); st.executequery("select * login username='" + user + "' , password='" + pass + "'"); homecoming true; } grab (sqlexception e1) { // todo auto-generated grab block e1.printstacktrace(); } homecoming 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"); homecoming mapping.findforward("success");} else {system.out.println("not"); homecoming mapping.findforward("failure"); } } }
and write in dislpaydata.jsp display username , password in dont want java code in it. thankyou
right. time ago built application struts 1.x , mysql login.
loginactionpublic actionforward login( ... ) throws exception { string forward; final string mail service = propertyutils.getproperty(form, "mail"); final string password = propertyutils.getproperty(form, "password"); if (loginservice.getinstance().validate(mail, password)) { // e.g. set name of user in session forwards = success; } else { forwards = error; } homecoming mapping.findforward(forward); }
loginservice public boolean validate(final string mail, final string password) throws serviceexception { seek { final boolean valid; // validate null , empty // validate 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; } } homecoming valid; } grab (daoexception e) { throw new serviceexception("error validating user , password.", e); } }
userdao private static final string find_by_pk_sql = "select mail, name, password, admin user mail service = ?"; public user findbypk(final string mail) throws daoexception { connection conn = null; preparedstatement ps = null; resultset rs = null; seek { conn = getconnection(); ps = conn.preparestatement(find_by_pk_sql); ps.setstring(1, mail); // pk, not null rs = ps.executequery(); if (rs.next()) { homecoming fill(rs); } homecoming null; } grab (final sqlexception e) { throw new daoexception(e); } { // 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")); homecoming user; }
in case have table user
mail
primary key. there various forms.
more examples:
building login application struts login application using action form tutorial | dzone creating email login web application strutse.g. show name of variable user in session scope database:
loginactionif (loginservice.getinstance().validate(mail, password)) { final httpsession session = request.getsession(); final user user = userservice.getinstance().getuser(mail); session.setattribute("user", user); forwards = success; }
home.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 database struts dao struts-1
No comments:
Post a Comment