• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help me to find the answer

 
shanmu sha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm novice to servlet. Could anyone please help me to find the correct answer to the below code. And why it is correct?.

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



a) An error page is returned from the Server.


b) The browser displays "Hello World"


c) The server will not find the Servlet due to an incorrect URL.


d) The code fails to compile.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12263
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanmu,

Your display name does not meet our naming policy. Basically, it requires a real sounding first AND last name. Please go here and update your profile.

Thanks!
 
shanmu sha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred,

sorry , I did update.

Thanks.
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to work with the same example and the server returns a message
"HTTP method GET is not supported by this URL", since default is GET and we do not have a service doGet in our servlet. When I change the service to doGet everything went good and HelloWorld was displayed. So choose your answer accordingly. Hope this helps

Thanks
Chandu
 
shanmu sha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Chandu, by the way how can we come to know whether this URL will support GET or POST by seeing the URL "http://www.javaranch.com/servlet/com.javaranch.PostServlet"

Thanks
shanmu
 
Francisco Moroyoqui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanmu, just looking at the URL won't tell you whether it supports GET and/or POST.

You tell the browser whether you are GETting a resource or POSTing to it by using an specific HTML element. For example, if you use an anchor element
then you are using GET.

If you just type the URL on the address bar and then press enter, that's also a GET.

To use POST, you use a FORM element with its METHOD attribute set to POST.


Hope this helps.
 
shanmu sha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch, Francisco.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic