gpt4 book ai didi

validation - 如何以漂亮的格式打印/显示错误?

转载 作者:行者123 更新时间:2023-12-01 03:35:53 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How perform validation and display error message in same form in JSP?

(3 个回答)


5年前关闭。




我目前正在 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();
}
}

最佳答案

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

关于validation - 如何以漂亮的格式打印/显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com