Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

How To Develop Login Form In Struts

Apache Struts is an open-source framework for developing J2EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt an MVC architecture.

Ads

Tutorials   
Struts 2 Tutorial and Examples Struts 2.5.10.1 General Availability - released Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 version 2.3.16.3 released Struts 2 version 2.3.15.3 released Struts 2 Hello World Annotation Example Struts Roseindia Struts 2 Tutorial: Easy steps to learn Struts 2 Difference between Struts and Spring Apache Struts 2.3.15.2 launched to fix vulnerable Feature Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts 2.3.15.1 Maven - How to use Maven dependency of Struts 2.3.15.1? Download Struts 2.3.15.1 How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts 2.3.15.1 Struts 2 Tutorials - Struts version 2.3.15.1 Struts 1 Tutorial and example programs Struts Tutorial What is Struts Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts Video Tutorials Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 In Struts What is Model? What is Struts? Download Struts What is Struts Framework? Struts Framework Latest Version of Struts Framework Downloading Struts Official example Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Hibernate4 Struts Integration
Ads

How To Develop Login Form In Struts

     

Apache Struts  is an open-source framework for developing J2EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt an MVC architecture. It was originally created by Craig McClanahan and donated to the Apache Foundation in May, 2000.

This article  will explain how to develop login form in struts. Struts adopts an MVC  architecture.

Model Part of Login form Example:
Model is basically data of you application. A collection of Java beans and other helper classes that you use for  your application
In Login Form Example ,the following classes will be used--

  1. Action class
  2. ActionForm class 

Action Class:
The action class  is the link between the Struts framework and your business application logic. The purpose of Action Class is to translate the HttpServletRequest to the business logic.

In this example our Action class is LoginAction.java.  this Action class only forwards the login.jsp. Our Action class returns the ActionForward  called "loginAction", which is defined in the struts-config.xml file . Here is code of our Action Class:

LoginAction.java

package roseindia.web.struts.action;
/**
* @Author Sushil Pal
* @Web http://www.roseindia.net
* @Email [email protected] 
* @Copyright RoseIndia.net  
*/
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 mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

LoginForm loginform = (LoginForm) form;
if (loginform.getUsername().equalsIgnoreCase("username") && 
   loginform.getPassword().equals("password")) {
// we are in
   return mapping.findForward("success");
} else {
// not allowed
   return mapping.findForward("failure");
}
}
}

Action Class process the specified HTTP request, and create the corresponding HTTP response and Return an ActionForward instance describing where and how control should be forwarded. In our login action class if username field value will be 'username' and password will be 'password' then the mapping will forward to success.jsp otherwise to failure.jsp.

ActionForm Class:
An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side. It must have setters and getters for all the form fields.The ActionForm class is pre-populated with form data when the form is submitted.
In this example our ActionForm class is LoginForm.java. Here is code of our ActionForm Class:

LoginForm.java

package roseindia.web.struts.action;

/**
* @Author Sushil Pal
* @Web http://www.roseindia.net
* @Email [email protected] 
* @Copyright RoseIndia.net  
*/
/**
 * Form bean for the Login Entry Screen.
 *
*/

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public class LoginForm extends ActionForm
{
  private String action="add";
  private String username=null; 
  private String password=null;
   private String usertype=null;

public void setAction(String action){
  this.action=action;
}

public String getAction(){
   return this.action;
}

public void setUsername(String username){
  this.username=username;
}

public String getUsername(){
  return this.username;
}

public void setPassword(String password){
   this.password=password;
}

public String getPassword(){
   return this.password;
}

public void setUsertype(String usertype){
   this.usertype=usertype;
}

public String getUsertype(){
   return this.usertype;
}

public void reset(ActionMapping mapping,
HttpServletRequest request){

   this.username=null;
   this.password=null;
   this.usertype=null;
   this.action="add";

}

View Part of Login form Example:
The View portion of a web application is most often constructed using JavaServer Pages (JSP) technology. JSP pages can contain static HTML (or XML),plus the ability to insert dynamic content based on the interpretation of special action tags.
In this example our JSP pages are login.jsp, success.jsp, failure.jsp. Here is code of our JSP pages:
login.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<html:html locale="true">
<head>
<title><bean:message key="welcome.title"/></title>
<html:base/>

<script language="javascript">
function validate(objForm){

if(objForm.username.value.length==0){
alert("Please enter UserID!");
objForm.username.focus();
return false;
}

if(objForm.password.value.length==0){
alert("Please enter Password!");
objForm.password.focus();
return false;
}

if(objForm.usertype.selectedIndex == 0 ){

alert("Please Select User Type!");
objForm.usertype.focus();
return false;
}

return true;
}
</script>

</head>

<body bgcolor="white">


<html:form action="/login" method="post"
 onsubmit="return validate(this);"><center>

<table width="400" border="1" align="center"
cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellspacing="2"
cellpadding="1" width="100%" >
<tr bgcolor="#eaeac8">
<td align="left" colspan="2"><font size="5">
User Login</font></td></tr> 

<tr><td colspan="2"><p>&nbsp;</p></td></tr>

<tr align="center">
<td align="right">User ID:</td> 
<td><html:text property="username" size="30"
maxlength="30"/></td></tr> 

<tr align="center">
<td align="right">Password:</td> 
<td><html:password property="password" size="30"
maxlength="30"/></td>
</tr> 

<tr align="center">
<td align="right">Login As:</td> 
<td> 
<html:select property="usertype">
<option>---------------Login As---------------</option>
<option value="Buyer">Buyer</option>
<option value="Seller">Seller</option>
<option value="Guest">Guest</option>
</html:select>
</td>
</tr> 

<tr><td colspan="2"><p>&nbsp;</p></td></tr>

<tr>
<td align="center" colspan="2"><html:submit>
Login Now !</html:submit></td></tr></table>
</td>
</tr>
</table>

</center>


</html:form>
<body>
</html:html>


success.jsp

<[email protected] import="java.io.*"%>
<[email protected] import="java.sql.*"%>
<[email protected] import="javax.sql.*"%>
<[email protected] import="java.util.*"%>
<[email protected] import="javax.naming.*"%>
<[email protected] import="javax.servlet.*"%>

<%
//getting value
String username=request.getParameter("username");
String password=request.getParameter("password");
String usertype=request.getParameter("usertype");
%>

<html>
<head>
<title>Success</title>
</head>
<body>
<p> Hi , <%=username%><p>
<p align="center"><font size="3" >You Are Successfully
Loged in as <font color="red"><%=usertype%></font>
</font></p></body></html>

failure.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<html:html locale="true">
<head>
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>

<body bgcolor="white">

<p align="center"><font size="3" color="red">
Sorry your UserId/Password is incurrect</font></p>
<center><p><html:link page="/pages/login.jsp"><font size="3"
 color="blue">Retry click here!</font></html:link></p></center>
</body>
</html:html>

Controller Part of Login form Example:
The Controller part is focused on receiving requests from a  web browser, deciding what business logic function is to be performed, and then delegating responsibility for producing the next phase of the user interface to an appropriate View component. The main component of the Controller in the framework is a servlet of class ActionServlet. This servlet is configured by defining a set of ActionMappings.Defining the mapping for our example. Put the following code in struts-config.xml.
Put the following codes within the <form-beans> --------- </form-beans> section.
<form-bean
  name="LoginForm"
  type="roseindia.web.struts.action.LoginForm"/>
</form-beans>

Put the following codes within the <action-mappings> --------- </action-mappings> section.

<action
  path="/login"
  type="roseindia.web.struts.action.LoginAction"
  name="LoginForm"
  scope="request"
  validate="true"
  input="/pages/login.jsp">
  <forward name="success" path="/pages/success.jsp"/>
  <forward name="failure" path="/pages/failure.jsp"/>
</action>

How to run this Example:

Installing Struts Application:

Download latest version of Struts from the site of Struts http://jakarta.apache.org/struts. Copy struts-blank.war to tomcat webapps directory. Start tomcat, Tomcat automatically extracts the file and loads the application. Download the code from here and then integrate it with the struts-blank application. Make necessary changes in the struts-config.xml, and compile application using ant. The restart the tomcat. Now your login application is ready. Now open Internet Explorer and type http://localhost:8080/struts-blank/ and press Enter. You browser window should look like:

Now click on the link  Login Page Example the following window will open...
  
Now type User Id as 'username' and password as 'password' and select a login option then press login button. The success page will display.

Hi , username

   You Are Successfully Loged in as Buyer

 If you enter incorrect user id and password the failure page will display.

   Sorry your UserId/Password is incorrect

    Retry click here!

In this tutorial you have learned how to develop simple login application with struts.

Advertisements

Ads
Share on Google+Share on Google+

How To Develop Login Form In Struts

Posted on: January 8, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: How To Develop Login Form In Struts   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
sunny
April 5, 2011
re:web.xml

wat abt web.xml file....
harish
January 26, 2012
comment

awesome code

Ads

 

Ads