Saturday, July 16

Loginform using Struts

Total files --* 6
1. loginpage.jsp
2. success.jsp
3. LoginActionForm.java (in LoginForm package)
4. LoginAction.java (in LoginAction package)
5. struts-config.xml (in WEB-INF folder) mapping file
6. web.xml (in WEB-INF folder)
__________________________________________________________________________
step 1 --* loginpage.jsp
__________________________________________________________________________

~%@page contentType="text/html" pageEncoding="UTF-8"%*
~%@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*
~body*
~center*~h1*Welcome to Login Page~/h1* ~/center*
~html:form action="LoginAction" method="post"*
Name: ~html:text property="username"*~/html:text*
~br* ~br*
Password: ~html:password property="password"*~/html:password*
~br*
~html:submit value="Login"*~/html:submit*
~/html:form*
~/body*
~/html*
_________________________________________________________________________
step 2 --* success.jsp
_________________________________________________________________________
~%@page contentType="text/html" pageEncoding="UTF-8"%*
~!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"*

~html*
~body*
~center*~h1*You have successfully Login....!!~/h1* ~/center*
~/body*
~/html*
__________________________________________________________________________
step 3 --* LoginActionForm.java
__________________________________________________________________________
package LoginForm;
public class LoginActionForm extends org.apache.struts.action.ActionForm
{
String username;
String password;

public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public LoginActionForm()
{
super(); // TODO Auto-generated constructor stub
}
}
__________________________________________________________
step 4 --* LoginAction.java
__________________________________________________________
package LoginAction;
import LoginForm.LoginActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends org.apache.struts.action.Action
{
private static final String SUCCESS = "success";
private static final String FAILURE="failure";

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
LoginActionForm laf = (LoginActionForm) form;
String username=laf.getUsername();
String password=laf.getPassword();
if(username.equals("")&& password.equals(""))
{
mapping.findForward(FAILURE);
}
else if(username.equals("prembharti")&& password.equals("school"))
{
return mapping.findForward(SUCCESS);
}
return mapping.findForward(FAILURE);
}
}
_________________________________________________________________
step 5 --* struts-config.xml
_________________________________________________________________
~?xml version="1.0" encoding="UTF-8" ?*

~!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"*

~struts-config*
~form-beans*
~form-bean name="LoginActionForm" type="LoginForm.LoginActionForm"/*

~/form-beans*

~global-exceptions* ~/global-exceptions*
~global-forwards*
~forward name="LoginAction" path="/LoginAction.do"/*
~/global-forwards*

~action-mappings*
~action input="/loginpage.jsp" name="LoginActionForm" path="/LoginAction" scope="request" type="LoginAction.LoginAction" validate="false"*
~forward name="success" path="/success.jsp"/*
~forward name="failure" path="/loginpage.jsp" /*
~/action*

~/action-mappings*

~controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/*

~message-resources parameter="com/myapp/struts/ApplicationResource"/*
~plug-in className="org.apache.struts.tiles.TilesPlugin" *
~set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /*
~set-property property="moduleAware" value="true" /*
~/plug-in*

~!-- ==== Validator plugin =====--*
~plug-in className="org.apache.struts.validator.ValidatorPlugIn"*
~set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/*
~/plug-in*
~/struts-config*
__________________________________________________________________
step 6 --* web.xml (Set in welcome file --* loginform.jsp
__________________________________________________________________
~?xml version="1.0" encoding="UTF-8"?*
~web-app 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"*
~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*debug~/param-name*
~param-value*2~/param-value*
~/init-param*
~init-param*
~param-name*detail~/param-name*
~param-value*2~/param-value*
~/init-param*
~load-on-startup*2~/load-on-startup*
~/servlet*
~servlet-mapping*
~servlet-name*action~/servlet-name*
~url-pattern**.do~/url-pattern*
~/servlet-mapping*
~session-config*
~session-timeout*
30
~/session-timeout*
~/session-config*
~welcome-file-list*
~welcome-file*loginpage.jsp~/welcome-file*
~/welcome-file-list*
~jsp-config*
~taglib*
~taglib-uri*/WEB-INF/struts-bean.tld~/taglib-uri*
~taglib-location*/WEB-INF/struts-bean.tld~/taglib-location*
~/taglib*
~taglib*
~taglib-uri*/WEB-INF/struts-html.tld~/taglib-uri*
~taglib-location*/WEB-INF/struts-html.tld~/taglib-location*
~/taglib*
~taglib*
~taglib-uri*/WEB-INF/struts-logic.tld~/taglib-uri*
~taglib-location*/WEB-INF/struts-logic.tld~/taglib-location*
~/taglib*
~taglib*
~taglib-uri*/WEB-INF/struts-nested.tld~/taglib-uri*
~taglib-location*/WEB-INF/struts-nested.tld~/taglib-location*
~/taglib*
~taglib*
~taglib-uri*/WEB-INF/struts-tiles.tld~/taglib-uri*
~taglib-location*/WEB-INF/struts-tiles.tld~/taglib-location*
~/taglib*
~/jsp-config*
~/web-app*

No comments: