Advanced Java Programming - Old Questions
11. How do you handle HTTP request (GET) using servlet?
Handling HTTP GET
requests involves overriding the doGet
method.
The following example shows the use of HTTP GET method.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
res.setContentType(“text/html”);
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println(“<title>HW</title>”);
pw.println("</head>");
pw.println("<body>");
pw.println("<h2>Hello World!</h2>");
pw.println("</body>");
pw.println("</html>");
pw.close();
}
}