我见过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);
}
谢谢! ;)
答案 0 :(得分:9)
有几种不同的方法可以做到这一点:
将servlet转发到HTML文件所在的路径。类似的东西:
RequestDispatcher rd = request.getRequestDispatcher("something.html");
rd.forward(request, response);
将重定向发送到HTML所在的网址。类似的东西:
response.sendRedirect("something.html");
读入HTML文件的内容,然后将HTML文件的内容写出到servlet的PrintWriter。