Struts 1 login application example error -
i new struts , tried execute login form. it's not getting executed after default constructor in action class.
loginform.java
package struts.login.action; import org.apache.struts.action.actionform; @suppresswarnings("serial") public class loginform extends actionform { public loginform() { } private string username; private string password; public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } } loginaction.java
package struts.login.action; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import org.apache.struts.action.action; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.action.actionmapping; public class loginaction extends action { private final static string success = "success"; private final static string failure = "failure"; //private final static string failure = "failure"; public loginaction() { system.out.println("default constructor of login action"); } @override public actionforward execute(actionmapping mapping, actionform form, servletrequest request, servletresponse response) throws exception { loginform loginform = (loginform) form; if(loginform.getusername().equals(loginform.getpassword())){ return mapping.findforward(success); } else{ return mapping.findforward(failure); } } } login.jsp
<%@ taglib uri="/web-inf/tld/struts-html.tld" prefix="html"%> <html> <head> <title>login page</title> </head> <body> <div style="color:red"> </div> <html:form action="/login" method="get"> user name :<html:text property="username"/><br> password :<html:password property="password"/> <html:submit value="login here" /> </html:form> </body> </html> success.jsp
<html> <head> <title>insert title here</title> </head> <body> logged in! </body> </html> failure.jsp
<html> <head> <title>insert title here</title> </head> <body> login failed please try again! </body> </html> struts-config.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.3//en" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="loginform" type="struts.login.action.loginform"> </form-bean> </form-beans> <action-mappings> <action path="/login" name="loginform" type="struts.login.action.loginaction" > <forward name="success" path="/success.jsp"></forward> <forward name="failure" path= "/failure.jsp"></forward> </action> </action-mappings> </struts-config> web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app id="webapp_id" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- standard actionservlet configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.actionservlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/web-inf/struts-config.xml</param-value> </init-param> <init-param> <param-name>application</param-name> <param-value>applicationresources</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- standard actionservlet mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- struts tag library descriptors --> <jsp-config> <taglib> <taglib-uri>/web-inf/tld/struts-html.tld</taglib-uri> <taglib-location>/web-inf/tld/struts-html.tld</taglib-location> </taglib> </jsp-config> </web-app> i have included struts-html.tld file in web-inf/tld folder. after login username , password it's not redirected either success.jsp or failure.jsp page
it because have passed servletrequest , servletresponse in execute method in action class instead of httpservletrequest , httpservlet response.. use following
public actionforward execute(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception { } and import these things:
import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; hope work..
Comments
Post a Comment