Java 通过PrintWriter返回html页面
maven依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.1</version>
</dependency>
@GetMapping("/html")
public void getHtml(HttpServletResponse response) throws IOException {
String html = "<html>\n" +
"<head>\n" +
"<script type=\"text/javascript\">\n" +
" window.alert(5 + 6);\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
" <div>\n" +
" <p>1111<br></p>\n" +
" </div>\n" +
"</body>\n" +
"</html>\n" +
"\n" +
"<style type=\"text/css\">\n" +
" p{\n" +
" color: red;\n" +
" }\n" +
"</style>";
Document doc = Jsoup.parse(html);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println(doc.outerHtml());
}