Html 通过servlet访问webapp文件夹中文件的正确路径
我的webapp文件夹中有一个“index.html”文件。现在我想通过我的servlet重定向到这个文件,但它总是给出异常,因为我不知道该放什么路径。我的代码:Html 通过servlet访问webapp文件夹中文件的正确路径,html,servlets,Html,Servlets,我的webapp文件夹中有一个“index.html”文件。现在我想通过我的servlet重定向到这个文件,但它总是给出异常,因为我不知道该放什么路径。我的代码: public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { // Must set the content type first
public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// Must set the content type first
RequestDispatcher view = request.getRequestDispatcher("webapp/index.html");
try {
view.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我使用了“index.html”、“/index.html”、“/index.html”,
“/webapp/index.html”、“/webapp/index.html”
我不知道如何访问这个文件。请提供帮助。您的代码有两个问题:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("index.html");
try {
view.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
doService()
方法。相反,实现一个包含doService()
方法中的代码的doGet()
方法service()
方法并添加包含代码的doGet()
方法:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("index.html");
try {
view.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}