How to Develop login form in struts

Source: Internet
Author: User

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 translateHttpservletrequestTo the business logic.
In this example our action class is loginaction. java. This action class only forwards the login. jsp. Our action class returnsActionforwardCalled "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 pal_sushil@hotmail.com
*@ 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) form;
If (loginform. GetUserName (). inclusignorecase ("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 returnActionforwardInstance describing where and how control shocould 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 extendsOrg. 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. theActionformClass 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 pal_sushil@hotmail.com
* @ 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
{< br> private string action = "add";
private string username = NULL;
private string Password = NULL;
private string usertype = NULL;
Public void setaction (string action) {
This. action = action;
}< br> Public String getaction () {
return this. action;
}

Public void setusername (string username) {
This. username = username;
}< br> Public String GetUserName () {
return this. username;
}< br> Public void setpassword (string password) {
This. password = password;
}< br> Public String GetPassword () {
return this. password;
}< br> Public void setusertype (string usertype) {
This. usertype = usertype;
}< br> Public String getusertype () {
return this. usertype;
}< br> Public void reset (actionmapping mapping, httpservletrequest request) {
This. username = NULL;
This. password = NULL;
This. usertype = NULL;
This. action = "add";
}< BR >}

View part of login form example:
The view portion of a Web application is most often constructed using extends erver 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

<% @ Page import = "Java. Io. *" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "javax. Naming. *" %>
<% @ Page 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" color = "#000080"> You are successfully loged in as <font color = "red"> <% = usertype %> </font> </P>
</Body>
</Html>

Failure. jsp

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


<Bean: Message key = "welcome. title "/>



sorry your USERID/password is incurrect


retry click here!



controller part of login form example:
the Controller part is focused on processing ing requests from a Web browser, deciding what business logic function is to be performed med, 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 --------- section.
name = "loginform"
type = "roseindia. web. struts. action. loginform "/>

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 shoshould look like:

Now click on the linkLogin Page exampleThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.