我正在尝试建立用户注册页面。
我的servlet看起来像这样
package main.com.attempt.trial.servlet;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserRegisterServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response){
Logger logger = Logger.getLogger("LOGGER");
logger.info("doGet() called");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
Logger logger = Logger.getLogger("LOGGER");
String name = (String) request.getParameter("firstname");
boolean isSuccess = true;
logger.info("doPost() called");
logger.info(name);
if (isSuccess){
response.sendRedirect("/userHomePage.html");
}else{
response.sendRedirect("/register.html");
}
}
}
我正在尝试将用户重定向到另一个JSP页面或返回到注册页面,具体取决于注册是否成功。我将web页面映射到web.xml中,如下所示
<servlet>
<servlet-name>UserRegisterServlet</servlet-name>
<servlet-class>main.com.attempt.trial.servlet.UserRegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserRegisterServlet</servlet-name>
<url-pattern>/registerUser.serv</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserRegisterPage</servlet-name>
<jsp-file>/WEB-INF/jsp/register.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>UserRegisterPage</servlet-name>
<url-pattern>/register.html</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserHomePage</servlet-name>
<jsp-file>/WEB-INF/jsp/userHomePage.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>UserHomePage</servlet-name>
<url-pattern>/userHomePage.html</url-pattern>
</servlet-mapping>
如果我发送重定向到“/registerUser.serv”,servlet中的doGet()方法会像我期望的那样被调用。 Tomcat不记录任何错误,local_host_access_log看起来也像我期望的那样。
127.0.0.1 - - [28/Jul/2014:14:45:20 -0700] "GET /register.html HTTP/1.1" 200 1576
127.0.0.1 - - [28/Jul/2014:14:45:20 -0700] "GET /content/jquery-2.1.1.js HTTP/1.1" 304 -
127.0.0.1 - - [28/Jul/2014:14:45:22 -0700] "POST /registerUser.serv HTTP/1.1" 302 -
127.0.0.1 - - [28/Jul/2014:14:45:22 -0700] "GET /userHomePage.html HTTP/1.1" 200 433
所以,似乎服务器端一切正常,但浏览器根本不知道它需要转到另一个页面。此外,如果我尝试重定向到JSP页面,静态HTML页面,或者如果我使用相对或绝对路径,则无关紧要。
有任何帮助吗?提前谢谢。