Struts 2.x program

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Jtc1</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
 
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!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>Java Training Center</h1>
<h3>Account Login</h3>
<s:actionerror/>
<s:form action="loginsubmit">
<s:textfield key="username" cssStyle="fontsize:30;color=green"/>
<s:textfield key="password" cssStyle="fontsize:30;color=green"/>
<s:submit name="submit" value="submit"/>
</s:form>
</body>
</html>

index.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">
<meta http-equiv="refresh" content="1;url=jtcindia/LoginDisplay.action">
<title>Insert title here</title>
</head>
<body>
<p><font size="9">Loading.........Please Wait</font>
</body>
</html>

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><s:head/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello!<s:property value="username"/><br>Welcome To Jtc</h1>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

   

   <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>

    <constant name="struts.devMode" value="true" />
    <package name="jtcindia" namespace="/jtcindia" extends="struts-default">
        <action name="LoginDisplay" class="com.jtcindia.struts2.LoginDisplayAction">
            <result name="success">/login.jsp</result>
        </action>


        <action name="loginsubmit" class="com.jtcindia.struts2.LoginSubmitAction">
            <result name="success">/home.jsp</result>
            <result name="failed">/login.jsp</result>
            <result name="input">/login.jsp</result>



        </action>
    </package>
</struts> 

LoginDisplayAction.java

package com.jtcindia.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginDisplayAction extends ActionSupport{

public String execute() throws Exception {
    return "success";
}
}



 LoginSubmitAction.java









package com.jtcindia.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginSubmitAction extends ActionSupport{
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;
}
/*
public void validate(){
    System.out.println("validate()");
    if(username==null||username.length()==0){
        addFieldError(username, "Username is required");
    }
    if(password==null||password.length()==0){
        addFieldError(password, "Password is required");
    }
}*/

@Override
    public void validate() {
    System.out.println("validate()");
    if(username==null||username.length()==0){
        addFieldError("username", "Username is required");
    }
    if(password==null||password.length()==0){
        addFieldError("password", "Password is required");
    }

    }


    public String execute() throws Exception {
        System.out.println("execute()");
        String result="";
        if(username.equals(password)){
            result="success";
        }else{
            result="failed";
            addActionError("invalid Username or password");
        }
        return result;
    }
}






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