Using STRUTS2 framework to connect database to implement user registration and login Tools/raw Materials
Eclipse, struts-2.3.16.3
Mysql Method/Step 1
Project Catalogue:
The jar packages used in the project are:
Database User Table Tb_user:
Web.xml configuration:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<web-app 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/web-app_2_4.xsd"
version= "2.4" >
<filter>
<filter-name>struts2</filter-name>
<!-Note that the new version of Struts will be configured as follows-->
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Struts.xml configuration:
<?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>
<package name= "com" extends= "Struts-default" >
<action name= "Login" class= "com. Useraction "method=" Login ">
<result name= "Loginout" >/error.jsp</result>
<result name= "Loginin" >/welcome.jsp</result>
</action>
<action name= "regist" class= com. Useraction "method=" Regist ">
<result name= "Error" >/error.jsp</result>
<result name= "Success" >/welcome.jsp</result>
</action>
</package>
</struts>
Database Dao.java
Package DAO;
Import java.sql.*;
public class Dao {
Connection con = null;
Statement stat = null;
ResultSet rs = null;
Public Dao () {
try {
Class.forName ("Com.mysql.jdbc.Driver");
con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/db_vote", "root", "root");
Stat = Con.createstatement ();
catch (Exception e) {
Todo:handle exception
con = null;
}
}
Public ResultSet executequery (String sql) {
try {
rs = stat.executequery (SQL);
catch (Exception e) {
Todo:handle exception
rs = null;
}
Return RS;
}
public int executeupdate (String sql) {
try {
Stat.executeupdate (SQL);
return 0;
catch (Exception e) {
Todo:handle exception
}
return-1;
}
}
Useraction.java
Package com;
Import java.sql.*;
Import Com.opensymphony.xwork2.ActionSupport;
Import DAO. Dao;
@SuppressWarnings ("Serial")
public class Useraction extends actionsupport{
Private dao DAO = new Dao ();
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 String Login () {
String sql = "SELECT * from Tb_user where username= '" + getusername () + "' and password = '" +getpassword () + "";
ResultSet RS = dao.executequery (sql);
try {
if (Rs.next ()) {
return "Loginin";
}
return "Loginout";
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return "Loginout";
}
}
Public String regist () {
String sql = "INSERT into Tb_user (Username,password,ischeck) VALUES (' +getusername () +" ', ' "+getpassword () +" ', ' 0 ') ";
int i = dao.executeupdate (sql);
if (i >-1) {
Return "Success";
}
return "error";
}
Common methods of execution
Public String Execute () throws Exception {
if (GetUserName (). Equals ("Scott") && GetPassword (). Equals ("Tiger")) {
System.out.println ("I am Success");
Return "Success";
// }
//
else {
System.out.println ("I am the error");
return "error";
// }
// }
}
User Registration Interface Regist.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<center>
<form action= "Regist.action" method= "POST" >
<table>
<caption>
<tr>
<td>username: </td>
<td><input type= "text" name= "username"/></td>
</tr>
<tr>
<td>password: </td>
<td><input type= "password" name= "password"/></td>
</tr>
<tr>
<td><input type= "Submit" name= "Submit"/></td>
</tr>
</table>
</form>
</center>
</body>
Login Interface login.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<form action= "Login.action" method= "POST" >
<table align= "center" >
<caption>
<tr>