Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem while trying to retreive values doPost

 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks ,
Im trying to write a web app. I face an issue while trying ot retreive a value that I've set in the request scope of the variable in my jsp as below


I set the value for the variable in the doGet method of my servlet.


However, when i try to access this value in the doPost mehod of my servlet after the page submits , it always shows up as null

Any clue as to where I could be going wrong?
Thanks a lot.
 
Ravi Sree
Ranch Hand
Posts: 64
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First of all,
The methods doGet and doPost should be like:

&

Also,
when is this 'issue' that you see?
where are you setting this value in your jsp for posting to your servlet?

Regards,
Sree
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi!
Thanks for replying ! I tried changing the return type from protected to public , yet no use.
The issue that I face , as explained before occurs when I try to store a value that I obtain from my do Get method in the jsp using c:set which works well .But when i try to get back the value in the do Post method using request .getParameter it throws a null. Hope im being clear
Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the response is sent to the browser, the request is complete. So when the page is subsequently submitted, it creates a new request. Any scoped variable created in the first will, of course, not be available in the second. Why would you think it would be?

And what is the purpose of the following statement?
<c:set value="${consumer_id}" var="consumer_id" scope="request"/>

This does nothing. Why is it in your page?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Once the response is sent to the browser, the request is complete. So when the page is subsequently submitted, it creates a new request. Any scoped variable created in the first will, of course, not be available in the second. Why would you think it would be?

And what is the purpose of the following statement?
<c:set value="${consumer_id}" var="consumer_id" scope="request"/>

This does nothing. Why is it in your page?


Hi Bear,
Thanks for replying. I dont know whether my approach is right here .But here's what Im trying to do ..
Consider Im using 2 jsp's A and B with servlets A1 and B1 respectively
The value of "consumer_id" is actually being fetched from jsp A which points to servlet B through a link.
I get the value of consumer id in servlet B's doGet method using request.getParameter .
Now I want this value of "consumer_id" to be available for processing in the doPost method of servlet B, when i submit the jsp B and hence i try and set it in a variable using c:set in jsp B, but that approach doesn't seem to be working..
Any clue on what I could be doing wrong here?
Thanks.


through page which redirects to the
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using your <c:set> is not going to do anything for you. The way you coded it, it's just setting a scoped variable to its own value. Of what use is that? And as the scoped variable doesn't exist in the first place in the request, it's certainly not going to do anything useful at all.

Think! Stop, and think!

If you set a scoped variable into the request, how is that going to be helpful in obtaining the value in a completely different request?

The answer is, of course, that it's not.

So where might you store the data such that it will be available in a subsequent request?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session?? Unless im not mistaken again ...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
 
Ravi Sree
Ranch Hand
Posts: 64
Java MySQL Database PHP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are sure you still want to use the variable in request scope,

&
and get this property where you want next,
Also before submitting make sure you are using the correct form method.

Regards,
Sree
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65110
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Messy. The session is much cleaner.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic