Saturday, January 29, 2011

Login Example in Struts

Struts-confix.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<data-sources/>
<form-beans>
<form-bean name="LoginForm" type="com.test.LoginFormBean"/>
</form-beans>
<global-exceptions/>
<global-forwards>
<forward name="error" path="/error.jsp"/>
</global-forwards>
<action-mappings>
<action forward="/login.jsp" path="/home" type="org.apache.struts.ActionForward"/>
<action input="/login.jsp" name="LoginForm" path="/login" type="com.test.LoginAction">
<forward name="success" path="/layout.jsp"/>
<forward name="failure" path="/failure.jsp"/>
</action>
</action-mappings>
<controller/>
</struts-config>

ActionForm.java : 

package com.test;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
public class LoginFormBean extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
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 com.test;
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;

public class LoginAction extends Action {
public ActionForward execute(ActionMapping maping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
String returnParam = null;
LoginFormBean loginFormBean = (LoginFormBean)form;

String username = loginFormBean.getUsername();
String password = loginFormBean.getPassword();

System.out.println("Usename : " +username+ " Password : "+password);

if(username.equals("admin") && password.equals("admin")){
returnParam = "success";
}else{
returnParam = "failure";
}

return maping.findForward(returnParam);
}
}


web.xml :

<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<display-name>Smaple</display-name>
<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>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

index.jsp :

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>

<jsp:forward page="home.do"></jsp:forward>


login.jsp : 

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<html:form action="login.do">
User Name :<html:text property="username"></html:text> <br />
Password : <html:password property="password"></html:password> <br />
<html:submit value="Login"></html:submit> <html:cancel value="Cancel"></html:cancel>
</html:form>

layout.jsp : 

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<html:html>
<head>
<title></title>
</head>
<body>
You are in Layout Page
</body>
</html:html>

failure.jsp

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<html:html>
<head>
<title></title>
</head>
<body>
You are in Failure Page
</body>
</html:html>

No comments: