File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the output of the code and why is it so????

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Craig Jackson
Ranch Hand
Posts: 405
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic