|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Trouble with struts2 validationHi all,
In my login application I want to validate whether the textfield for Usename and password is not null and also if some user enters some username say "admin" and password as "admin" it should get logged in. For this I am using postgresql database and I have already stored username and password as both as "admin. Now the problem is my Login page is able to check whether the textfield for Usename is Null or not but not able to validate with database stored username and pasword. Initially I did not check for null textfield and I was able to validate the username and password directly from database.but When I applied validate method in my action class It began to validate for null fields but not for stored username and password. I am pasting my code.. My Action class :- package struts2demo.login.action; import com.opensymphony.xwork2.ActionSupport; import java.sql.*; import java.text.*; import java.io.*; public class Login extends ActionSupport { private String userid; private String pwd; Connection db; Statement sql; DatabaseMetaData dbmd; public Login() { } public void validate() { if (getUserid().length() == 0) { addFieldError("userid", "User Name is required"); } if (getPwd().length() == 0) { addFieldError("pwd","password isrequired"); } } public String execute() throws ClassNotFoundException,SQLException { String database = "LOGIN"; String username ="vikrant"; String password ="vikrant"; System.out.println("inside action class"); Class.forName("org.postgresql.Driver"); db = DriverManager.getConnection("jdbc:postgresql:"+database,username,password ); dbmd =db.getMetaData(); System.out.println("Connection to "+dbmd.getDatabaseProductName()+" " +dbmd.getDatabaseProductVersion()+" successful \n"); System.out.println("Connected to the database"); sql=db.createStatement(); System.out.println("Now executing the command : " +"select * from my_table"); System.out.println("select * from my_table where usr = "+" '"+userid+"'"+" and pass = "+"'"+pwd+"'"); ResultSet results =sql.executeQuery("select * from my_table where usr = "+" '"+userid+"'"+" and pass = "+"'"+pwd+"'"); if (results != null) { return SUCCESS; } else return ERROR; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } } And my struts.xml is as follows:- <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2demo.login.action" extends="struts-default"> <action name="Login" class="struts2demo.login.action.Login"> <result>success.jsp</result> <result name="input">/login.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts> I am not able to understand that why the validation failed for database when I applied validation for textfield.? Could you please help me out of this..? |
|
|
Re: Trouble with struts2 validationWhat does your JSP look like?
Also, I'm not sure I understand your question. What error message are you getting? -Brian On Thu, Nov 12, 2009 at 6:16 AM, vikrant S <shimpi.vikrant@...> wrote: > > Hi all, > In my login application I want to validate whether the textfield for > Usename > and password is not null and also if some user enters some username say > "admin" and password as "admin" it should get logged in. For this I am > using > postgresql database and I have already stored username and password as > both > as "admin. Now the problem is my Login page is able to check whether the > textfield for Usename is Null or not but not able to validate with database > stored username and pasword. > Initially I did not check for null textfield and I was able to validate the > username and password directly from database.but When I applied validate > method in my action class It began to validate for null fields but not for > stored username and password. I am pasting my code.. > > > My Action class :- > package struts2demo.login.action; > import com.opensymphony.xwork2.ActionSupport; > import java.sql.*; > import java.text.*; > import java.io.*; > > > public class Login extends ActionSupport { > private String userid; > private String pwd; > Connection db; > Statement sql; > DatabaseMetaData dbmd; > > public Login() { > > } > > public void validate() { > if (getUserid().length() == 0) { > addFieldError("userid", "User Name is required"); > > } > if (getPwd().length() == 0) { > addFieldError("pwd","password isrequired"); > } > } > > public String execute() throws ClassNotFoundException,SQLException > { > String database = "LOGIN"; > String username ="vikrant"; > String password ="vikrant"; > System.out.println("inside action class"); > > Class.forName("org.postgresql.Driver"); > db = > DriverManager.getConnection("jdbc:postgresql:"+database,username,password > ); > dbmd =db.getMetaData(); > System.out.println("Connection to "+dbmd.getDatabaseProductName()+" > " > +dbmd.getDatabaseProductVersion()+" successful \n"); > System.out.println("Connected to the database"); > sql=db.createStatement(); > > System.out.println("Now executing the command : " +"select * from > my_table"); > System.out.println("select * from my_table where usr = "+" > '"+userid+"'"+" > and pass = "+"'"+pwd+"'"); > ResultSet results =sql.executeQuery("select * from my_table where > usr = > "+" '"+userid+"'"+" and pass = "+"'"+pwd+"'"); > > if (results != null) > { > return SUCCESS; > } > else > return ERROR; > > } > > > public String getUserid() { > return userid; > } > public void setUserid(String userid) { > this.userid = userid; > } > public String getPwd() { > return pwd; > } > public void setPwd(String pwd) { > this.pwd = pwd; > } > > } > > And my struts.xml is as follows:- > > <?xml version="1.0" encoding="UTF-8" ?> > <!DOCTYPE struts PUBLIC > "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" > "http://struts.apache.org/dtds/struts-2.0.dtd"> > <struts> > <package name="struts2demo.login.action" extends="struts-default"> > <action name="Login" class="struts2demo.login.action.Login"> > <result>success.jsp</result> > <result name="input">/login.jsp</result> > <result name="error">error.jsp</result> > </action> > </package> > </struts> > > I am not able to understand that why the validation failed for database > when I applied validation for textfield.? > Could you please help me out of this..? > -- > View this message in context: > http://old.nabble.com/Trouble-with-struts2-validation-tp26316586p26316586.html > Sent from the Struts - User mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@... > For additional commands, e-mail: user-help@... > > |
|
|
Re: Trouble with struts2 validationvikrant S wrote:
> Initially I did not check for null textfield and I was able to validate the > username and password directly from database.but When I applied validate > method in my action class It began to validate for null fields but not for > stored username and password. I am pasting my code.. Are you sure the validate() method you've written is doing what you think it is? If there are no field errors after the validate() method, the execute() action will run--that's just how it works. > ResultSet results =sql.executeQuery("select * from my_table where usr = > "+" '"+userid+"'"+" and pass = "+"'"+pwd+"'"); SQL injection: be wary here. > if (results != null) Is this the appropriate check, or should you be checking for a length? Have you turned up the logging levels to get a handle on what's going on behind the scenes? Dave --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
| Free embeddable forum powered by Nabble | Forum Help |