Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Login App getting redirected back to login page after login

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is my first strut app and I am still not getting it right. It's a simple login app. Where if someone uses admin/visitor credentials, he/she gets to the login page for admin/visitor. Although in my case when I type in the login details, it is coming back to login page again. Please help

1)LoginAction

package com.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.struts.form.LoginForm;;

public final class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

LoginForm loginForm = (LoginForm) form;

// validating login
if (loginForm.getUname().equals("abc")
&& loginForm.getPwd().equals("abc")) {
return mapping.findForward("admin");
} else if (loginForm.getUname().equals("sdf")
&& loginForm.getPwd().equals("sdf")) {
return mapping.findForward("visitor");
} else {
return mapping.findForward("error");
}

}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2)LoginForm

package com.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm {

private static final long serialVersionUID = 1L;
private String uname = "";
private String pwd = "";

public void setUname(String uname) {
this.uname = uname;
}

public String getUname() {

return uname;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public String getPwd() {

return pwd;
}

// validating form errors
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

if (uname == null || uname == "") {
errors.add("uname", new ActionError("error.uname.required"));
}

if (pwd == null || pwd == "") {
errors.add("pwd", new ActionError("error.pwd.required"));
}

return errors;

}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------
3)struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="LoginForm" type="com.struts.form.LoginForm"/>
</form-beans>


<action-mappings>
<action input="/login.jsp" type="com.struts.action.LoginAction" name="LoginForm" path="/login" scope="request" validate="true">
<forward name="admin" path="/admin.jsp"/>
<forward name="visitor" path="/visitor.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>

</struts-config>


------------------------------------------------------------------------------------------------------------------------------------

4)web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app >
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>











<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>


<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</web-app>

-----------------------------------------------------------

5) login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Login Page</title>
</head>
<body bgcolor="#f6f7f8">
<center>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<html:errors/>

<html:form action="login">
<table>
<tr>
<td align="right">Username:</td>
<td><html:text name="LoginForm" property="uname"/></td>
</tr>
<tr />
<tr>
<td align="right">Password:</td>
<td><html:password name="LoginForm" property="pwd"/></td>
</tr>
<tr>
<td><html:submit value="Login"/></td>
<td><html:reset value="Reset"/></td>
</tr>
</table>
</html:form>
</center>
</body>
</html>
 
Ranch Hand
Posts: 63
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By looking into code , not able to figure it out why it is not working correctly.
Its better to debug the application to know what exactly is getting wrong.
 
Bartender
Posts: 9625
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't use Struts 1 unless you have a very good reason (i.e. you are maintaining a Struts 1 application and you cannot move to an alternative). It is past End of Life and will not be updated in the future.
Struts 2 is an alternative, but it is a very different framework (it is based on WebWork) and there are many others.
 
pihu blank
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Joe! Sorry, but Struts 1 is kinda requirement over here
 
Joe Ess
Bartender
Posts: 9625
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let whoever made that requirement know that Struts 1 is unsupported and will not get security or bug fixes and that may change. Also, other, newer, frameworks also are much more productive uses of a programmer's time (form beans for instance).
As for your issue, I would assume if you are getting redirected back to your form, your validation is returning errors. It's a good idea to log method calls and the return values so you can see what's going on in your code.
By the way:

this code does not do what you think it does. see here.
 
pihu blank
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ignore.
 
I think she's lovely. It's this tiny ad that called her crazy:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic