如何以良好的格式化方式打印/显示错误?

时间:2016-01-22 09:33:17

标签: validation jsp servlets

我目前正在java eclipse IDE上做一个项目。我有一个servlet,显示密码更改功能。正如您所看到的,我根据用户输入编程返回的各种错误,例如: “密码不匹配”。但是,它始终打印在屏幕顶部。有什么办法可以将这些错误输出为实际的格式错误吗?也许以弹出框的形式或至少将其放置在适当的位置?

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class NewPassword extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();

                String oldpassword = request.getParameter("oldpassword"); 
                String newpassword = request.getParameter("newpassword"); 
                String confirmpassword = request.getParameter("confirmpassword"); 

                int newpass = newpassword.length(); 

                HttpSession session = request.getSession(false);
                String employeeid = ""; 

                if(session != null) { 
                    employeeid = (String)session.getAttribute("employeeid"); 
                }

                boolean st = false; 
                try { 
                    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", ""); 
                    PreparedStatement ps = con.prepareStatement("SELECT pwd FROM payroll_system.employee_login WHERE employeeID = ?");
                    ps.setString(1, employeeid);
                    ResultSet rs = ps.executeQuery(); 
                    st = rs.next(); 
                    String currentpassword = rs.getString("pwd"); 
                    if(st) { 

                        if((currentpassword.equals(oldpassword)) && newpassword.equals(confirmpassword) && newpass >= 8) { 
                            PreparedStatement pd = con.prepareStatement("UPDATE payroll_system.employee_login SET pwd = ? where employeeID = ?" ); 
                            pd.setString(1, newpassword);
                            pd.setString(2, employeeid);
                            int updated = pd.executeUpdate(); 
                            if(updated > 0){
                                out.println("Password Successfully Updated");
                                RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html");
                                rd.include(request, response);
                            }
                        }

                        else if(oldpassword.equals("") || newpassword.equals("")  || confirmpassword.equals("") ) { 
                            out.println("Fields cannot be left blank.");
                            RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html");
                            rd.include(request, response);
                        }
                        else if(!currentpassword.equals(oldpassword) && !newpassword.equals(confirmpassword)) { 
                            out.println("Old password is incorrect and passwords do not match, please try again.");
                            RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html"); 
                            rd.include(request, response); 
                        }
                        else if(!currentpassword.equals(oldpassword)) { 
                            out.println("Old password entered incorrectly, please try again.");
                            RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html");
                            rd.include(request, response);
                        }
                        else if(!newpassword.equals(confirmpassword)) { 
                            out.println("Passwords do not match, please try again.");
                            RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html");
                            rd.include(request, response);
                        }
                        else if(newpass < 8 ) {
                            out.println("New Password must be atleast 8 characters in length.");
                            RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html");
                            rd.include(request, response);
                        }



                    }
                }catch(Exception e)
                 {
                     e.printStackTrace();
                 }
               out.close();
       }
   }

3 个答案:

答案 0 :(得分:1)

你可以使用ajax和js来处理错误。使用ajax与服务器通信,当得到错误时,使用js函数显示一个对话框。这只是一个解决方案。你可以试试这个。

答案 1 :(得分:1)

您可以将spring-form标签与bean验证一起使用。

例如,在jsp中,您可以显示特定字段的错误:

C

答案 2 :(得分:0)

您可以使用log4j之类的日志框架。例如,在Log4j中,有记录器,追加器和布局。借助布局,您可以设计自己的错误消息的格式。

A short introduction to Log4j