• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing variables

 
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello mates.
I have a question.I have a form on my index.jsp page and when it is submitted it calls a servlet that with .getParameter() gets 2 strings.I want when the servlet finishes what it is to do i want to call an other jsp page and pass 1 variable to it.How can i do it without using the url???

thnx in advance..
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you transitioning between the servlet and the JSP? Redirect? Forward?
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, forgot to right that down.I use redirect.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then your choice is to pass the data as a query paramter on the URL, or place it in the session to retrieve on the other side.

P.S. Please see my PM to you regarding your signature string.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aris
After completion all stuff in servlet whatever variable u want
to pass to another page put it in the request and then use
RequestDispatcher's forward method for forwarding to appropriate
jsp page .The forwarding page gets same request and response object.
[ May 30, 2006: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aris has already stated that he's using a redirect and not a forward. If he were to change the means of transfer to a forward, then there's actually no need to do anything, including creating a request-scoped vairable, since the original request parameters would still be available to the target resource.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your opinion what is the best way to pass variables.with foward or redirect/???
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrong question. The question should be: what makes the most sense in the context of the application: a redirect or a forward. Then pass the variables accordingly.

Tell us more about the processing that is going on in this scenario so we can advise whether a redirecto ro froward would be appropriate.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the thumb rule - User RequestDispatcher.forward() if possible. This will save a client trip

Now the question is, how to find out the "if possible" part?

To send the request to another resource(JSP, servlet or HTML) with in the SAME Web Application you should use RequestDispatcher.forward().

To send the request across web applications you don't have much choice but to use "send redirect".
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harpreet Hira:

To send the request to another resource(JSP, servlet or HTML) with in the SAME Web Application you should use RequestDispatcher.forward().



That is way too simplistic a rule, and not a very good one at that.

For example, it would break the PRG Pattern (search if you don't know what it is).

The "rule" should be: use a forward if the current request needs to be transferred to the new resource, use a redirect if not.

A forward is only available to resources in the same web app, but just because the resource is in the same app does not automatically indicate a forward.
[ May 30, 2006: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:


The "rule" should be: use a forward if the current request needs to be transferred to the new resource, use a redirect if not.



What is 'new resource' here??

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:

What is 'new resource' here??



The target of the forward or redirect; usually a servlet or JSP.
 
Harpreet Hira
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "rule" should be: use a forward if the current request needs to be transferred to the new resource, use a redirect if not.

use a forward if the current request needs to be transferred to the new resource - Agreed
use a redirect if not. - why?

Doing an HTTP redirect requires a round-trip to the client. If this is not required, and the only desire is to forward the request to another resource, then this can be much more efficiently accomplished with the RequestDispatcher.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harpreet Hira:
this can be much more efficiently accomplished with the RequestDispatcher.



Efficiency should take a back-seat to what makes the most sense for the application.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
i am working on an e-commerce shop.(bookstore) and trying to send from a login page for customer to find his customer_id and use it in a servlet for a checkout page..
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read this article in the JavaRanch Journal?

In particular, check out if the PRG pattern applies to your situation.
 
Harpreet Hira
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Efficiency should take a back-seat to what makes the most sense for the application.



I guess you missed highlighted part, " the only desire is to forward the request to another resource, then this can be much more efficiently accomplished with the RequestDispatcher"

Efficiency may take back seat sometimes. But I suggested the condition when this is more efficient.

If any one wants to implement PRG(or may be some other pattern) then forward might not fit his/her needs.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's excactly what I'm saying. If a forward is indicated, then use it. If a redirect is needed, then use it.

What I'm saying not to do is to use a forward, when a redirect is more appropriate, just because it's more "efficient".
reply
    Bookmark Topic Watch Topic
  • New Topic