Junior — Middle
How to implement a server application that serves an HTML page to display information?
sobes.tech AI
Answer from AI
To implement a server application in Java that serves an HTML page to the client, you can use servlets or frameworks such as Spring Boot.
Example using a servlet:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("<html><body><h1>Hello, client!</h1></body></html>");
}
}
This servlet returns a simple HTML page on a GET request to /hello. In more complex applications, template engines (Thymeleaf, JSP) and frameworks are often used to simplify working with HTML.