• Homework1——印象最深刻的error


      我印象最深刻的error就是JAVA中equals和==的区别。

      这是我在写web大作业时出现的错误:

     1 public void doPost(HttpServletRequest request, HttpServletResponse response)
     2             throws ServletException, IOException {    
     3         //String checkcode = (String) request.getSession().getAttribute("safecode");
     4         //System.out.println(checkcode);
     5         request.setCharacterEncoding("UTF-8");
     6         response.setCharacterEncoding("UTF-8");
     7         response.setContentType("text/html;charset=UTF-8");        String id = request.getParameter("id");
     8         String password = request.getParameter("password");
     9         String password1=request.getParameter("password1");
    10         PrintWriter out = response.getWriter();
    11         MessagerDB messagerDB=new MessagerDB(0);
    12         try {
    13                 if(id=="")
    14                 {
    15                     out.write("<html><head><title>两次密码不一致</title></head><body>");
    16                     out.write("<h1>对不起,用户名不能为空</h1></body></html>");
    17                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    18                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    19                     out.flush();
    20                 }
    21                 else if(password=="")
    22                 {
    23                     out.write("<html><head><title>两次密码不一致</title></head><body>");
    24                     out.write("<h1>对不起,密码不能为空</h1></body></html>");
    25                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    26                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    27                     out.flush();
    28                 }
    29                 else if(!password==password1)
    30                 {
    31                     out.write("<html><head><title>两次密码不一致</title></head><body>");
    32                     out.write("<h1>对不起,您两次输入的密码不一致,请重新输入</h1></body></html>");
    33                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    34                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    35                     out.flush();
    36                 }
    37                 else if(messagerDB.add(id,password,"1")){
    38                     response.sendRedirect("/redcross1/messager.jsp");
    39                 } 
    40                 else
    41                 {
    42                     out.write("<html><head><title>FailedLogin</title></head><body>");
    43                     out.write("<h1>对不起,此ID已经被注册,请重新输入</h1></body></html>");
    44                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    45                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    46                     out.flush();
    47                 }
    48         } catch (Exception e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         }
    52     }

      String类的比较,我全部用的是==来比较的,导致密码比较的时候总是错误,即使输入正确的密码,也无法成功进入到程序中。这个错误困扰了我好长时间。最后,我通过在每个if中输出字符,才发现原来是判断String类是否相等时出现了错误,导致程序根本没有进入到任何一个if中。通过百度才知道,原来判断两个String的值是否相等应该用a.equals(b),这种方法来判断,而==判断的是两个String的地址是否相等,和C++中的判断方法不同。然后,我将代码修改成为以下这样:

     1 public void doPost(HttpServletRequest request, HttpServletResponse response)
     2             throws ServletException, IOException {    
     3         //String checkcode = (String) request.getSession().getAttribute("safecode");
     4         //System.out.println(checkcode);
     5         request.setCharacterEncoding("UTF-8");
     6         response.setCharacterEncoding("UTF-8");
     7         response.setContentType("text/html;charset=UTF-8");        String id = request.getParameter("id");
     8         String password = request.getParameter("password");
     9         String password1=request.getParameter("password1");
    10         PrintWriter out = response.getWriter();
    11         MessagerDB messagerDB=new MessagerDB(0);
    12         try {
    13                 if(id.equals(""))
    14                 {
    15                     out.write("<html><head><title>两次密码不一致</title></head><body>");
    16                     out.write("<h1>对不起,用户名不能为空</h1></body></html>");
    17                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    18                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    19                     out.flush();
    20                 }
    21                 else if(password.equals(""))
    22                 {
    23                     out.write("<html><head><title>两次密码不一致</title></head><body>");
    24                     out.write("<h1>对不起,密码不能为空</h1></body></html>");
    25                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    26                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    27                     out.flush();
    28                 }
    29                 else if(!password.equals(password1))
    30                 {
    31                     out.write("<html><head><title>两次密码不一致</title></head><body>");
    32                     out.write("<h1>对不起,您两次输入的密码不一致,请重新输入</h1></body></html>");
    33                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    34                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    35                     out.flush();
    36                 }
    37                 else if(messagerDB.add(id,password,"1")){
    38                     response.sendRedirect("/redcross1/messager.jsp");
    39                 } 
    40                 else
    41                 {
    42                     out.write("<html><head><title>FailedLogin</title></head><body>");
    43                     out.write("<h1>对不起,此ID已经被注册,请重新输入</h1></body></html>");
    44                     out.write("<meta http-equiv="Content-Language" content="zh-CN">"
    45                             + "<meta http-equiv="refresh" content="1;url=/redcross1/add.jsp">");
    46                     out.flush();
    47                 }
    48         } catch (Exception e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         }
    52     }

      通过更改使得程序可以正确运行,即输入正确的密码可以进入到系统中。

  • 相关阅读:
    Win2019 preview 版本的安装过程
    Windows 下 Docker 的简单学习使用过程之三 创建images 导出images
    Windows 下 Docker 的简单学习使用过程之二 Docker For windows
    Windows 下 Docker 的简单学习使用过程之一 dockertoolbox
    Helm 安装 nginx-ingress 的方法
    libc.so.6被删后导致系统无法使用的原因及解决方法
    centos6.x升级glibc-2.17
    jmx远程访问权限设置
    ngxtop
    nginx: [emerg] unknown directive "stub_status" in /usr/local/openresty/nginx/conf/conf.d/ngx_metric.conf:19
  • 原文地址:https://www.cnblogs.com/wsruning/p/5242416.html
Copyright © 2020-2023  润新知