• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • 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();
}
}
 
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • 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
 
Ranch Hand
Posts: 405
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!