Struts 2.0 Login Example Using Oracle Database

Struts 2.0 Login Example Using Oracle Database

Previous Home Next

 

Login Example using Struts 2.0 the following tool are required for run this example
  • JDK 1.5
  • MyEclipse IDE
  • Server Tomcat 6.0
  • Struts 2.0 jar file
  • Ojdbc14.jar
  • Oracle 10G Database Server
Directory Structure of Login Using Database Example in Struts 2.0 Using MyEclipse IDE


 
index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:actionerror/>
<s:form action="login">
<s:textfield name="name" label="Name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login"/>
</s:form></center>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping></web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="login" class="org.r4r.LoginAction">
<result name="success">/hello.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts> 

LoginAction.java

package org.r4r;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
	private String name;
	private String password;
	DAO dao=new DAO();
	
	public void validate(){
		if(name.length()==(0))
			this.addFieldError("name", "Name is required");
		if(password.length()==(0))
			this.addFieldError("password", "Password is required");
	}
	public String execute(){
		if(dao.find(getName(),getPassword()))
			return "success";
		else
			this.addActionError("Invalid username and password");
		    return "error";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

DAO.java

package org.r4r;

import java.sql.*;

public class DAO {
	public boolean find(String name,String password){
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection con=DriverManager.getConnection
			("jdbc:oracle:thin:@localhost:1521:xe","system","system");
			PreparedStatement stmt=con.prepareStatement
			("select * from userlist where name=? and address=?");
			stmt.setString(1,name);
			stmt.setString(2,password);
			ResultSet rset=stmt.executeQuery();
			if(rset.next())
				return true;
		}catch(Exception e){
			System.out.println(e);
		}
		return false;
	}

}

hello.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<b>Welcome,<s:property value="name"/></b>

Output




Previous Home Next