aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes using getReader() of ServletRequest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "using getReader() of ServletRequest" Watch "using getReader() of ServletRequest" New topic
Author

using getReader() of ServletRequest

Verity smith
Greenhorn

Joined: May 27, 2003
Posts: 18
Hi All,

//PostTest.html
<html>
<body>
<form method="POST" action="/servlet/PostTest">
val1=<input type="text" name="value1"><br>
val2=<input type="text" name="value2">
<input type="submit" name="AddButton">
</form>
</body>
</html>

//PostTest.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostTest extends HttpServlet
{
int sum = 0;

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/plain");
BufferedReader inReader = req.getReader();
PrintWriter out = res.getWriter();

String val1Str = inReader.readLine();
String val2Str = inReader.readLine();

out.println("value obtained manually = "+val1Str);
out.println("value obtained again manually is = "+val2Str);

out.println("val1 obtained again is = "+req.getParameter("value1"));
out.println("val2 obtained again is = "+req.getParameter("value2"));

}
}


The form PostTest.html is accessed in a browser, and the values entered are value1 = 750 and value2 = 1000. What will be displayed in the browser on clicking the 'submit' button?

Can anyone please explain this question to me.
Thanks in advance!
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
If the parameter information sent to a servlet as encoded POST data has been read manually using the Reader or InputStream obtained using the getReader() or getInputStream() method of the ServletRequest, then, it cannot be read again.
This will be displayed!
value obtained manually = value1=750&value2=1000&AddButton=Submit+Query
value obtained again manually is = null
val1 obtained again is = null
val2 obtained again is = null
Now consider the following code:
public class PostTest extends HttpServlet
{

public void doPost(HttpServletRequest req, HttpServletResponse res) throws SE, IOE
{
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("val1 obtained is = "+req.getParameter("value1"));
out.println("val2 obtained is = "+req.getParameter("value2"));
out.println("val1 obtained again is = "+req.getParameter("value1"));
out.println("val2 obtained again is = "+req.getParameter("value2"));
}
}

Now if the form (in your example) PostTest.html is accessed in a browser, and the values entered are value1 = 750 and value2 = 1000 and you submit the form.

This will be displayed:
val1 obtained is = 750
val2 obtained is = 1000
val1 obtained again is = 750
val2 obtained again is = 1000
Here the "String HttpServletRequest.getParameter(String )" method has been implemented in such a way that no matter how many times it is called for a particular parameter, it always returns the parameter value.
[ June 10, 2003: Message edited by: Amer Khan ]

<i>Dare to dream - everything that exists today,was once a figment of someone's imagination, nobody says tomorrow can't be a figment of your today.</i>
Verity smith
Greenhorn

Joined: May 27, 2003
Posts: 18
Thanks very much for clearing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using getReader() of ServletRequest