what is the output of the code and why is it so????
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
What will be the output of the following Servlet. For example, it can be invoked through a Web Browser with the URL http://<server-name>:8080/servlet/HelloServlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>The Standard Hello World Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<b>doGet Hello World!!</b>"); out.println("</body>"); out.println("</html>"); out.close(); }
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>The Standard Hello World Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<b>doPost Hello World!!</b>"); out.println("</body>"); out.println("</html>"); out.close(); } }
Um, nothing because you overwrote the service method, which overrides the Servlets ability to "push" the request to the correct Post or Get method. You would need to call one of the doPost or doGet methods to get it to display anything, since the calls that are automatically done for you are gone by overriding the service method. Mark
Well, let me start off by saying you shouldn't override the service method unless you have a very good reason. As far as your question is concerned, the above code will not generate any output But, you could say something like this
in the HelloServlet.service() method. This would generate an output from the doGet() by default.