Login Page code using struts 1.3 example

Building applications using Struts adopts model view controller (MVC) architecture which is used to separate the model (application logic that interacts with a database) from the view (HTML / JSP) and the controller which helps to maintain / organise the code powerful & easily. Now we are going to build a Login Page using Struts. The following files are used to build the Login application using struts .

login.jsp – > JSP page which accepts Login Name & Password

success.jsp – > If the login name & password entered matches with the database username & password , the success.jsp is loaded

web.xml – > Action servlet , struts-config.xml , welcome html / jsp and more is defined in web.xml file.

struts-config.xml – > Contains the XML which is used to configure struts frame work for login application.

LoginAction.java – > contains code to process the request from the login page.

LoginForm.java – > this is a class file which is extended by ActionForm class that captures and transfers data to and from login page.

ApplicationResource.properties – > Contains error messages, strings, labels. Can be changed without recompile.

Now let us see the actual code involved in these files to develop login page application . In this application , ActionForm , Dispatch Action are used.

login.jsp

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
 <HTML >
 <HEAD >
 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
 <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" % >
 <TITLE >login.jsp </TITLE >
 </HEAD >
 <script language="javascript" >
 function submitForm()
 { 
 document.loginForm.method.value="processLogin";
 document.loginForm.submit();
 }
 </script >
 <body > 
 <html:form name="loginForm" type="test.javaonline.forms.LoginForm" method="post" scope="request" action="/login.do" onsubmit="return submitForm()" > 
 <html:hidden property="method" value="processLogin" / >
 <html:errors / > 
 Enter Login Name : <html:text name="loginForm" property="loginId" / > 
 Enter Password : <html:password name="loginForm" property="passWord" / > 
 <html:button property="dfd" value="Login" onclick="submitForm()" / >
 </html:form > 
 </body >
 </HTML >
 

In the above page one tage <html:errors/ > which is used to display errors returned by the server.

web.xml : ( to mention Action servlet , struts-config.xml , welcome html / jsp)

if we want to use the login.jsp as a wecome page , then it is to defined in web.xml as follows.

<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>
  .......
  ......
</servlet>  

<welcome-file-list >
 <welcome-file >login.jsp </welcome-file >
 </welcome-file-list >

struts-config.xml

struts-config.xml binds together model, view and controller. It contains XML to configure the struts for the login application.

 <struts-config >
 <form-beans >
 <form-bean name="loginForm" type="test.javaonline.forms.LoginForm" >
 </form-bean >
 </form-beans >

 <action name="loginForm" scope="request" path="/login" type="test.javaonline.actions.LoginAction" validate="true" input="login.jsp" parameter="method" >
 <forward name="success" path="/success.jsp" > </forward >
 <forward name="failure" path="/login.jsp" > </forward >
 </action >
 </action-mappings >
 <!-- Message Resources -- >
 <message-resources parameter="test.javaonline.resources.ApplicationResources"/>
 </struts-config >

In the above lines , LoginForm is the ActionForm. validate=true sothat validate method in the LoginForm is called for the login.jsp , parameter=”method” is used to pass the method in the action class to be called as we are using dispatchaction.

LoginForm.java

LoginForm is the Action Form (bean) whic is used to declare form fields and define getter / setter methods of the fields that captures and transfers data to and from the login page. validate method also defined in the action class which is automatically called to validate the fields declared in the class. reset method is used to reset the values of the fields declared in the action form class.

package test.javaonline.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm {
  private String loginId="";
  private String passWord="";
  private String method="";
 public void reset(ActionMapping mapping, HttpServletRequest request) {
     loginId="";
     passWord="";
 }
 public ActionErrors validate( ActionMapping mapping,  HttpServletRequest request) {
  ActionErrors errors = new ActionErrors();
  if ((loginId == null) || (loginId.length() == 0)) {
    errors.add("loginId", new org.apache.struts.action.ActionError("errors.loginId.required"));
   }
   if ((passWord == null) || (passWord.length() == 0)) {
   errors.add("passWord", new org.apache.struts.action.ActionError("errors.passWord.required"));
    }
    return errors;
 }
 public String getLoginId() {
   return loginId;
 }
 public String getPassWord() {
  return passWord;
 }
 public void setLoginId(String string) {
  loginId = string;
 }
 public void setPassWord(String string) {
  passWord = string;
 }
 public String getMethod() {
  return method;
 }
 public void setMethod(String string) {
  method = string;
 }
}

LoginAction.java

Contains the class code which process the requests from the login page. Action form is passed to this method (processLogin in our example) , the values are processed by application logic that interacts with a database which is is called from here.

package test.javaonline.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import test.javaonline.forms.LoginForm;

public class LoginAction extends DispatchAction {
 public ActionForward processLogin(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception {
  ActionErrors errors = new ActionErrors();
  ActionForward forward = new ActionForward();
  LoginForm loginForm = (LoginForm) form;
  LoginDAO loginDAO = new LoginDAO();
  String target="";
  try {
     String loginid=loginForm.getLoginId();
     String password=loginForm.getPassWord();
    // loginValidate has Data access logic, password verification logic 
     boolean success = loginDAO.loginValidate(loginid, password);     
      if (success)
        {
    // Statements for Session creation and Others .Also business logic funtion /          Data access function  is called from here 
       }
      else
       {
        errors.add("invalidlogin", new ActionError("errors.invalid.login"));
       }
    } catch (Exception e) {
      errors.add("name", new ActionError("id"));
    }
  if (!errors.isEmpty()) {
    saveErrors(request, errors);
    System.out.println("In Error");
    forward = mapping.findForward("failure");
    } else {
      System.out.println("success");
      forward = mapping.findForward("success");
    }
  return (forward);
 }
}

ApplicationResource.properties

ApplicationResource.properties – > Contains error messages , strings , labels . Can be changed without recompile

errors.loginId.required = <h5 > <font color="red" >Login Name is required. </font > <h5 >
errors.passWord.required = <h5 > <font color="red" > Password is required </font > <h5 >
errors.invalid.login = <h5 > <font color="red" > Invalid Login </font > <h5 >

These errors are added to ActionErrors object .For example Suppose if you give login name blank, then the error “errors.loginId.required” is added . The errors are displayed in the JSP using <html:errors/> tag

Output :

login.jsp

1. if we enter login name blank &amp; password blank , the error message is displayed Login Name is required & Password is required

2. if we enter login name and password blank , the error message displayed is Password is required

3. if we enter login name & password , then it matches with database login name &amp; password , if both matches , then success otherwise the error message “invalid login” is displayed.

Leave a Reply

weight loss diet while breastfeeding best testosteroneand weight loss supplement weight loss diet recipe keto diet pills vitamin shoppe weight loss pills for truck drivers over the counter pills to lose weight new celebrity weight loss pills all natural weight loss pills that work mct ketogenic diet weight loss nausea and fatigue after weight loss diet nutless keto diet recipes d4 weight loss supplement vegetarian recipes for keto diet diet plan for weight loss and flat belly the keto zone diet recipes weight loss pills saxenda liquid keto diet recipes detox and cieanse weight loss pills good diet for teenage girl lose weight best diet menu to lose weight can fiber supplements help you lose weight best time to take acv pills for weight loss best days to start a diet to lose weight diets for men to lose weight indian diet plan for weight loss in one month pdf weight loss pills blue pills no excersize effective diet chart for weight loss new drug for weight loss fda weight loss drug approval vitapatch weight loss diet drops exercise versus diet for weight loss weight loss pills with sibutramine free healthy diet plans for weight loss craze supplement weight loss how to start a keto diet for weight loss why can i not lose weight with exercise and diet 3 benefits of weight loss supplements are the keto ultra diet pills all natural weight loss pills wysert secret weight loss pills south africa best weight loss pills on amazon reddit keto diet pills from shark tank hydroxycut weight loss pills walmart robert costa weight loss diet best diet program loss weight lose weight fast pills medical weight loss diet requirements thrive weight loss diet dr sebi diet plan for weight loss mascarpone recipes keto diet transformations weight loss diet plan lake mary raspberry weight loss pills reviews why is diet 80 of weight loss top 10 weight loss dorps vs pills how to weight loss diet in telugu do any weight loss supplements really work how many apple cider vinegar pills to lose weight dennis mac weight loss pills weight loss pills droz fda weight loss drug

size up xl pills pills to have sex and not get pregnant sperm booster pills binaural penis enlargement peak male enhancement pills red dragon sexual enhancement pill wachsen riesig male enhancement penis enlargement 1 foot boss rhino gold male enhancement purple rhino pill report best male enhancement options what store sells male enhancement pills want penis enlargement pills penis enlargement vine male enhancement apx mens health stamina sex enhancement viagra blue pills male enhancement hot rod free samples of ed pills that work erection pills names best ed pills for hardest erection enhancerx vs vigrx plus drug causig erectile dysfunction dr loria male enhancement reviews erectile dysfunction pills from abroad what do sex chandge pills do to you otc blue pill for erectile dysfunction extenze pills what do they do erection pills cvs penus enlargement patch increasing libido supplements male enhancement proof pictures rino male enhancement pills best supplements to boost energy top 10 male enhancement for 4000 blue rhino male enhancement pp 100 pill thunder rock male enhancement alpha man extreme pill penis enlargement las vegas hydrotherapy penis enlargement results libadorm male enhancement recall cedars sinai penis enlargement surgery outlaw laboratory male enhancement penis enlargement pill formula 41 dysfunction erectile pills advantages of sex enhancement pills penis growth pills review rhino vp sex pills generic prescription drugs online hydrochloride pills penis enlargement for gay men order viagra pills exr pro male enhancement street pills green packet sex pills horny goat natural male enhancement pills smiling bob how to enlarge male penis how to grow your dick bigger without pills cvsmale sex pills

can you fail a drug test from cbd buy thc cbd oil the truth about cbd oil plus cbd gummies review what are the effects of cbd sera labs cbd oil review private label cbd products cbd central business district is cbd oil bad for your liver does cbd cause weight gain cbd oil for hemorrhoids possible side effects of cbd oil does cbd help sleep cbd laws in texas cbd oil e cig cbd oil for motion sickness health plus life cbd cbd massage benefits cbd e juice review what drugs should not be taken with cbd cbd lube review mail order cbd oil louisiana does cbd oil help high blood pressure viva oil cbd hemp oil selling cbd on shopify cbd oil athens greece herbal renewals gold cbd oil review prime my body cbd oil reviews promo code for cbd cbd oil legal in texas cbd oil 5000mg is cbd the same as medical marijuana twine cbd oil topical cannabis oil for arthritis lava love cbd how to make cbd oil suppositories hygeia cbd plumbing supplies melbourne cbd cbd subreddit prednisone and cbd oil cbd feather flag will cbd topical show up on a drug test cbd hemp direct wholesale is cbd oil from hemp legal cbd oil hemp drops cbd infused coffee near me why cbd should be illegal who cbd 2020 cbd oil temecula organic elixinol cbd oil review new world cbd oil what is cbd oil and is it legal in nj house of hemp cbd oil cbd oil addiction cbd oil source huntersville cbd oil for gout cbd oil help liver issies cbd online stores does cannabis oil get you high does cbd interact with sertraline