重定向到同一页面会导致空白页面
Redirecting to the same page results in blank page
我正在创建一个 java
servlet web 应用程序,我在 index.jsp
处接收用户输入并在 POST
操作后在 result.jsp
页面显示结果。在提交表单之前,我验证用户输入。如果发现任何验证错误,我希望 redirect
用户访问包含错误消息的同一 index.jsp
页面。但是 redirect
操作会导致空白页。这是我到目前为止所做的 -
Servlet class doPost
方法 -
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
//this is just a dto class
CovidQa covidQa = new CovidQa(name);
CovidTestValidator validator = new CovidTestValidator();
boolean hasError = validator.validate(covidQa, req);
if (hasError) {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
req.getRequestDispatcher("/result.jsp").forward(req, resp);
}
验证者的 validate
方法-
public boolean validate(CovidQa covidQa, HttpServletRequest request) {
boolean hasError = false;
if (covidQa.getName() == null || covidQa.getName().equals("")) {
request.setAttribute("nameErr", "Name can not be null");
hasError = true;
}
return hasError;
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Covid-19</servlet-name>
<servlet-class>CovidTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Covid-19</servlet-name>
<url-pattern>/Cov</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
-
<form action="Cov" method="post">
//input fields
<label>Name</label>
<input type="text"
id="name"
name="name">
<span>${nameErr}</span>
<button type="submit">Submit</button>
</form>
req.getRequestDispatcher()
不会从方法中隐式 return
。如果您没有明确提及 return
,编译器也会执行直接行。
正如@BalusC 评论的那样,系统通过连续执行两个 req.getRequestDispatcher()
语句而感到困惑。
要么您需要 return
明确 -
if (hasError) {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("/result.jsp").forward(req, resp);
或者,将后一个放在 else
块中 -
if (hasError) {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
} else {
req.getRequestDispatcher("/result.jsp").forward(req, resp);
}
我正在创建一个 java
servlet web 应用程序,我在 index.jsp
处接收用户输入并在 POST
操作后在 result.jsp
页面显示结果。在提交表单之前,我验证用户输入。如果发现任何验证错误,我希望 redirect
用户访问包含错误消息的同一 index.jsp
页面。但是 redirect
操作会导致空白页。这是我到目前为止所做的 -
Servlet class doPost
方法 -
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
//this is just a dto class
CovidQa covidQa = new CovidQa(name);
CovidTestValidator validator = new CovidTestValidator();
boolean hasError = validator.validate(covidQa, req);
if (hasError) {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
req.getRequestDispatcher("/result.jsp").forward(req, resp);
}
验证者的 validate
方法-
public boolean validate(CovidQa covidQa, HttpServletRequest request) {
boolean hasError = false;
if (covidQa.getName() == null || covidQa.getName().equals("")) {
request.setAttribute("nameErr", "Name can not be null");
hasError = true;
}
return hasError;
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Covid-19</servlet-name>
<servlet-class>CovidTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Covid-19</servlet-name>
<url-pattern>/Cov</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
-
<form action="Cov" method="post">
//input fields
<label>Name</label>
<input type="text"
id="name"
name="name">
<span>${nameErr}</span>
<button type="submit">Submit</button>
</form>
req.getRequestDispatcher()
不会从方法中隐式 return
。如果您没有明确提及 return
,编译器也会执行直接行。
正如@BalusC 评论的那样,系统通过连续执行两个 req.getRequestDispatcher()
语句而感到困惑。
要么您需要 return
明确 -
if (hasError) {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("/result.jsp").forward(req, resp);
或者,将后一个放在 else
块中 -
if (hasError) {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
} else {
req.getRequestDispatcher("/result.jsp").forward(req, resp);
}