73006

Struts 1 login application example error

Question:

I am new to Struts and tried to execute Login Form. But it's not getting executed after the default constructor in the ACTION class.

<hr />

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> Successfully 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 with username and password It's not redirected to either success.jsp or failure.jsp page

Answer1:

It is because you have passed ServletRequest and ServletResponse in execute method in action class instead of HttpServletRequest and HttpServlet Response.. Use Like 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 it will work..

Recommend

  • How to retrieve the data from the database and display it on a jsp using Struts and Hibernate
  • Populate a list element on to jsp using display table
  • Regex: Match everything except backreference
  • Get Users in Group from Azure AD via Microsoft Graph
  • Why == is different for Integer and String?
  • Type mismatch: cannot convert from Connection to Connection
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • Where should I store a file in Android?
  • Dynamically generated lookup key for IQueryable
  • Spring MVC redirect with custom http headers
  • How to fail Phing without triggering backtrace
  • Linq Full Outer Join on Two Objects
  • How to get the index of element in the List in c#
  • Is it mandatory to have a doGet or doPost method?
  • Authentication failed with Azure Active Directory in Windows Phone
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • How to use RequestBodyAdvice
  • Time complexity of a program which involves multiple variables
  • JSON response opens as a file, but I can't access it with JavaScript
  • Array.prototype.includes - not transformed with babel
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • javascript inside java/jsp code
  • Symfony2: How to get request parameter
  • jQuery tmpl and DataLink beta
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • Android Studio and gradle
  • SQL merge duplicate rows and join values that are different
  • AngularJs get employee from factory
  • log4net write single file for each call to log.info
  • Proper way to use connect-multiparty with express.js?
  • Getting error when using KSoap library to consume .NET web services
  • How to set the response of a form post action to a iframe source?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal