我尝试使用tomacat登录后将请求转发到jsp文件。但它(servlet)不转发请求。任何人都可以在这里找出错误吗?
Servlet:
public class AuthenticationServer extends HttpServlet {
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request, response);
}
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService (request, response);
}
public void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getRemoteUser();
request.setAttribute("user", user);
RequestDispatcher dispatcher = request.getRequestDispatcher("/" + request.getParameter("direct"));
dispatcher.forward(request, response);
}
}
当我打印"/" + request.getParameter("direct")
时,会打印出/welcome.jsp
。但它只是没有转发它。
答案 0 :(得分:2)
request.getRequestDispatcher(String path);
指定的路径可能是相对的,但它不能扩展到当前的servlet上下文之外。如果路径以“/”开头,则会将其解释为相对于当前上下文根。如果servlet容器无法返回RequestDispatcher,则此方法将返回null
。请尝试:RequestDispatcher dispatcher = request.getRequestDispatcher(request.getParameter("direct"));
答案 1 :(得分:1)
如果您可以指定错误,则可以更轻松地解决您的问题......
问题可能是因为找不到jsp视图。 在getRequestDispatcher()中放入“/”时,路径相对于应用程序的根目录。如果http://localhost:8080是您的根,则您的网址将为http://localhost:8080/YourApp/welcome.jsp
您可以获得更多解释here