我需要在我的 servlet 中包含 .jsp 文件。
我写了一个简单的jsp文件,我把它放在了dir WEB-INF/jsps/中:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title> Galleries </title>
</head>
<body>
Test
</body>
</html>
和小服务程序:
package photoGallery;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class GalleriesServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
System.out.println("Test");
//get the request dispatcher
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsps/galleries.jsp");
//forward to the jsp file
if (dispatcher != null)
dispatcher.forward(request, response);
else
System.err.println("Error: dispathcer is null");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
在 web.xml 我添加了下一行:
<servlet>
<display-name>Gallery Servlet</display-name>
<servlet-name>GalleryServlet</servlet-name>
<servlet-class>photoGallery.GalleriesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GalleryServlet</servlet-name>
<url-pattern>/galleries/*</url-pattern>
</servlet-mapping>
当我打开页面时
http://localhost:8080/PhotoGallery/galleries
然后在控制台中打印“测试”,但在浏览器中我看到“HTTP 状态 404 - 未找到”
我在哪里犯了错误?