Question - Write a Hello World Program using Servlets.
Answer -
The code to write a Hello World PRogram using Servlets is as follows:
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- public class HelloWorld extends HttpServlet {
- private String message;
- public void init() throws ServletException {
- message = "Hello World";
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("
" + message + "
"); - }
- public void destroy() {
- }
- }