想想MVC-使用JavaBean完成用户登陆,注册和修改密码功能

MVC

MVC即模型(M),视图<V>,控制器(C)。在Java Web的开发中,我们可以将Jsp文件看作是视图部分,将Servlet部分看作是控制器部分,JavaBean部分看作是模型部分。脑海里有着MVC的概念,在做开发的时候思路就会比较清晰。大的开发走向也变得清晰一些。

JavaBean

JavaBean实际上就是开发者根据需要创建的类,它必须有一个无参的构造方法,对于它的属性要求有对应的封装函数,这样更加安全。也体现了软件工程中“高内聚,低耦合”的思想。

小项目要求

1.提供用户登录,用户注册,用户修改密码的功能
2.将用户登录后的信息存储到JavaBean对象中,并记录到session中,当用户输入正确登陆信息时,从session中取出信息,进行显示。
3.链接数据库的操作要求被封装成一个类,每次需要链接字符串的时候实例化该类的一个实例,类中必须提供实现功能要求的所有方法。

小提示

在小项目中我写了很多很多的注释,帮助大家理解代码的含义。其中有很多被我注释掉的代码,并不是说它们是错误的,只是因为有我认为的好一点的方法可以替代它们。大家在看的时候不妨也看看这些代码,想想为什么要替换掉它们。写的过程中,大家也会慢慢的注意到一些性能的问题,比如:对于用户提交的信息做验证的过程,如果我可以在客户端做完验证,那么我为什么还要去浪费服务器资源呢?这就是为什么在修改密码时,我将验证写在了Jsp的Scripe中。

我的开发环境

数据库:依旧是WAMP SERVER;
数据库管理工具:依旧是NavicatForMysql;
IDE:MyEclipse;
默认的WebServer:TomCat7.0;
浏览器:Chrome

代码

Login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>Login</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <style type="text/css">
    .login_mainbody{
        position:absolute;
        left:400px;
        top:200px;
    }
    .login_footer{
        position:absolute;
        left:600px;
        top:400px;
    }
    </style>
  </head>

  <body>
    <h2 style="color:lightgreen">欢迎登陆</h2><hr/>
    <form method="post" action="servlet/Judge">
    <div class="login_mainbody">
        用户<input type="text" name="Username"><br/><br/>
        密码<input type="password" name="Password"><br/><br/>
        <input type="submit" value="登陆" />
        <input type="reset" value="重置" /><br/><br/>
    </div>
    <div class="login_footer">
        <a href="Jsp/Register.jsp">没有账号?申请一个!</a>&nbsp;&nbsp;<a href="Jsp/Change_Password.jsp">修改密码?</a>
    </div>
    </form>
  </body>
</html>

Register.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>Register</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <style type="text/css">
    .register_mainbody{
        position:absolute;
        left:400px;
        top:200px;
    }
    .register_footer{
        position:absolute;
        left:600px;
        top:400px;
    }
    </style>

  </head>

  <body>
    <h2 style="color:lightgreen">注册用户</h2><hr/>
     <form method="post" action="servlet/Register_Judge">
        <div class="register_mainbody">
        用户<input type="text" name="Username"><br/><br/>
        密码<input type="password" name="Password"><br/><br/>
        <input type="submit" value="注册" />
        <input type="reset" value="重置" /><br/><br/>
        </div>
        <div class="register_footer">
            <a href="Jsp/Login.jsp">回到登陆界面</a>&nbsp;&nbsp;<a href="Jsp/Change_Password.jsp">修改密码?</a>
        </div>
    </form>
  </body>
</html>

Change_Password.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>Change_Password</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <!-- 对用户输入的信息进行验证,要求信息不可以为空,两次输入的密码不可以是一样的 -->
    <script language="javascript">
        function isValidate(form){

            //获取表单信息
            username=form.Username.value;
            password=form.Password.value;
            confirm_password=form.Confirm_Password.value;
            //判断用户是否输入的完整的信息
            if(username==""||password==""||confirm_password==""){
                alert("所有信息必须填写完整");
                return false;
            }else if(password==confirm_password){
                //判断用户是否输入了形同的密码
                alert("新密码不可以与原密码一致!");
                return false;
            }else{
                return true;
            }

        }
    </script>
    <style type="text/css">
    .Change_Password_mainbody{
        position:absolute;
        left:400px;
        top:200px;
    }
    </style>
  </head>

  <body>
   <h2 style="color:lightgreen">修改密码</h2><hr/>

   <!-- 当用户点击 提交 按钮后,这份表单是否可以提交还取决于form中设置的onsubmit属性是真还是假 -->

    <form name="form1" method="post" action="servlet/Change_Password" onsubmit="return isValidate(form1)">
        <div class="Change_Password_mainbody">
            &nbsp;用户<input type="text" name="Username"><br/><br/>
             原密码<input type="password" name="Password"><br/><br/>
             新密码<input type="password" name="Confirm_Password"><br/><br/>
            <input type="submit" value="修改" />
            <input type="reset" value="重置" />
        </div>
    </form>
  </body>
</html>

success.jsp:

<!-- 引入自己写好的JavaBean包很重要啊 -->
<%@ page language="java" import="java.util.*,student.MyBean" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>Success</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
         <%
            student.MyBean mybean=(student.MyBean)session.getAttribute("user");
            out.print(mybean.getUsername());
         %>  
        欢迎登陆!
  </body>
</html>

fail.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'fail.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
        登陆失败!
  </body>
</html>

Judge.java:

//用户在登陆时进行处理的后台代码
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//一样记得引入这些写好的类
import student.Connect;
import student.MyBean;

@SuppressWarnings("serial")
public class Judge extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //依旧调用写好的方法
        doPost(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //设置文本格式
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        //获取用户通过form表单提交的信息
        String username = request.getParameter("Username");
        String password = request.getParameter("Password");

        //创建链接对象,设置初始化字符串
        Connect connect=new Connect();
        String sqlstr="select * from user where username='" + username
                    + "' and password='" + password + "'";

        //设置connect对象的属性值(通过封装好的sqlstr的方法)
        connect.setSqlstr(sqlstr);

        //记录记录数的变量
        int totalRows=0;
        try {
            //去看看数据库中有没有这个人
            totalRows=connect.excute_query();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (totalRows > 0) {

            //如果有的话,就创建MyBean的一个对象,将其信息封装在一个对象里
            MyBean mybean=new MyBean();
            mybean.setUsername(username);
            mybean.setPassword(password);

            //将当前用户信息加入缓存
            request.getSession().setAttribute("user", mybean);

            //然后跳转到success页面,提示用户登录成功
            response.sendRedirect("../Jsp/success.jsp");
        } else {
            //如果没有这个人,跳到fail,提示登陆失败
            response.sendRedirect("../Jsp/fail.jsp");
        }



    }
        /*
        String user = "root";
        String pass = "";

        String url = "jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=UTF-8";

        String driver = "com.mysql.jdbc.Driver";

        String sqlstr;

        Connection connection = null;

        java.sql.Statement statement = null;

        ResultSet result = null;

        try {

            Class.forName(driver);

            connection = DriverManager.getConnection(url, user, pass);

            statement = connection.createStatement();

            sqlstr = "select * from user where username='" + username
                    + "' and password='" + password + "'";

            result =statement.executeQuery(sqlstr);
            result.last();

            int totalRows = result.getRow();
            System.out.println(totalRows);

            if (totalRows > 0) {

                MyBean mybean=new MyBean();

                mybean.setUsername(username);
                mybean.setPassword(password);

                request.getSession().setAttribute("user", mybean);

                response.sendRedirect("../Jsp/success.jsp");
            } else {

                response.sendRedirect("../Jsp/fail.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (result != null)
                try {
                    result.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (connection != null)
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }*/


}

Register_Judge:

//处理用户注册的后台代码
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//还是要记住引入这些写好的类,因为你要使用他们
import student.Connect;
import student.MyBean;


public class Register_Judge extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //你说doPost会不会不让doGet用,哈哈
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //注册的时候,用户如果输入相同的姓名,不同的密码是可以注册的,但是现在要怎么阻止这种情况
        //这个我好像处理不了

        //设置文本格式
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        //获取用户信息
        String username = request.getParameter("Username");
        String password = request.getParameter("Password");

        //创建链接对象,初始化字符串
        Connect connect=new Connect();
        String sqlstr="select * from user where username='" + username
                    + "' and password='" + password + "'";

        //设置connect对象的sqlstr属性值
        connect.setSqlstr(sqlstr);
        //记录符合要求的记录数
        int totalRows=0;
        try {
            //看看有没有这个人
            totalRows=connect.excute_query();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //获取输出流
        PrintWriter out=response.getWriter();
        if (totalRows > 0) {
            //如果有这个人,用户就不能以这个人的身份再次注册,需要给出用户相应的提示信息
            out.print("<html>");
            out.print("<head></head>");
            out.print("<body>该用户已经存在,请直接登陆!</body>");
            out.print("</html>");
            //提示之后怎么跳到Login.jsp

        } else {
            //如果没有这个人,连接字符串改为插入信息的字符串
            sqlstr = "insert into user values('"+username+"','"+password+"')";
            //传递给connect对象,它好用来进行操作
            connect.setSqlstr(sqlstr);  
            try {
                //这里执行的是excuteUpdate函数
                connect.excute_update();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            out.print("<html>");
            out.print("<head></head>");
            out.print("<body>注册成功!</body>");
            out.print("</html>");
        }
        /*
        String user = "root";
        String pass = "";

        String url = "jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=UTF-8";

        String driver = "com.mysql.jdbc.Driver";

        String sqlstr;

        Connection connection = null;

        java.sql.Statement statement = null;

        ResultSet result = null;

        try {

            Class.forName(driver);

            connection = DriverManager.getConnection(url, user, pass);

            statement = connection.createStatement();

            sqlstr = "select * from user where username='" + username
                    + "' and password='" + password + "'";

            result =statement.executeQuery(sqlstr);
            result.last();

            int totalRows = result.getRow();

            PrintWriter out=response.getWriter();

            if (totalRows > 0) {
                out.print("<html>");
                out.print("<head></head>");
                out.print("<body>该用户已经存在,请直接登陆!</body>");
                out.print("</html>");
                //提示之后怎么跳到Login.jsp

            } else {
                sqlstr = "insert into user values('"+username+"','"+password+"')";
                statement.executeUpdate(sqlstr);
                out.print("<html>");
                out.print("<head></head>");
                out.print("<body>注册成功!</body>");
                out.print("</html>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (result != null)
                try {
                    result.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (connection != null)
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        */

    }

}

Change_Password.java:

//用户修改密码时的Servlet后台处理实现
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//记住要引入自己定义的这两个类
import student.Connect;
import student.MyBean;


public class Change_Password extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //还是调用写好的doPost方法
            doPost(request,response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //设置文本格式
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        //获取用户输入信息
        String username=request.getParameter("Username");
        String old_password = request.getParameter("Password");
        String new_password=request.getParameter("Confirm_Password");

        //获取输出流
        PrintWriter out = response.getWriter();

        /*
        if(old_password.equals(new_password)){
            out.print("<html>");
            out.print("<head></head>");
            out.print("<body>新密码不可与旧密码一致!</body>");
            out.print("</html>");
        }
        */

        //创建链接对象
        Connect connect=new Connect();
        //设置链接字符串(因为链接字符串大多时候不一样,所以我只能把它放在外面)
        String sqlstr="select * from user where username='" + username
                    + "' and password='" + old_password + "'";
        //调用sqlstr属性的封装函数设置connect对象的属性值
        connect.setSqlstr(sqlstr);
        //设置记录搜索到记录数的变量
        int totalRows=0;
        try {
            //调用connect中写好的方法,进行查询
            totalRows=connect.excute_query();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (totalRows > 0) {

            //当数据库中有这个人时,我才允许你修改密码
            sqlstr="update user set password='"+new_password+"' where username='"+username+"'";
            connect.setSqlstr(sqlstr);
            try {
                //调用connect的另一个方法,更新该记录的密码属性值
                connect.excute_update();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //更新后提示用户,已经修改完成
            out.print("<html>");
            out.print("<head></head>");
            out.print("<body>修改成功!</body>");
            out.print("</html>");
            //给个返回的超链接吧
        } else {
            //如果没有这个人,需要让他先去申请一个账号
            out.print("<html>");
            out.print("<head></head>");
            out.print("<body>请先去注册一个账号!</body>");
            out.print("</html>");
            //给个超链接跳到注册页面
        }



        /*
        String user = "root";
        String pass = "";

        String url = "jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=UTF-8";

        String driver = "com.mysql.jdbc.Driver";

        String sqlstr;

        Connection connection = null;

        java.sql.Statement statement = null;

        ResultSet result = null;

        try {

            Class.forName(driver);

            connection = DriverManager.getConnection(url, user, pass);

            statement = connection.createStatement();

            sqlstr = "select * from user where username='" + username
                    + "' and password='" + old_password + "'";

            result =statement.executeQuery(sqlstr);
            result.last();

            int totalRows = result.getRow();

            if (totalRows > 0) {

                sqlstr="update user set password='"+new_password+"' where username='"+username+"'";
                statement.executeUpdate(sqlstr);
                out.print("<html>");
                out.print("<head></head>");
                out.print("<body>修改成功!</body>");
                out.print("</html>");
                //给个返回的超链接吧
            } else {

                out.print("<html>");
                out.print("<head></head>");
                out.print("<body>请先去注册一个账号!</body>");
                out.print("</html>");
                //给个超链接跳到注册页面
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (result != null)
                try {
                    result.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (connection != null)
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        */
    }

}

现在是MyBean.java:

/**
 * 创建这个类是因为,用户可以被作为一个对象处理,这样更符合面向对象的要求
 */
package student;

//开始实现这个类
public class MyBean {

    //构造方法
    public MyBean() {
        // TODO Auto-generated constructor stub
    }

    //数据成员的封装函数
    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;
    }

    private String username;
    private String password;


}

然后是链接类:

//这里写的是我的链接对象
package student;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Connect {

    //构造函数
    public Connect() {
        // TODO Auto-generated constructor stub
    }

    //初始化需要的变量(登陆数据库需要的信息)
    String user = "root";
    String pass = "";
    //数据源
    String url = "jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=UTF-8";
    //数据库的驱动
    String driver = "com.mysql.jdbc.Driver";
    //sql语句变量
    private String sqlstr;
    //链接对象
    Connection connection = null;
    //语句对象(所谓语句对象其实就是用来执行sql命令的)
    java.sql.Statement statement = null;
    //结果集(对于有记录返回的sql操作,你得把它存在一个记录集里面)
    ResultSet result = null;

    //这个函数是用来执行有返回结果的sql操作
    public int excute_query() throws ClassNotFoundException, SQLException{
        //加载驱动
        Class.forName(driver);
        //创建链接
        connection = DriverManager.getConnection(url, user, pass);
        //创建语句对象
        statement = connection.createStatement();
        //执行sql语句
        result =statement.executeQuery(getSqlstr());
        //游标指向最后一条记录
        result.last();
        //获取最后一条记录的行数
        int totalRows = result.getRow();
        //返回给函数调用处,用于判断
        return totalRows;
    }

    //这个函数用来执行没有返回结果集的sql操作
    public void excute_update() throws ClassNotFoundException,SQLException{
        Class.forName(driver);
        connection = DriverManager.getConnection(url, user, pass);
        statement = connection.createStatement();
        statement.executeUpdate(getSqlstr());

    }

    //sqlstr属性的封装函数
    public String getSqlstr() {
        return sqlstr;
    }

    public void setSqlstr(String sqlstr) {
        this.sqlstr = sqlstr;
    }
}

最后是配置文件web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Judge</servlet-name>
    <servlet-class>Judge</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Register_Judge</servlet-name>
    <servlet-class>Register_Judge</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Change_Password</servlet-name>
    <servlet-class>Change_Password</servlet-class>
  </servlet>



  <servlet-mapping>
    <servlet-name>Judge</servlet-name>
    <url-pattern>/servlet/Judge</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Register_Judge</servlet-name>
    <url-pattern>/servlet/Register_Judge</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Change_Password</servlet-name>
    <url-pattern>/servlet/Change_Password</url-pattern>
  </servlet-mapping>

</web-app>

运行结果:

1.这是我的IDE界面及目录结构

2.我数据库中的记录

3.开始登陆

4.登陆成功后从session中取出用户名

5.注册一个用户

6.如果用户不存在,就可以创建成功

7.去修改密码时,没有输入全部需要的信息

8.修改密码时,用户输入的新旧密码一致

9.修改密码成功

10.修改密码后,看看数据库,嗯,成功修改了

问题

这里没有截用户登录失败和用户注册失败的图片,并不是没有对其进行处理,只是因为博主已经截完图且准备去看更新的动漫了,所以大家要相信我,功能看代码就可以知道有没有实现。
代码之中确实还是存在一些不完善的地方,本次的实验旨在为大家介绍如何结合MVC与JavaBean进行Java Web开发,大家具体在实现的时候要多加注意。

总结

写代码的过程中遇到的问题,不要怕,去找解决方法就好,没什么好怕的。
其实写代码是一件很快乐的事情,如果你真的感受到它带给你的乐趣的话。我看linux之父在Ted上的采访时,才知道最初是他一个人独立的完成了linux与git的代码。这些人才是真正推动社会进步的人。我每天打开搜索引擎,只是在textbox中点一下,下拉十条新闻中,一半都是消极的东西,我很想去改变这些东西,我想帮助那些人,我不知道什么时候我才可以拥有那样的能力,我会努力的,你们也要加油。
这是我的博客,我说了一些不知道该对谁说的话。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值