Struts 2.x : [A Framework from ASF] :: Struts 2.x with Database
Struts 2.x with Database |
Here, I have tried to create Struts 2.x application and using MySQL Server to validate username and password from database.
Example |
Project Structure
MyConnection.java
package com.javaskool.struts;
import java.sql.DriverManager;
import java.sql.*;
public class MyConnection {
Connection con;
public Connection getConnect() throws Exception
{
/* Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","SYSTEM","admin");
*/
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaskoolDB","root","admin");
return con;
}
}
DAOForStruts.java
package com.javaskool.struts;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DAOForStruts {
Connection con=null;
MyConnection c=null;
public void insertData(String user,String password,String name) throws Exception
{
c=new MyConnection();
con=c.getConnect();
PreparedStatement st=con.prepareStatement("insert into login values(?,?,?)");
st.setString(1, user);
st.setString(2,password);
st.setString(3, name);
st.executeUpdate();
}
public String getData(String user,String password) throws Exception
{
String name="";
c=new MyConnection();
con=c.getConnect();
PreparedStatement st=con.prepareStatement("select * from login where user=? and password=?");
st.setString(1, user);
st.setString(2,password);
ResultSet rs=st.executeQuery();
rs.next();
name=rs.getString(3);
return name;
}
}
SQL Code
mysql> create database javaskooldb;
mysql> use javaskooldb;
mysql> create table login
-> (
-> user varchar(20) primary key,
-> password varchar(20),
-> name varchar(20)
-> );
Query OK, 0 rows affected (2.26 sec)
mysql> select * from login;
+-------+----------+------+
| user | password | name |
+-------+----------+------+
| james | bond | Mr X |
+-------+----------+------+
1 row in set (0.05 sec)
LoginAction.java
package com.javaskool.struts;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private String user;
private String name;
private String password;
public String execute()
{
String ret=ERROR;
DAOForStruts d=null;
try
{
d=new DAOForStruts();
//for inserting record
//d.insertData(user, password, "Mr X");
//for retrieving
name=d.getData(user,password);
ret=SUCCESS;
}
catch(Exception e)
{
ret=ERROR;
}
return ret;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
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;
}
}
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">
<title>Login </title>
</head>
<body>
<form action="loginaction" method="post">
Username : <input type="text" name="user"><br>
Password : <input type="password" name="password"> <br>
<input type="submit" value="Login">
</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">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success</title>
</head>
<body>
Welcome , <s:property value="name"/> !! You are successfully logged in.
</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>Error</title>
</head>
<body>
You are not authorized. Please check your credential.
</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"></constant>
<constant name="struts.multipart.maxSize" value="1000000"></constant>
<package name="p1" extends="struts-default">
<action name="loginaction" class="com.javaskool.struts.LoginAction"
method="execute">
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>
</struts>
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?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>Struts2WithDB</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
If you enter wrong credential, will return below screen.
Click here to download complete code with jar files [4.2 MB]
Recent Comments