Struts 1.x program

 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.jtcindia.struts1.LoginForm" />
    </form-beans>

    <action-mappings>
        <action path="/loginSubmit" name="loginform"
            type="com.jtcindia.struts1.LoginAction" validate="true" input="/Login.jsp">
            <forward name="home" path="/Home.jsp" />
            <forward name="login" path="/Login.jsp" />

        </action>
    </action-mappings>

    <message-resources parameter="com.jtcindia.struts1.message" />

</struts-config>


login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://jakarta.apache.org/struts/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>
    <center>
        <h1>Account Login</h1>
        <html:form action="loginSubmit">
            <table>

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

                <tr>
                    <td>password</td>
                    <td><html:password property="password" /></td>
                    <td><html:errors property="password" /></td>
                </tr>
<tr>
                    <td colspan="3"><center><html:submit value="Login" /></center></td>
                </tr>
            </table>
        </html:form>
    </center>
</body>
</html>

Home.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>
<h1>
Hello
<font color="blue">
${UN}</font>
<br/>Welcome to JTC Home Page
</h1>
</body>
</html>

message.properties

errors.un.required=<font color="red" size="5">UserName is mendatory</font>
errors.pw.required=<font color="red" size="5">Password is Mendatory</font>

LoginForm.java

package com.jtcindia.struts1;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.*;


public class LoginForm extends ActionForm {
   
private String username;
private String password;
//Setters and Getters method
   
    public void reset(ActionMapping am, HttpServletRequest req) {
    this.username=null;
    this.password=null;
   
    }
    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){
        System.out.println("validate method");
    ActionErrors errors=new ActionErrors();
    if(username==null||username.length()==0){
        errors.add("username",new ActionError("errors.un.required"));
    }
    if(password==null||password.length()==0){
        errors.add("password",new ActionError("errors.pw.required"));
    }
        return errors;
      
    }

}

LoginAction.java

 package com.jtcindia.struts1;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
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.ActionForwards;
import org.apache.struts.action.ActionMapping;


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




web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JTC_1</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>
      <param-value>/WEB-INF/jtcindia-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>









Divanshu Sindhwani

Comments

Popular posts from this blog

HTML& CSS USERNAME AND PASSWORD PAGE CODING

spring core exp

Swapping without using 3rd variable in c and c++ and any language use logic