在Javaservlet中生成HTML响应如何在Javaservlet中生成HTML响应?
2 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
${}
@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贡献1998条经验 获得超6个赞
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
提交
取消