拉風(fēng)的咖菲貓
2019-07-27 09:15:01
在Javaservlet中生成HTML響應(yīng)如何在Javaservlet中生成HTML響應(yīng)?
2 回答

紅顏莎娜
TA貢獻1842條經(jīng)驗 獲得超13個贊
${}
@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貢獻1779條經(jīng)驗 獲得超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
提交
取消