在Javaservlet中生成HTML响应如何在Javaservlet中生成HTML响应?
2 回答
慕码人2483693
TA贡献1860条经验 获得超9个赞
${}
@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); }}
/WEB-INF/hello.jsp
<!DOCTYPE html><html lang="en"> <head> <title>SO question 2370960</title> </head> <body> <p>Message: ${message}</p> </body></html>
Message: Hello World
弑天下
TA贡献1818条经验 获得超8个赞
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>");}
添加回答
举报
0/150
提交
取消