Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forwarding to jsp

 
Brucaliffo John
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • 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.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4265
229
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is a good way you can implement.
 
Brucaliffo John
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
ok thank you.
response should be visible in jsp...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Can't tell if that's a question ;)

Yes, "response" is available in JSP scriptlet code.
 
arjun kt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya response is available in JSP scriptlets.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at following link for example of redirect and forward.
Response.sendRedirect example
Forward request to JSP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic