如何从java servlet返回一个html文档? [英] How to return an html document from java servlet?

查看:482
本文介绍了如何从java servlet返回一个html文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可以返回一个字符串:

This works to return a string:

import javax.servlet.http.*;
@SuppressWarnings("serial")
public class MonkeyServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

        resp.setContentType("text/plain");
        resp.getWriter().println("got this far");

    }

}

但我可以不能让它返回一个HTML文档。这不起作用:

But I can't get it to return an html document. This doesn't work:

import javax.servlet.http.*;
@SuppressWarnings("serial")
public class BlotServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

        resp.setContentType("text/html");
        resp.getWriter().println("html/mypage.html");

    }

}

很抱歉noob!

编辑:

我已经在单独的文档中使用了html。所以我需要返回文档,或以某种方式读取/解析它,所以我不只是重新输入所有的HTML ...

I already have the html in separate documents. So I need to either return the document, or read/parse it somehow, so I'm not just retyping all the html...

编辑:

我在我的web.xml中有这个

I have this in my web.xml

<servlet> 
    <servlet-name>Monkey</servlet-name> 
    <servlet-class>com.self.edu.MonkeyServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Monkey</servlet-name> 
    <url-pattern>/monkey</url-pattern> 
</servlet-mapping>

还有什么我可以放在那里所以它只返回一个文件,比如...... / p>

Is there something else I can put in there so it just returns a file, like...

<servlet-mapping> 
    <servlet-name>Monkey</servlet-name> 
    <file-to-return>blot.html</file-to-return> 
</servlet-mapping>


推荐答案

print 从Servlet本身输出HTML (不建议使用)

PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>My HTML Body</h1>");
out.println("</body></html>");

发送 到现有资源(servlet,jsp等)(称为转发到视图)(首选)

RequestDispatcher view = request.getRequestDispatcher("html/mypage.html");
view.forward(request, response);

您需要将当前HTTP请求转发到的现有资源不需要特殊任何方式,即它像任何其他Servlet或JSP一样编写;容器无缝地处理转发部分。

The existing resource that you need your current HTTP request to get forwarded to does not need to be special in any way i.e. it's written just like any other Servlet or JSP; the container handles the forwarding part seamlessly.

只需确保提供资源的正确路径。例如,对于servlet, RequestDispatcher 将需要正确的URL模式(在web.xml中指定)

Just make sure you provide the correct path to the resource. For example, for a servlet the RequestDispatcher would need the correct URL pattern (as specified in your web.xml)

RequestDispatcher view = request.getRequestDispatcher("/url/pattern/of/servlet");

另外,请注意a RequestDispatcher 可以从 ServletRequest ServletContext 中检索,区别在于前者可以采用 相对路径

Also, note that the a RequestDispatcher can be retrieved from both ServletRequest and ServletContext with the difference that the former can take a relative path as well.

参考:

http://docs.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher。 html

public class BlotServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
        // we do not set content type, headers, cookies etc.
        // resp.setContentType("text/html"); // while redirecting as
        // it would most likely result in an IllegalStateException

        // "/" is relative to the context root (your web-app name)
        RequestDispatcher view = req.getRequestDispatcher("/path/to/file.html");
        // don't add your web-app name to the path

        view.forward(req, resp);    
    }

}

这篇关于如何从java servlet返回一个html文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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