aspose file tools*
The moose likes Struts and the fly likes forwarding to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "forwarding to jsp" Watch "forwarding to jsp" New topic
Author

forwarding to jsp

Brucaliffo John
Greenhorn

Joined: Jan 20, 2009
Posts: 29
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

Joined: Oct 08, 2006
Posts: 2458
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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3164
    
  47



This is a good way you can implement.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Brucaliffo John
Greenhorn

Joined: Jan 20, 2009
Posts: 29
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 10, 2002
Posts: 61413
    
  67

I think I'll keep it here as there may be Struts-specific ramifications.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Brucaliffo John
Greenhorn

Joined: Jan 20, 2009
Posts: 29
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

Joined: Sep 29, 2008
Posts: 12617



IIRC you cannot do a redirect once *any* data has been written to the response.
Brucaliffo John
Greenhorn

Joined: Jan 20, 2009
Posts: 29
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 20, 2009
Posts: 29
ok thank you.
response should be visible in jsp...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(Can't tell if that's a question ;)

Yes, "response" is available in JSP scriptlet code.
arjun kt
Greenhorn

Joined: Feb 14, 2009
Posts: 5
Ya response is available in JSP scriptlets.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

"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.


SCJP, SCWCD.
|Asking Good Questions|
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Look at following link for example of redirect and forward.
Response.sendRedirect example
Forward request to JSP

[Servlet tutorial] [Servlet 3.0 Cook Book]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forwarding to jsp