实现简单的注册登录

废话不多说,先贴代码

先是注册页面:zhuce.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>



"UTF-8">
Insert title here


"post" action="${pageContext.request.contextPath}/Zhuceservlet?method=add">
"center">
用户名: "text" name="username" >
密码: "password" name="password" >
邮箱: "text" name="email" >
  "center"> "submit" value="注册" >

 

然后是登录界面:login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>



"UTF-8">
Insert title here


"/TestDenglu/UserServlet" method="post" >
"center">
用户名: "text" name="username" >
密码: "password" name="password" >
"submit" value="登录" >
"zhuce.jsp">注册账号

 

主界面就有些简陋了,因为主要是实现注册登录的:welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>



"UTF-8">
Insert title here


    
"center"> 欢迎您!

 

servlet实现登录的主要方法

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
             request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String psw =new UserDao().findUsername(username);
           
            if(psw ==null){
                PrintWriter out = response.getWriter();
                out.println(" ");    
                out.println("");
                out.flush();
                out.close();       
            }
            if(psw!=null&&!psw.equals(password)){
                PrintWriter out = response.getWriter();
                out.println(" ");    
                out.println("");
                out.flush();
                out.close();   
            }
            if(psw!=null&&psw.equals(password)){
                PrintWriter out = response.getWriter();
                out.println(" ");      
                out.println("");
                out.flush();
                out.close();
                out.print(true);
            }
    }

 

其中findUsername(username)方法

public String findUsername(String username){
            String psw = null;
            Connection con =null;
            PreparedStatement pstmt =null;
            ResultSet rs = null;
            try {
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3308/testdenglu?characterEncoding=utf8&useSSL=true";
                String user = "root";
                String password = "root";
                Class.forName(driver);
                con = DriverManager.getConnection(url, user, password);
                String sql = "select * from user where username=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, username);
                rs = pstmt.executeQuery();
                if(rs==null){
                    return null;
                }
                if(rs.next()){
                    psw=rs.getString("password");
                }else{
                    psw=null;
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(pstmt!=null)pstmt.close();
                    if(con!=null)con.close();
                }
                catch (SQLException e) {       
                                        }
            }
            return psw;
        }

 

 

再说一下我的想法

   首先是注册

  1.先实现注册的页面及表单内容,例如用户名、密码、邮箱、性别等

  2.然后实现用户添加操作,表单提交并实现数据库连接

  3.最后是数据库的添加成功

   接着是登录

  1.先是实现登录界面,表单以及每项标签的name

  2.然后是form路径,通过servlet实现查询校验

     servlet:

    1、先获取当前输入的用户名、密码

    2、然后拿着这个用户名通过数据库查询,是否有与之相同的

      若没有,则返回空,用户名不存在

      若有,则返回该用户在数据库中的密码,然后和当前输入的密码进行校验

          若不同,则用户名或密码输入错误!

          若相同,则登陆成功!跳转到主界面

 

你可能感兴趣的:(实现简单的注册登录)