• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

using getReader() of ServletRequest

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Verity smith
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for clearing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic