File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes what is the output of the code and why is it so???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "what is the output of the code and why is it so????" Watch "what is the output of the code and why is it so????" New topic
Author

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

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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.
 
Don't get me started about those stupid light bulbs.
 
subject: what is the output of the code and why is it so????
 
Similar Threads
compiling servlets
if (req.getMethod().equals("HEAD")) return
Progress bar on IE6
HTTP method GET is not supported by this URL
Problem deploying-accesing a single servlet webapp in Tomcat 5