zoukankan      html  css  js  c++  java
  • 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;
            }
            
    }
    

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

    作者:Li-Cheng
  • 相关阅读:
    程序为什么加载到内存中
    cortex-A cortex-R cortex-M处理器的性能比较
    makefile 中的赋值方式
    python(老男孩全栈观后感------文件处理)
    python------lambda(匿名函数)
    python------filter(过滤器)
    Express深入解读
    nodejs安装
    一道有意思的题目
    charAt获取数组,测试
  • 原文地址:https://www.cnblogs.com/safoie/p/10603755.html
Copyright © 2011-2022 走看看