0
我需要在我的servlet中包含.jsp文件。不能在servlet中包含jsp
我已經寫了簡單的JSP文件,我已經把DIR WEB-INF/JSP的/:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title> Galleries </title>
</head>
<body>
Test
</body>
</html>
和servlet:
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 - 未找到」
在哪裏我已經錯了嗎?
我嘗試了不同的方法,如「WEB-INF/jsps/galleries.jsp」,「jsps/galleries.jsp」和「 /jsps/galleries.jsp「,但它沒有幫助。我重新啓動了計算機,eclipse,tomcat的結果是一樣的 – kobra
嘗試將jsps放在與WEB-INF相同的級別,然後嘗試直接轉到JSP。如果可行,那麼更新你的getRequestDispatcher()方法,你應該很好。 – JustinKSU
你是什麼意思直接去jsp? – kobra