Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain why error????

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.javaranch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PostServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>\n <body>\n");
out.println(" <h2>\n Hello World\n </h2>\n");
out.println(" </body>\n</html>\n");
out.flush();
out.close();
}
}
What is the result of compiling the above Servlet and accessing it by typing:
"http://www.javaranch.com/servlet/com.javaranch.PostServlet"
into the address field of a browser
/********
The Servlet does not implement a doGet method so, an error is returned.
**********/
 
tony lee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
doPost is used to retrieve data from HTTP form. when typing a URL on browser, we're supposed to request data from server, where a doGet method is used.
however, we can override doPost by calling doGet as the following:
-----
package com.javaranch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PostServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>\n <body>\n");
out.println(" <h2>\n Hello World\n </h2>\n");
out.println(" </body>\n</html>\n");
out.flush();
out.close();
}
}
----------
it works
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic