Saturday, December 17, 2016

Struts 1 Login Examples step by step using eclipse.

1. Download jars required for Login example using struts-1 framework.

  • javax.servlet.jar (http://www.java2s.com/Code/Jar/j/Downloadjavaxservletjar.htm)
  • struts-1.3.10-lib.zip (http://struts.apache.org/download.cgi)
  • struts-taglib-1.3.5.jar (http://www.java2s.com/Code/Jar/s/Downloadstrutstaglib135jar.htm)

2. Create a dynamic web project (named as 'LoginExample') in eclipse.














3. Extract all downloaded jars (as mentioned in point number 1) and then go to eclipse and click on your project 'LoginExample' and and you will a 'lib' folder under "WebContent/WEB-INF".directory. Then copy paste all jar files under this "lib" folder.

5.  Create folder structures for this project as below,

  • Create two packages ("com.struts1.actions" and "com.struts1.forms") under "src" folder.
  • Create one folder (Presentations) under "WebContent"

















6. Open deployment descriptor (web.xml) file and remove all code available in this file and copy-paste the below written code for this project.

<?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>StrutsApplication</display-name>
<welcome-file-list>
<welcome-file>Presentations/Login.jsp</welcome-file>
</welcome-file-list>
<servlet>
   <servlet-name>Struts</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>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>Struts</servlet-name>
   <url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

7. Create an XML file named as "struts-config.xml" under "WEB-INF" folder (same location where web.xml present) and then delete the source code available here and copy paste the below code.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
         "http://struts.apache.org/dtds/struts-config_1_3.dtd">
         
<struts-config>
   <!-- ========== Form Bean Definitions ================================== -->
   <form-beans>
       <form-bean name="loginForm" type="com.struts1.forms.LoginForm"/>
   </form-beans>
   <!-- ========== Action Mapping Definitions ============================= -->
   <action-mappings>
           <action name="loginForm" path="/Login" type="com.struts1.actions.LoginAction" scope="request" input="/Login.jsp">
               <forward name="failure" path="Presentations/Failure.jsp" redirect="true"/>
               <forward name="success" path="Presentations/Success.jsp" redirect="true"/>
           </action>
   </action-mappings>
</struts-config>

8. Now create 3 JSP files under your created folder named as "Presentations". The files are as follows;
  • Login.jsp
  • Success.jsp
  • Failure.jsp


Copy paste the below source code for the above 3 JSP files,

================= Login.jsp =================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<html:form action="/Login" method="post">
User Name:<html:text property="userName"/></br>
Password:<html:password property="password"/></br>
<html:submit/>
</html:form>
</body>
</html>
================= Success.jsp =================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
SUCCESS
</body>
</html>
================= Failure.jsp =================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
FAILURE
</body>
</html>

9. Now create a POJO class named as "LoginForm.java" under "com.struts1.forms" package and the file contain is as follows,

package com.struts1.forms;

import org.apache.struts.action.ActionForm;

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;
}
}

10. Now create an Action class named as "LoginAction.java" under "com.struts1.actions" package and the file contain is as follows,

package com.struts1.actions;

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;

import com.struts1.forms.LoginForm;

public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse response)
throws Exception {
LoginForm frm = (LoginForm) form;
if (frm.getUserName().equalsIgnoreCase("admin")
&& frm.getPassword().equalsIgnoreCase("admin")) {
return mapping.findForward("success");
} else {
return mapping.findForward("failure");
}
}
}






11. Now configure server to run your web project. For this example I have used "Apache Tomcat 6" and run the project and it will open the "Login" page.



















Login is page is as follows. You can use any browser to run this project using the below URL.









12. For this example we set user name and password as "admin" and "admin" respectively.
 So once click on submit button with correct user name and password, it will redirect to "Success.jsp" page and for invalid credential it will redirect to "Failure.jsp" page.

With valid credential ==>







With Invalid credential ==>








================= Total description =================

Will be updated soon.