我见过servlets的例子,它们是这样的:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
...
}
我的问题是,我能返回一个html页面而不是代码吗?我是说,像这样的事情:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
SHOW(FILE.HTML);
}
谢谢!!!;)
1条答案
按热度按时间axkjgtzd1#
有几种不同的方法可以做到这一点:
将servlet转发到html文件所在的路径。比如:
RequestDispatcher rd = request.getRequestDispatcher("something.html"); rd.forward(request, response);
向html所在的url发送重定向。比如:response.sendRedirect("something.html");
读入html文件的内容,然后将html文件的内容写入servlet的printwriter。