This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes what should be output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "what should be output" Watch "what should be output" New topic
Author

what should be output

jawwad ahmed
Ranch Hand

Joined: Dec 01, 2001
Posts: 179
Well i am reading data manually of Get Method.
It is printing just:
<br>reader.readLine().
but the o/p is comming in the url and it is just printing:
<br>reader.readLine().
while i think it should also print the second line i.e:
reader.readLine():WHAT EVER I PASS IN POSTSERVLET.
or print with null or should throw exception.
I tried in tomcat4 and tomcat3 in both but out put is same.
What should be the o/p in your opinion.
Here is the code.

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class PostServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
out.println("<form action=LoginPage method=Get>");
out.println("<input type=text name=name>");
out.println("<input type=text name=rollno>");
out.println("<INPUT TYPE=submit name=confirm>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class LoginPage extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
{
BufferedReader reader=req.getReader();
System.out.println("<br>reader.readLine()");//1
System.out.println("<br>reader.readLine()"+reader.readLine());//2
}

}
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Beats me... .
Although I don't understand the need to do a getReader() on the Request object.
I tried some combinations like adding a doPost() method etc, but doesn't answer your qstn.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Data passed can be seperated by emty lines. That is probably the case here. Try changing

to

This will show you everything sent by the browser in the body.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
[b]
String lineIn = null while((lineIn = reader.readLine()) != null) { System.out.println("<BR>reader.readLine() : " + lineIn); }[/v]
Nope. Din't do much good either.
Attempted in IE and NS using Tomcat 4.0.1.
regds.
- satya
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
knock....knock....knock.....
ersin....Guy....Axel....Ashik....faiza..anybody!!!
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Here it is dark outside and friday.
GMT +1
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi,
I got a Timeout-Exception in the log file. Maybe its a question for a jakarta newsgroup, but I am really to tired. Was a hard week.


You should have the same:
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
HttpServletResponse getReader will read the information passed to the servlet in the Post InputStream. The information being past here is a GET and Not a post. Change the form to do a post and change the LoginPage to implemtent the doPost method and see your results. (ps, then try my other changes )
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
HttpServletResponse getReader...
HttpServletRequest getReader, that is right?
Will try.
- satya
Sita Kotamraju
Ranch Hand

Joined: Nov 30, 2001
Posts: 60
getReader() returns a BufferedReader which retrives the body of the request as character data.
The difference between GET and POST requests,as we all know, is that in a "GET" form data is encoded into a URL, whereas in a "POST" the form data appears in the message body.
Changing the method to POST should display the expected output. To see the same in GET use request.getQueryString()
I have tested this in WebLogic6.1 and it works fine, after changing to POST.
[ January 25, 2002: Message edited by: Sita Kotamraju ]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
I agree it works with POST. I verified with Tomcat.
So does this confirm that in the initial code with the GET method, nothing gets passed in the message body and so there was nothing to read?
Thanks.
- satya
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
It demonstrats some of what you need for Objective:
1.2 For each of the HTTP methods, GET, POST, and HEAD, identify triggers that might cause a browser to use the method, and identify benefits or functionality of the method.
I think Ken's notes had a good discussion of the rest
ersin eser
Ranch Hand

Joined: Feb 22, 2001
Posts: 1072
SOB
I been telling to myself BufferedReader.readLine() reads the body, but never thought about checking the action="Get" ! DuH! I assumed that it was POST
Sita Kotamraju
Ranch Hand

Joined: Nov 30, 2001
Posts: 60
Originally posted by Madhav Lakkapragada:
I agree it works with POST. I verified with Tomcat.
So does this confirm that in the initial code with the GET method, nothing gets passed in the message body and so there was nothing to read?
Thanks.
- satya

I would like to know that too...however, getContentLength() returns a -1(unknown length) with a GET.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
new explanation why it don't work with doGet().
Api states retrieves the body of the request as character data using a BufferedReader.
I strongly believe that a http-get request has NO body, whereas a post request has. Can't find it, but I can sweare that I read something about that a http-post has a special body section, where you find all the paramName-paramValue pairs.
Please comment on this.
Axel
jawwad ahmed
Ranch Hand

Joined: Dec 01, 2001
Posts: 179
Well i know that this code work with post request.
But the specification does't told about retrieving data from the request object.
As in the above code the data is retrieved on the next page but only appear as query string.it does not print the data on the screen.I think it is bug in tomcat or it is the case of rfc about the http methods.
 
jQuery in Action, 2nd edition
 
subject: what should be output
 
Similar Threads
Progress bar on IE6
if (req.getMethod().equals("HEAD")) return
Waiting page while loading
Requested Dispatcher
Help me please