gpt4 book ai didi

java - ConcurrentHashMap 在我的 Java Web 应用程序中陷入无限循环?

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:29 26 4
gpt4 key购买 nike

我对网络开发总体来说是新手,但到目前为止我正在做基本的事情,所以我不知道为什么这不起作用。我的 servlet 收到向数据库添加新用户的请求,但在此之前我首先想使用正则表达式检查值。

所以我的想法是将所有参数名称和正则表达式模式放在 HashMap 中,然后迭代该映射,从请求对象获取参数并返回一个仅包含无效字段的数组(目前)。然而,似乎我可能会陷入无限循环,因为我找不到其他解释为什么这不起作用。

我不确定这是否与线程有关,因为我只会读取它,而不会在运行时修改它,但无论如何我从 HashMap 切换到 Concurrent。看起来太简单了,不会出错。所以这里是:

public class FormValidator {

public ArrayList<String> Validate(HttpServletRequest request) {
ArrayList<String> invalidFields = new ArrayList<String>();
ConcurrentHashMap<String, String> fieldRegexMap = new ConcurrentHashMap<String, String>();
fieldRegexMap.put("username", ".{8,}");
fieldRegexMap.put("email", "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$)");
fieldRegexMap.put("password", "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,10}$");
//fieldRegexMap.put("confirmPassword", "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,10}$");
fieldRegexMap.put("firstname", ".{1,20}");
fieldRegexMap.put("lastname", ".{4,20}");
fieldRegexMap.put("DOB", "^(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$\\");

fieldRegexMap.put("city", ".{2,20}");
fieldRegexMap.put("address", ".{2,20}");
fieldRegexMap.put("profession", ".{2,20}");
fieldRegexMap.put("interests", ".{,100}");
fieldRegexMap.put("moreinfo", ".{,500}");

//Get all parameters from the request and validate them key:parametername value:regex
for (Map.Entry<String, String> entry : fieldRegexMap.entrySet()) {
if (!(request.getParameter(entry.getKey()).matches(entry.getValue()))) {
invalidFields.add(entry.getKey());
}

}

return invalidFields;
}
}

然后我的 servlet 的 doPost 调用 ProcessRequest:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

ArrayList<String> results = new ArrayList<String>();

out.println("<h1>Servlet lqRegisterServlet at " +
request.getContextPath() + "</h1>");

out.println("<h2>Method: " + request.getMethod() + "</h2>");
out.println("This Shows up");

FormValidator validator = new FormValidator();

out.println("Everything shows up to this point!");
results = validator.Validate(request);
out.println("This does not show");
for (String param : results) {

out.println("<p>Field:" + param + "</p>");
}

}
response.getOutputStream().println("This is servlet response");
}

我不知道这是否是检查字段的最佳方式,这是我学习java web开发的一个项目。但这是我能想到的唯一对我来说有意义并且似乎可以重复使用的方法。我计划在验证函数之外创建并填充 HashMap 。

感谢您的宝贵时间

最佳答案

您的 servlet 调用没有被卡住,而是失败并出现错误,因为至少 DOB 的正则表达式是错误的。我通过编写一个简短的 main 方法发现了这一点:

public static void main(String[] args) {
ArrayList<String> invalidFields = null;
ConcurrentHashMap<String, String> fieldRegexMap = new ConcurrentHashMap<String, String>();
fieldRegexMap.put("username", ".{8,}");
fieldRegexMap.put("email", "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$)");
fieldRegexMap.put("password", "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,10}$");
//fieldRegexMap.put("confirmPassword", "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,10}$");
fieldRegexMap.put("firstname", ".{1,20}");
fieldRegexMap.put("lastname", ".{4,20}");
fieldRegexMap.put("DOB", "^(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$\\");

fieldRegexMap.put("city", ".{2,20}");
fieldRegexMap.put("address", ".{2,20}");
fieldRegexMap.put("profession", ".{2,20}");
fieldRegexMap.put("interests", ".{,100}");
fieldRegexMap.put("moreinfo", ".{,500}");

fieldRegexMap.entrySet().stream()
.forEach(elem -> {
System.out.println(elem.getKey());
System.out.println(Pattern.compile(elem.getValue()));
});
}

这会产生以下输出:

profession
.{2,20}
password
^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,10}$
firstname
.{1,20}
address
.{2,20}
city
.{2,20}
DOB
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 325
^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$\
^
at java.util.regex.Pattern.error(Pattern.java:1955)
at java.util.regex.Pattern.compile(Pattern.java:1702)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at IntArraySplitter.lambda$0(IntArraySplitter.java:30)
at IntArraySplitter$$Lambda$1/1995265320.accept(Unknown Source)
at java.util.concurrent.ConcurrentHashMap$EntrySpliterator.forEachRemaining(ConcurrentHashMap.java:3606)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at IntArraySplitter.main(IntArraySplitter.java:28)

您看不到这一点的原因是您已经向客户端发送了响应。由于服务器已返回 HTTP 200 响应代码,因此无法将其更改为 HTTP 500,因此在到达此点时仅关闭与客户端的连接。不过,您应该在服务器日志中看到一些错误消息。

关于java - ConcurrentHashMap 在我的 Java Web 应用程序中陷入无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47444846/

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