从jsp重定向jsp – 错误“请求的资源不可用。”
我正在社交网站上工作,我有以下问题。 在profile.jsp
我有一个用户可以上传照片的表单。 这个表单对FileUploadHandler
servlet有一个动作,它上传照片然后发送重定向到uploadfileController.jsp
如下所示:
RequestDispatcher requestDispatcher; requestDispatcher = request.getRequestDispatcher("/uploadfileController.jsp"); requestDispatcher.forward(request, response);
在uploadfileController.jsp
我将此post插入到我的MySQL数据库中,然后将重定向发送到profile.jsp
。
response.sendRedirect("/profile.jsp");
但后来我收到此错误消息:
HTTP状态404 – /profile.jsp
类型状态报告
消息/profile.jsp
description请求的资源不可用。
但是,当我再次访问profile.jsp
,post就会被创建! 有任何想法吗?
如果响应是相对于servlet上下文,那么将上下文路径预先添加到URL。
String contextPath = request.getContextPath(); response.sendRedirect(response.encodeRedirectURL(contextPath + "/profile.jsp");
您可以将sendRedirect
与相对地址一起使用。 例如,如果servlets URL是
并且您将使用sendRedirect("bar/other")
您将被重定向到相同上下文中的不同资源
http://www.serwer.com/yourwebapp/foo/ bar / other
但是如果你在sendRedirect
开头添加/
那么/
将代表你服务器的主目录,
http://www.serwer.com/ ^this one
所以sendRedirect("/bar/other")
会将你重定向到
http://www.serwer.com/ bar / other
因此,当您看到yourwebapp
和foo
已从URL中删除时。
在你的情况下,似乎问题是缺少yourwebapp
。 你可以通过很多方式解决它。 例如,你可以:
- 从重定向位置的开始删除
/
使其相对于当前URL - 在
sendRedirect("/yourAppName/profile.jsp")
之后添加应用程序名称(您可以通过使用Roman C +1 提及的request.getContextPath()
动态获取/yourAppName
以获取其信息)