Login form using struts

Here we develop a login form using struts framework.
 Our requirements are:
  • Develop a login form with two fields User Name and Password.
  • User Name and Password are mandatory.(validation purpose)
  • User Name and Password at least 5 digits. (validation purpose)
  • If User Name and Password are match then go to home page with welcome message otherwise give error message Invalid User Name or Password. 
So our requirements are very clear what we have to develop.Since we are developing in struts so we need some jar files and tld files:

Jar Files:
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-digester.jar
  • commons-fileupload.jar (your choice for uploading file)
  • commons-lang.jar
  • commons-logging.jar
  • commons-validator.jar
  • jakarta-oro.jar
  • struts.jar
  • struts-legacy.jar
Tld files:
  • struts-bean.tld
  • struts-html.tld
  • struts-logic.tld
  • struts-nested.tld
  • struts-template.tld
  • struts-tiles.tld
All these jar and tld files are free available so you can download and copy to all these jar files in your lib and tld in WEB-INF.

Here project name is StrutsApp Now we need following files:
  1. LoginForm.java
  2. LoginAction.java
  3. Application.properties
  4. login.jsp
  5. home.jsp
  6. web.xml
  7. struts-config.xml
So our project structure is like this


































Now we write the code of each file as 

LoginForm.java


package com.javaforecast.struts1;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public class LoginForm extends ActionForm {
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;
}
public ActionErrors validate(ActionMapping am,HttpServletRequest req){
 ActionErrors errors=new ActionErrors();
 if(username==null||username.length()==0){
  errors.add("username", new ActionError("errors.login.un.required"));
 }else if(username.length()<5||username.length()>10){
  errors.add("username", new ActionError("errors.login.un.length"));
 }
 if(password==null||password.length()==0){
  errors.add("password", new ActionError("errors.login.pw.required"));
 }else if(password.length()<5||password.length()>10){
  errors.add("password", new ActionError("errors.login.pw.length"));
 }
 return errors;
}
}


LoginAction.java

package com.javaforecast.struts1;

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

import org.apache.struts.action.*;

public class LoginAction extends Action {
public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest req,HttpServletResponse res)throws Exception{
String result="login";
LoginForm lf=(LoginForm)af;
String un=lf.getUsername();
String pw=lf.getPassword();
if(un.equals(pw)){
result="home";
req.setAttribute("UN", un);
}else{
req.setAttribute("EMSG", "Invalid Username or Password");
}
return am.findForward(result);
}
}

Application.properties

errors.login.un.required=<font color=red size=3>Username is required</font>
errors.login.un.length=<font color=red size=3>Min length is 5-digit</font>
errors.login.pw.required=<font color=red size=3>Password is required</font>
errors.login.pw.length=<font color=red size=3>Min length is 5-digit</font>

login.jsp

<%@taglib uri="WEB-INF/struts-html.tld" prefix="html"%>
<html>
<body background=green >
<br><br><br><center>
<h1>Java Forecast</h1>
<h2><font color=red size=5>LoginForm</font></h2>
<font color=red size=5>${EMSG }</font>
<html:form action="/loginSubmit">
<table>

<tr>
<td>UserName</td>
<td><html:text  property="username"/><html:errors property="username"/></td>

</tr>

<tr>
<td>Password</td>
<td><html:password  property="password"/><html:errors property="password"/></td>

</tr>

<tr>
<td> <html:submit value ="login"/></td>
</tr>
</table>
</html:form>

</body>
</html>

home.jsp

<html>
<body>
<h1>Java Forecast</h1>
<h1>Mr.${UN }! Welcome to JFC Home Page</h1>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Java Forecast</display-name>
  <welcome-file-list>
    
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
  <param-name>config</param-name>
<!-- Here the param value's content(struts-config.xml) must be your struts -config file(any name xx-config.xml)-->
  <param-value>/WEB-INF/struts-config.xml</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>
  <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

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.javaforecast.struts1.LoginForm" />
</form-beans>
<action-mappings>
<action path="/loginSubmit" 
      name="loginForm"
  type="com.javaforecast.struts1.LoginAction" scope="request" 
  input="/login.jsp">
<forward name="login" path="/login.jsp" />
<forward name="home" path="/home.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.javaforecast.struts1.Application" />
</struts-config>

After successfully follow these above steps and run the project then you will get as below mentioned output:

Login without submitting user name and password.

















Login with user name and password whose length is less than 5-digits.




























Login with invalid user name and password




































Now finally user name and password same we get final requirement as






















Thanks, I hope you enjoyed with this tutorial.

0 comments :

Post a Comment