• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to handle this seneario in requestDispatcher?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have a JSP page suppose its abc.jsp

at the time its URL is

localhost:8080/MyWebApp/abc.jsp


there is a form inside it which is mapped to a request to "/register" servlet url.Its URL in server is

localhost:8080/MyWebApp/register


when I get data in "/register" servlet after processing then I used requestDispatcher to forward it again to "/abc.jsp"

and URL at the top is


Now abc.jsp is displaying on the URL


Problem is when I try to reload abc.jsp actuallly I Am calling URL "/register" which sends request to a get method of "/register" servlet.

My Intention is I want to refresh abc.jsp but by loading I am calling to get method of servlet

How can I handle this seneraion in my Web App?

Thanks
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you think this is a problem. What you are describing is the correct way to use jsp. All calls to a jsp should go through a controlling servlet. You should never access a jsp page directly.

So what exactly is the problem that you are trying to solve?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that I have already pointed you to this article which explains how to avoid problems like this.

You should not be able to address JSPs directly, place them under WEB-INF.
Each JSP should have a page controller that prepares the page for display and forwards to it.
Your forms should submit to a task controller that performs a task. Task controllers never forward to JSPs, but redirect to the next page's page controller.

Read the article again and ask questions on anything you do not understand.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
So what exactly is the problem that you are trying to solve?

I believe the problem is that he is forwarding to a JSP from a task controller, which leaves the task controller "on the browser" subject to refresh issues. These issues disappear when page and task controllers are used properly. (the so-called PRG pattern).
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I believe the problem is that he is forwarding to a JSP from a task controller, which leaves the task controller "on the browser" subject to refresh issues. These issues disappear when page and task controllers are used properly. (the so-called PRG pattern).


this is exactly my problem task controller URL is on browser.You got my point.But I was not able to get my answar the link you have suggested me above

some more help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The proper flow is outlined in this diagram from the article:

Be sure to read carefully about the PRG pattern. Using it, what is left "on the browser" is the page controller, which performs no tasks. It just fetches any data that the page needs and so is safe to perform again and again and again, as all it does is to display the page.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear

Thanks for this wonderful diagram
cleared a lot of things in my mind

But How can I map a jsp page in controller?

a short code snipshot
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the article, it explains the diagram clearly.

I'm not sure what you mean by "map a JSP in a controller". You forward to a JSP from its page controller. You submit a form (or link) to a task controller in the JSP. Task controllers redirect to page controllers.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear

I have read these artciles
http://www.coderanch.com/t/637985/Servlets/java/change-url-jsp-passing-doPost
http://www.coderanch.com/t/557156/JSP/java/Developing-Page-Controller
http://www.javaranch.com/journal/200603/frontman.html


But I am not able to transfrom this into code.

Let me explain there are two pages one is abc.jsp and other is TaskController(/login)

1)abc.jsp call to /login url is know localhost:8080/MyWebApp/login


2)in /login processing is done
now
request.setAttribute("somAttribute","Some Value");
request.getRequestDispatcher("/abc.jsp).forward(request,response);

and know URL on the top is still
http://localhost:8080/MyWebApp/login

How to change this URL to
http://localhost:8080/MyWebApp/abc.jsp


?
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I call your attention to Bear's earlier post:

Your forms should submit to a task controller that performs a task. Task controllers never forward to JSPs, but redirect to the next page's page controller.


You should have picked up by now:

Forward == internal to server. Doesn't change the URL in the browser.
Redirect == changes the URL at the browser. Makes a new request.

So if you want the url in the browser to change from /login/ then need to redirect rather than forward.


The process as described by Bear should probably be along the lines of
user submits to /login (task)
--> redirect to /homePage (pageController) Url changes to /homePage
--> forward to /abc.jsp Url remains the same as previously set.

That will both change the url from the task (login) to a display (homePage) and effectively hide the fact you are using JSP.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you mean to say If I have 50 JSP pages then I need to make 50 PageControllers?
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a word: yes.
Following that architectural pattern at any rate.

If you are looking at an application of that size though, using a framework such as Stripes can do much of the heavy lifting for you.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got my answars

Thanks to both of you
@Stefan
@Bear
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!