在 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
Generate an HTML Response in a Java Servlet
提问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.jsp
look 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 的更多信息,请继续下面的链接。
- Our Servlets wiki page
- How do servlets work? Instantiation, sessions, shared variables and multithreading
- doGet and doPost in Servlets
- Calling a servlet from JSP file on page load
- How to transfer data from JSP to servlet when submitting HTML form
- Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
- How to use Servlets and Ajax?
- Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
- 我们的 Servlets wiki 页面
- servlet 是如何工作的?实例化、会话、共享变量和多线程
- Servlet 中的 doGet 和 doPost
- 在页面加载时从 JSP 文件调用 servlet
- 提交HTML表单时如何将数据从JSP传输到servlet
- 使用 MVC 和 DAO 模式在 JSP 页面中以 HTML 格式显示 JDBC ResultSet
- 如何使用 Servlet 和 Ajax?
- Servlet 返回“HTTP 状态 404 请求的资源 (/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();
}