• 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

forwarding to jsp

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have to forward to a jsp page from a java code in another jsp, how can I do that?
Thank you
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You normally use RequestDispatcher#forward() for this.

But you say, you have raw Java code in a JSP file? Immediately move it out into a Java class like a servlet before it's too late. Scriptlets indicate in any way a design flaw and are thus a bad practice.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is a good way you can implement.
 
Brucaliffo John
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know it's a bad practice, but I need to do in this way for now...

how can I get RequestDispatcher and ActionMapping in my java code in my jsp?

Thank you very much!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ActionMapping? Isn't it part of Struts? If so, then the trouble is bigger. Struts is already a MVC framework at its own. You should be doing this logic in a Struts action class or whatever it provides you to control and handle requests programmatically.

The excuse "doing in this way for now" makes no utter sense. You either don't understand Struts nor the MVC concepts at all or you have another problem which you didn't elaborate about at all and for which you think that this is the solution. I.e. you're asking how to achieve a workaround instead of asking how to solve an actual problem.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good practice or not, this thread should be moved to the servlet/JSP forum if it's not directly related to Struts.

(There are, btw, valid usecases for doing this in JSP, like if you don't have access to the action code.)
 
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
I think I'll keep it here as there may be Struts-specific ramifications.
 
Brucaliffo John
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway how can I redirect to a jsp page from a java code in a jsp page?
(without struts)

Thank you
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


IIRC you cannot do a redirect once *any* data has been written to the response.
 
Brucaliffo John
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what I need to redirect to a jsp?
wich lines of code, can you post an example, please.
Thank you
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the path of the JSP (relative to the app) inside the sendRedirect() call.

Searching the web for "jsp +response.sendredirect" will bring up many examples.
 
Brucaliffo John
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thank you.
response should be visible in jsp...
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Can't tell if that's a question ;)

Yes, "response" is available in JSP scriptlet code.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya response is available in JSP scriptlets.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"arjun kt " welcome to Javaranch
please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at following link for example of redirect and forward.
Response.sendRedirect example
Forward request to JSP
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic