【发布时间】:2015-03-12 16:53:35
【问题描述】:
我不明白这个错误500和cookie问题,希望有人能启发我。
当我运行我的 requestDispatcher 时,我得到一个我无法弄清楚的内部错误 500。资源是 /WEB-INF/jsp/index.jsp 但在错误消息中显示 /WEB-INF/jsp/cookie???
错误信息
HTTP 状态 500 - javax.servlet.ServletException: javax.servlet.jsp.JspException:java.io.FileNotFoundException: 请求的资源 (/MyNewRandomBlog1.0/WEB-INF/jsp/{cookie=JSESSIONID=4724BBA140EA29EFF07AD782C755ED13, 缓存控制=无缓存,连接=保持活动,主机=本地主机:8080, 接受语言=da,en-US;q=0.7,en;q=0.3, 接受=图像/jpeg, 应用程序/x-ms-应用程序、图像/gif、应用程序/xaml+xml、 图像/pjpeg,应用程序/x-ms-xbap,应用程序/vnd.ms-excel, 应用程序/vnd.ms-powerpoint,应用程序/msword,/, 用户代理=Mozilla/5.0(兼容;MSIE 9.0;Windows NT 6.2;Win64; x64;三叉戟/7.0; ASU2JS),接受编码=gzip,放气, ua-cpu=AMD64}) 不可用
我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MyNewRandomBlog1.0</display-name>
<welcome-file-list>
<welcome-file>frontpage</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>intname</servlet-name>
<servlet-class>dk.danicait.servlets.FrontpageCreation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>intname</servlet-name>
<url-pattern>/frontpage</url-pattern>
</servlet-mapping>
</web-app>
我的文件树
另外我在我的程序中没有使用任何cookie,所以它在消息中显示的cookie是标准cookie?
只是为了确保 requestDispatcher 的文件确实存在,我这样做了
String test = sc.getResource("/WEB-INF/jsp/index.jsp").toString();
获取资源网址效果很好。
编辑。添加了servlet代码
public class FrontpageCreation extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext sc = getServletContext();
// Set request attributes
request.setAttribute("header", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());
// Request dispatcher
getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
}
}
【问题讨论】:
-
您将您的 serlvet 定义为
intname映射到/frontpage,因此请尝试将其调用为http://<server>:<port>/MyNewRandomBlog1.0/frontpage- 顺便说一句,WEB-INF内的所有资源都受到保护,无法从 Web 应用程序外部直接访问(即来自用户的浏览器) -
感谢越智的回复。据我了解欢迎文件中的首页,/forntpage 和 intname 都是我们想要的内部值。我也在其他项目中使用这种风格,没有问题。我将 .jsp 文件放在 WEB-INF 文件中,因此我可以先执行一个 servlet 调用,这将依次生成 index.jsp 和所有其他 .jsp 页面。因此可以将动态内容插入到页面中,用户不能直接浏览它们。当我打电话给localhost:8080/MyNewRandomBlog1.0/frontpage 时,我得到了错误。
-
你能分享一些来自
FrontpageCreationservlet 的代码吗? -
最后分享了原题中的servlet代码。
-
尝试更改
header和footer的路径以在前面包含.(点) - 如./includes/nav.jsp- 或者尝试先删除这两个属性,看看是否简化的index.jsp(带有普通的 hello world 消息)正在正确地提供给浏览器