gpt4 book ai didi

java - 服务器遇到内部错误,导致其无法满足此请求 - 在 servlet 3.0 中

转载 作者:行者123 更新时间:2023-11-30 03:59:18 26 4
gpt4 key购买 nike

我想使用 servlet 运行简单的注册 jsp 页面。它抛出此错误消息:

description The server encountered an internal error that prevented it from fulfilling this request.

java.lang.NullPointerException
com.java.task11.utils.ValidationUtils.isEmailValid(ValidationUtils.java:22)
com.java.task11.webapp.RegistrationServlet.validateInputs(RegistrationServlet.java:95)
com.java.task11.webapp.RegistrationServlet.processRegistration(RegistrationServlet.java:66)
com.java.task11.webapp.RegistrationServlet.doPost(RegistrationServlet.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的registration.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<c:set var="language"
value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}"
scope="session"/>
<fmt:setLocale value="${language}"/>
<fmt:setBundle basename="com.java.task11.i18n.text"/>

<html lang="${language}">
<head>
<title>Registration</title>
<jsp:include page="parts/header.jsp"/>
</head>

<body>
<div class="container-fluid registration">
<div class="row">
<div class="login-screen">
<div class="login-part">
<div class="login-ico col-md-1 col-sm-2 col-xs-12 col-md-offset-3">
<h4>
<small><fmt:message key="registration.label"/></small>
</h4>
</div>
<div class="login-form col-md-4 col-sm-8 col-xs-12 col-md-offset-1 col-sm-offset-1">
<form action="/registration" enctype="multipart/form-data" method="post">
<%-- error messages --%>
<div class="form-group">
<c:forEach items="${registrationErrors}" var="error">
<p class="error">${error}</p>
</c:forEach>
</div>
<%-- input fields --%>
<div class="form-group">
<input class="form-control" placeholder="<fmt:message key="employee.firstName"/>" name="first_name" required
id="first-name"/>
<label class="login-field-icon fui-user"></label>
</div>
<div class="form-group">
<input class="form-control" placeholder="<fmt:message key="employee.lastName"/>" name="last_name" required
id="last-name"/>
<label class="login-field-icon fui-user"></label>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
<fmt:message key="button.browse"/>
<input type="file" name="userImage" accept="image/*"/>
</span>
</span>
<input type="text" class="form-control" readonly="">
</div>
</div>
<div class="form-group">
<input class="form-control" type="email" placeholder="<fmt:message key="login.email"/>" name="email"
pattern="[^ @]*@[^ @]*\.[^ @]{2,}" required id="email"/>
<label class="login-field-icon fui-mail"></label>
</div>
<div class="form-group">
<input class="form-control" type="password" placeholder="<fmt:message key="employee.password"/>" name="password" required
id="password"/>
<label class="login-field-icon fui-lock"></label>
</div>
<div class="form-group">
<input class="form-control" type="text" placeholder="<fmt:message key="employee.position"/>" name="position" required
id="position"/>
<label class="login-field-icon fui-plus"></label>
</div>
<div class="form-group">
<button class="btn btn-primary btn-lg btn-block" name="submit"
type="submit" value="Submit">
<fmt:message key="button.submit"/>
</button>
<a class="login-link" href="<c:url value="/login"/>"><fmt:message key="registration.login"/></a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<jsp:include page="parts/scripts.jsp"/>
</body>
</html>

RegistrationServlet:

@WebServlet("/registration")
public class RegistrationServlet extends HttpServlet {
private static Logger log = Logger.getLogger(RegistrationServlet.class);
private EmployeeService employeeService = new EmployeeService();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
request.getRequestDispatcher("/pages/registration.jsp").forward(request, response);
} catch (ServletException | IOException e) {
log.error(e);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
processRegistration(request, response);
} catch (ServletException | IOException e) {
log.error(e);
e.printStackTrace();
}
}

private void processRegistration(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String firstName = request.getParameter("first_name");
String lastName = request.getParameter("last_name");
String email = request.getParameter("email");
String password = request.getParameter("password");
String imageName = "default.png";
String position = request.getParameter("position");

System.out.printf("Request fields: %s %s %s %s %s%n", firstName, lastName, email, password, position);

Part filePart = request.getPart("userImage");
try {
String contentType = filePart.getContentType();
if (contentType.startsWith("image")) {
File image = FileUploadUtils.uploadFile(this, "img\\employees", filePart);
imageName = FileUploadUtils.getFilename(image);
}
} catch (Exception e) {
log.error(e);
}

List<String> registrationErrors = validateInputs(firstName, lastName, email, password, position);

if (registrationErrors.size() > 0) {
request.setAttribute("registrationErrors", registrationErrors);
request.getRequestDispatcher("/pages/registration.jsp").forward(request, response);
} else {
Employee employee = new Employee();
employee.setFirstName(firstName);
employee.setLastName(lastName);
employee.setEmail(email);
employee.setEncryptedPassword(password);
employee.setImage(imageName);
employee.setPosition(position);

employeeService.save(employee);
response.sendRedirect("/login");
}

}

private List<String> validateInputs(String firstName, String lastName, String email, String password, String position) {
List<String> registrationErrors = new ArrayList<>();

if (ValidationUtils.isNullOrEmpty(firstName)) {
registrationErrors.add(ValidationErrors.FIRST_NAME);
}
if (ValidationUtils.isNullOrEmpty(lastName)) {
registrationErrors.add(ValidationErrors.LAST_NAME);
}
if (!ValidationUtils.isEmailValid(email)) {
registrationErrors.add(ValidationErrors.EMAIL);
}
if (employeeService.getByEmail(email).getId() != 0) {
registrationErrors.add(ValidationErrors.EMAIL_ALREADY_PRESENT);
}
if (ValidationUtils.isNullOrEmpty(password)) {
registrationErrors.add(ValidationErrors.PASSWORD);
}
if (!ValidationUtils.isNullOrEmpty(position)) {
registrationErrors.add(ValidationErrors.POSITION_EMPTY);
}
return registrationErrors;
}
}

无法从请求参数中提取。根据我的简单检查,它打印出:

Request fields: null null null null null

我不明白为什么会发生这种情况?

有什么建议吗?

最佳答案

在这里:

    if (ValidationUtils.isNullOrEmpty(lastName)) {
registrationErrors.add(ValidationErrors.LAST_NAME);
}
if (!ValidationUtils.isEmailValid(email)) {
registrationErrors.add(ValidationErrors.EMAIL);
}

您检查姓氏是否为 null 或空值,但在 isEmailValid 中您不检查空值。像这样的事情应该做

    if (ValidationUtils.isNullOrEmpty(email) || !ValidationUtils.isEmailValid(email)) {
registrationErrors.add(ValidationErrors.EMAIL);
}

或者更好的是,修复您的 ValidationUtils.isEmailValid() 以处理空电子邮件值。它不应该崩溃,它应该返回 false。

关于java - 服务器遇到内部错误,导致其无法满足此请求 - 在 servlet 3.0 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22335466/

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