在 Java Servlet 中生成 HTML 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2370960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:22:57  来源:igfitidea点击:

Generate an HTML Response in a Java Servlet

htmlservlets

提问by ogechi nwogu

How do I generate an HTML response in a Java servlet?

如何在 Java servlet 中生成 HTML 响应?

回答by BalusC

You normally forward the request to a JSP for display. JSP is a view technology which provides a template to write plain vanilla HTML/CSS/JS in and provides ability to interact with backend Java code/variables with help of taglibs and EL. You can control the page flow with taglibs like JSTL. You can set any backend data as an attribute in any of the request, session or application scope and use EL (the ${}things) in JSP to access/display them.

您通常将请求转发到 JSP 进行显示。JSP 是一种视图技术,它提供了一个模板来编写普通的 HTML/CSS/JS,并提供在标签库和 EL 的帮助下与后端 Java 代码/变量交互的能力。您可以使用JSTL 等标签库控制页面流。您可以将任何后端数据设置为任何请求、会话或应用程序范围中的属性,并${}在 JSP 中使用 EL(事物)来访问/显示它们。

Kickoff example:

开场示例:

@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message = "Hello World";
        request.setAttribute("message", message); // This will be available as ${message}
        request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
    }

}

And /WEB-INF/hello.jsplook like:

而且/WEB-INF/hello.jsp样子:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 2370960</title>
    </head>
    <body>
         <p>Message: ${message}</p>
    </body>
</html>

When opening http://localhost:8080/contextpath/hellothis will show

当打开http://localhost:8080/contextpath/hello这将显示

Message: Hello World

in the browser.

在浏览器中。

This keeps the Java code free from HTML clutter and greatly improves maintainability. To learn and practice more with servlets, continue with below links.

这使 Java 代码不受 HTML 混乱的影响,并大大提高了可维护性。要学习和练习 servlet 的更多信息,请继续下面的链接。

Also browse the "Frequent" tab of all questions tagged [servlets]to find frequently asked questions.

还可以浏览所有标记为 [servlets] 的问题的“常见”选项卡以查找常见问题。

回答by codaddict

You need to have a doGet method as:

您需要有一个 doGet 方法:

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

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Hola</title>");
    out.println("</head>");
    out.println("<body bgcolor=\"white\">");
    out.println("</body>");
    out.println("</html>");
}

You can see thislink for a simple hello world servlet

你可以看到这个简单的 hello world servlet 链接

回答by Marco

Apart of directly writing HTML on the PrintWriter obtained from the response (which is the standard way of outputting HTML from a Servlet), you can also include an HTML fragment contained in an external file by using a RequestDispatcher:

除了直接在从响应中获取的 PrintWriter 上写入 HTML(这是从 Servlet 输出 HTML 的标准方式),您还可以使用 RequestDispatcher 包含包含在外部文件中的 HTML 片段:

public void doGet(HttpServletRequest request,
       HttpServletResponse response)
       throws IOException, ServletException {
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   out.println("HTML from an external file:");     
   request.getRequestDispatcher("/pathToFile/fragment.html")
          .include(request, response); 
   out.close();
}