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

要求
  • 必备知识

    JAVA/Struts2,JS/JQuery,HTML/CSS基础语法。

  • 开发环境

    MyEclipse 10

  • 演示地址

    演示地址


预览截图(抬抬你的鼠标就可以看到演示地址哦):

关于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<User> users;
    public LinkedList<User> getUsers() {
        return users;
    }
    public void setUsers(LinkedList<User> 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<User> getList(){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        User myUser=null;
        LinkedList<User> users=new LinkedList<User>();
        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;
        }
        
}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值