在java servlet中调用html页面

时间:2015-11-29 16:18:13

标签: java servlets

我有这个自动生成的java servlet和tomcat 6正在运行。我想在java servlet中调用我的HTML文件,这样我就可以在localhost上看到我的页面了。我发现我需要使用requestdispatcher,但我似乎无法使用以下代码使用它:

package com.captiveportal;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Demo
 */
public class Demo extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Demo() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.getWriter().append("Served at: ").append(request.getContextPath());
        RequestDispatcher rd = request.getRequestDispatcher("captiveportal.html");
        rd.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        RequestDispatcher rd = request.getRequestDispatcher("captiveportal.html");
        rd.forward(request, response);
    }

}

1 个答案:

答案 0 :(得分:0)

你给出了正确的html页面路径吗?尝试使用以下路径 - http :: / host:port / rootApp /../ yourHtml