眼眸繁星
2019-12-19 18:14:40
在Javaservlet中生成HTML響應(yīng)如何在Javaservlet中生成HTML響應(yīng)?
2 回答

qq_遁去的一_1
TA貢獻1725條經(jīng)驗 獲得超8個贊
${}
@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條經(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
提交
取消