从servlet返回HTML / XHTML文件 [英] Returning an HTML/XHTML file from servlet

查看:92
本文介绍了从servlet返回HTML / XHTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过servlets示例,它们是这样的:

I've seen servlets examples, they're something like this:

 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页面吗?我的意思是,像这样:

My question is, instead of the code, can I return an HTML page? I mean, something like this:

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter out = response.getWriter();

            SHOW(FILE.HTML);

        }

谢谢! ;)

推荐答案

有几种不同的方法可以做到这一点:

There are a few different ways you could do this:


  1. 将servlet转发到HTML文件所在的路径。类似于:

  1. Forward the servlet to the path where the HTML file is located. Something like:

RequestDispatcher rd = request.getRequestDispatcher(something.html);
rd.forward(请求,响应);

将重定向发送到HTML所在的URL。类似于:

Send a redirect to the URL where the HTML is located. Something like:

response.sendRedirect(something.html);

读入HTML文件的内容,然后将HTML文件的内容写出到servlet的PrintWriter。

Read in the contents of the HTML file and then write out the contents of the HTML file to the servlet's PrintWriter.

这篇关于从servlet返回HTML / XHTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆