要求:

必备知识:JAVA/Struts2,JS/JQuery,HTML/CSS基础语法;

开发环境:MyEclipse 10

jquery用户登录注册验证 jquery登录注册源代码_sql

jquery用户登录注册验证 jquery登录注册源代码_sql_02

jquery用户登录注册验证 jquery登录注册源代码_sql_03

jquery用户登录注册验证 jquery登录注册源代码_java_04

关于UI部分请查看下列链接,有详细制作步骤:

前段时间学校刚学完Struts2-Action篇,又自学了一点AJAX/JQuery,到网上看了一些CSS3知识。突然想要不要干脆做一个用户注册与登入功能。下面是JAVA部分的核心代码, 如果这样的逻辑和大家想的很有出入的话,欢迎拍砖劈斧,呵呵。

UserAction.java
package action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dao.UserDao;
import entity.User;
public class UserAction extends ActionSupport {
private String contentType = "text/html;charset=utf-8";
private User user;
private LinkedList users;
public LinkedList getUsers() {
return users;
}
public void setUsers(LinkedList users) {
this.users = users;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
/**
* 查询用户 登入验证
* @return
* @throws IOException
*/
public void select() throws IOException{
//指定输出内容类型和编码
ServletActionContext.getResponse().setContentType(contentType);
//获取输出流,然后使用
PrintWriter out = null;
out = ServletActionContext.getResponse().getWriter();
this.user=new UserDao().select(user); //给this.user赋值
if(user==null){
out.print("登入失败");
}else{
ActionContext actionContext=ActionContext.getContext();
actionContext.getSession().put("user",user);
actionContext.getSession().put("users",new UserDao().getList());
out.print("登入成功");
}
out.flush();
out.close();
}
/**
* 添加用户控制器
* @throws Exception
*/
public void add() throws IOException{
//指定输出内容类型和编码
ServletActionContext.getResponse().setContentType(contentType);
//获取输出流,然后使用
PrintWriter out = null;
out = ServletActionContext.getResponse().getWriter();
int rs=new UserDao().add(this.user);
if(rs==1){
ActionContext actionContext=ActionContext.getContext();
actionContext.getSession().put("user",user);
actionContext.getSession().put("users",new UserDao().getList());
}
out.print(rs);
out.flush();
out.close();
//System.out.print(new UserDao().add(this.user)); 这里不能在用 System.out.print() 否则后台报错
}
public String upd(){
return null;
}
public String del(){
return null;
}
/*@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}*/
}
UserDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import tools.ConvertJson;
import tools.JDBCUtilSingle;
import entity.User;
public class UserDao {
/**
* 插入操作 注册功能
* @param user 用户实例 POJO
* @return 操作标记 1成功 2邮箱存在 3用户名存在
*/
public int add(User user){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="select * from form2_user where name=? or email=?";
try {
statement=connection.prepareStatement(sql);
statement.setString(1,user.getName());
statement.setString(2, user.getEmail());
rs=statement.executeQuery();
if(rs.next()){
if(rs.getString("email").equals(user.getEmail())){return 2;} //2邮箱存在
if(rs.getString("name").equals(user.getName())){return 3;} //3用户名存在
}
sql="INSERT INTO form2_user (`id`, `email`, `name`, `pass`) VALUES (NULL,?,?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,user.getEmail());
statement.setString(2,user.getName() );
statement.setString(3, user.getPass());
statement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
}
return 1; //1表示成功注册
}
/**
* 用户登录 放回登入用户对象信息
* @param user 用户对象
* @return
*/
public User select(User user){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
User myUser=null;
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="select * from form2_user where (name=? or email=?) and pass=?";
try {
statement=connection.prepareStatement(sql);
statement.setString(1,user.getName());
statement.setString(2,user.getName());
statement.setString(3,user.getPass());
rs=statement.executeQuery();
if(rs.next()){
myUser=new User();
myUser.setName(rs.getString("name"));
myUser.setEmail(rs.getString("email"));
myUser.setPass(rs.getString("pass"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
}
return myUser;
}
/**
* 获取所有用户信息
* @return 用户集合
*/
public LinkedList getList(){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
User myUser=null;
LinkedList users=new LinkedList();
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="select * from form2_user";
try {
statement=connection.prepareStatement(sql);
rs=statement.executeQuery();
while(rs.next()){
myUser=new User(rs.getString("email"),rs.getString("name"), rs.getString("pass"));
users.add(myUser);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
}
return users;
}
}
User.java
package entity;
public class User {
private String email;
private String name;
private String pass;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public User(){}
public User(String email,String name, String pass){
this.email=email;
this.name=name;
this.pass=pass;
}
}

呵呵,又结束了,不知到你们看懂了没。请原谅童鞋我目前的表述能力只能到这了。欢迎大家来拍砖来劈斧,希望我幼小的心灵能抗得住。

Struts2+AJAX+JQuery 实现用户登入与注册功能。