在Javaservlet中生成HTML响应如何在Javaservlet中生成HTML响应?
2 回答
凤凰求蛊
TA贡献1825条经验 获得超4个赞
${}
@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
我们的servlet wiki页面
servlet是如何工作的?实例化、会话、共享变量和多线程
Servlet中的doGet和doPost
在页面加载时从JSP文件中调用servlet
提交HTML表单时如何将数据从JSP传输到servlet
使用MVC和DAO模式在JSP页面中显示JDBCResultSet
如何使用servlet和Ajax?
servlet返回“HTTP状态404请求的资源(/servlet)不可用”
小唯快跑啊
TA贡献1863条经验 获得超2个赞
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
提交
取消