• 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

Request dispatcher doesn't redirect to the jsp page

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

I have a jsp form. When 'send' button is pressed it should go to the Servlet. Servlet should do his magic and dispatch and forward the list attributes to the next jsp page. But when i'm pressing the send button, I'm only redirected to the Servlet (in the address bar it's the address of server, not the jsp page). I don't want to use response.sendRedirect(), is there a way to do it with RequestDispatcher ? I want to be redirected from the jsp page with the form to the other jsp page, but the servlet should also perform his magic somewhere between the form jsp page and the other jsp page.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, the RequestDispatcher should be able to forward the request from the Servlet to the JSP, provided the response hasn't been committed yet.
Perhaps you could show us some 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
Is your only issue that the address bar isn't changing? It won't! And it shoudn't. A forward is a server-side action of which the browser is not, and doesn't need to be, aware of.

Does the JSP display?
 
Ranch Hand
Posts: 65
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Is your only issue that the address bar isn't changing? It won't! And it shoudn't. A forward is a server-side action of which the browser is not, and doesn't need to be, aware of.

Does the JSP display?



Yes it's the only issue. I will have a problem, if I will need to include that jsp in other JSP- it won't display correctly, because the correct version is the version which address lead to the servlet. Maybe there is a way to include the servlet (of course which forwards response and request to the jsp) ?

What if I will need to display data from database, forward it to the JSP page, and include that JSP file in other place.

Let's say that I have an jsp page which is build using the <jsp:include>. In one of the includes I have a jsp page which has a <c:forEach> loop with the requested attribute. I also have a servlet which connects to the database, saves the result in the request attribute and forwards it to the jsp (some kind of mvc). On the main jsp page (which is build from the jsp:include) it won't display correctly, because I include the jsp page, not the servlet right ? I will see nothing.

Maybe I should make something like this:
Servlet connects to the db and saves the data to some variable. The new POJO class gets that data and saves it to the variable with getter/setter method. In the jsp page I import that POJO, or I use it as a bean and I get that variable by EL or <usBean>?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marek Krokosinski wrote:Maybe I should make something like this:
Servlet connects to the db and saves the data to some variable. The new POJO class gets that data and saves it to the variable with getter/setter method. In the jsp page I import that POJO, or I use it as a bean and I get that variable by EL or <usBean>?



Sorry, I couldn't understand any of what you wrote before this. But this is the normal thing to do: a servlet receives the request and processes it by assembling the data required for the response. It puts that data into a request attribute, or several request attributes if necessary, and then forwards to a JSP. The job of the JSP is to produce the HTML for the response, inserting data from those request attributes as required.

You seem to be concerned with the page from which the request was sent. That page is in the past, you can't get anything from it (or whatever you were thinking). The JSP which generates the HTML must generate all of the HTML. Makes no difference if earlier requests generated similar HTML.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Marek Krokosinski wrote:Maybe I should make something like this:
Servlet connects to the db and saves the data to some variable. The new POJO class gets that data and saves it to the variable with getter/setter method. In the jsp page I import that POJO, or I use it as a bean and I get that variable by EL or <usBean>?



Sorry, I couldn't understand any of what you wrote before this. But this is the normal thing to do: a servlet receives the request and processes it by assembling the data required for the response. It puts that data into a request attribute, or several request attributes if necessary, and then forwards to a JSP. The job of the JSP is to produce the HTML for the response, inserting data from those request attributes as required.

You seem to be concerned with the page from which the request was sent. That page is in the past, you can't get anything from it (or whatever you were thinking). The JSP which generates the HTML must generate all of the HTML. Makes no difference if earlier requests generated similar HTML.



No, no. I don't want anything from the page which the request was sent. I need the JSP page, with the data from database. The data which servlet should forward to the JSP page with the request. I want to enter that page by typing in the address bar www.someaddress.com/Page.jsp. And when I enter on that page I want to see the data from the database which servlet has forwarded. Currently if I will have on my JSP page an <h1> tag and the <c:forEach> loop, I see only the <h1> tag, but when I will enter on the Servlet address (www.someaddress/SomeServlet.do) I see the <h1> tag and the result of the <c:forEach> loop. And my problem starts when I need to include the Page.jsp in another JSP. If I do that, I will include only <h1> tag, without <c:forEach> loop.

 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marek Krokosinski wrote:I need the JSP page, with the data from database. The data which servlet should forward to the JSP page with the request. I want to enter that page by typing in the address bar www.someaddress.com/Page.jsp. And when I enter on that page I want to see the data from the database which servlet has forwarded.



That doesn't make any sense. If you send a request from the browser directly to the JSP, then the servlet hasn't forwarded anything to the JSP. You can't even be certain that the servlet has ever run, so having the servlet put data into the session for the JSP to use later doesn't help.

I also couldn't make much sense of the rest of what you wrote. That's because the <c:forEach> loop which you mentioned has no context. If it's using data which was in request scope, then you're going to have to make sure that something earlier in the request actually put that data into the scope. If it's the servlet which is supposed to be doing that, then you have to make sure that the servlet ran earlier in the same request. And of course it's impossible for the JSP to make sure that a servlet ran earlier in the same request -- code can't affect what happened in the past.

But then you didn't say that the <c:forEach> loop was trying to use data from the request scope. You didn't say anything at all about it, in fact.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Marek Krokosinski wrote:I need the JSP page, with the data from database. The data which servlet should forward to the JSP page with the request. I want to enter that page by typing in the address bar www.someaddress.com/Page.jsp. And when I enter on that page I want to see the data from the database which servlet has forwarded.



That doesn't make any sense. If you send a request from the browser directly to the JSP, then the servlet hasn't forwarded anything to the JSP. You can't even be certain that the servlet has ever run, so having the servlet put data into the session for the JSP to use later doesn't help.


Yes, and that's the reason why I got the empty JSP page. I know that.

Paul Clapham wrote:

I also couldn't make much sense of the rest of what you wrote. That's because the <c:forEach> loop which you mentioned has no context. If it's using data which was in request scope, then you're going to have to make sure that something earlier in the request actually put that data into the scope. If it's the servlet which is supposed to be doing that, then you have to make sure that the servlet ran earlier in the same request. And of course it's impossible for the JSP to make sure that a servlet ran earlier in the same request -- code can't affect what happened in the past.

But then you didn't say that the <c:forEach> loop was trying to use data from the request scope. You didn't say anything at all about it, in fact.



Sorry about that. Of course, the servlet forwards the List atrribute to the JSP, and in the c:forEach i retrive that List attribute from the request.

I'm looking for way to display data from database in the JSP without making any request, without calling any servlet. Is it impossible?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marek Krokosinski wrote:I'm looking for way to display data from database in the JSP without making any request, without calling any servlet. Is it impossible?



Of course it's impossible. If you don't send any request to the server then naturally it won't send you any responses.

However let's suppose you didn't really mean that. For some strange reason you don't want any servlets involved in processing the request you do send. Then you could put code into the JSP to get the data from the database. But the usual way to do things is (as I already said) to send the request to the servlet, to have it extract the data and forward to the JSP and so on. You seem to be resisting that idea. Why?
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Marek Krokosinski wrote:I'm looking for way to display data from database in the JSP without making any request, without calling any servlet. Is it impossible?



Of course it's impossible. If you don't send any request to the server then naturally it won't send you any responses.

However let's suppose you didn't really mean that. For some strange reason you don't want any servlets involved in processing the request you do send. Then you could put code into the JSP to get the data from the database. But the usual way to do things is (as I already said) to send the request to the servlet, to have it extract the data and forward to the JSP and so on. You seem to be resisting that idea. Why?



yes I didn't really mean that. There always must be request and response If I want something from server (in this case it will be data from database).

Why I don't want any servlets involved in the processing the request? I can't say that I don't want. I have a problem which is complicated for me. I don't know how to send request without using form. With forms everything is simple, we tell which method servlet should handle, we set the action to the right servlet, we press the submit button and the request is send to the servlet right? But when I have a JSP page without any form, how can I send the request? If I would know that, maybe it would be easier, because now

In my servlet i've got:



and I have a page.jsp with only that:



This is (as You said) the usual way to do things like retriving data from db. I'm retriving data in servlet and forwarding it to the page.jsp . It works perfect, if I'm requesting the servlet (by typing it's address in the address bar), but I will repeat once again, I want to include that result in the other page. Let say that in the page.jsp, I want to have list of the news (from db). I want to include page.jsp with news on my main page. If I would include page.jsp I would see nothing, because I didn't make request for the servlet (which sets the request attribute with news and forward it to the jsp), I made request only for the page.jsp.

How can I be sure that the servlet will ran, and forward this variable to the JSP, before the page.jsp will be displayed? How can I include a JSP page, which needs to be processed by the servlet before it will be included? I think this is my main question.
 
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
You don't need a form to invoke a servlet. You can use any link using the same URL as you would use for a form. If you need to pass parameters, those can be passed on the query string. The <c:url> action can help with formatting the URL correctly.

This is an important lesson on critical thinking. You didn't know how to invoke a servlet without a form so you asked "How do I avoid a servlet?". That was obviously the wrong question. The question should have been "How do I invoke a servlet without a form?"

You don't want to avoid the servlet. You always want the servlet to execute prior to the JSP.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood that I have to use servlets to proccess the requests, but how to invoke servlet before including JSP page? <c:url> is only for putting links right and passing parameters. If I have <jsp:include page='page.jsp' /> i don't know why I would like to use c:url here.

 
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
A JSP include directive has nothing to do with linking to a servlet. The <c:url> action will help you create a link that will call a servlet.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote: The <c:url> action will help you create a link that will call a servlet.



It's useful information, I will remember that. For now it won't help me, because I need to include a page which has to be processed by servlet before including. I don't know how to do it.
 
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
You always reference the servlet, not the JSP. Wherever you feel the urge to reference the JSP, just stop, and don't do it. Reference the servlet instead. It's really very very simple.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You always reference the servlet, not the JSP. Wherever you feel the urge to reference the JSP, just stop, and don't do it. Reference the servlet instead. It's really very very simple.



So I should include my servlet?
 
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
If you want to embed the result of processing the JSP at the point of include, yes.

This is one of the reasons that it's best to place the JSPs under a folder in the WEB-INF hierarchy. That way, you can't directly the JSP and always must go through its servlet controller. Takes the guesswork out of it.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:If you want to embed the result of processing the JSP at the point of include, yes.

This is one of the reasons that it's best to place the JSPs under a folder in the WEB-INF hierarchy. That way, you can't directly the JSP and always must go through its servlet controller. Takes the guesswork out of it.



But when I include servlet, I see only the page.jsp which I want to include, I don't see the rest of the JSP which is including page.jsp
 
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 done a View Source at the browser to try and find out why? That will show you what's been sent to the browser? Is it correct? Is it valid HTML? Is it what you expected?
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I saw it. It would be correct if I would like to see only the included servlet. But on my main JSP, I have also other elements (div, <h1> tags etc). When I have changed in my index.jsp the <jsp:include page="page.jsp" /> to the <jsp:include page="MyServlet" /> (which dispatch the attributes to the page.jsp and forwards request and response) I see the page.jsp, not the index.jsp.
 
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
So you have verified that the response to the browser contains only the included response? That's really odd -- there should be no difference, the include should only care about the response from the URL you give it. At this point you may need to strip your code down to only essentials and post it.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet


/WEB-INF/page.jsp:


index.jsp file:


When I enter on index.jsp, I see only page.jsp.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those relative URLs you have in your index.jsp: what URL are they relative to?
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
index.jsp is in the "main" folder, and the main folder and template folder are in the WebContent
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That wasn't what I asked, and it doesn't answer the question. You used relative URLs in your code. The specifications for JSP and for jsp:include tell you what URL they are relative to -- so look that up. I expect you are making an assumption about what they are relative to, and it isn't correct.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:That wasn't what I asked, and it doesn't answer the question. You used relative URLs in your code. The specifications for JSP and for jsp:include tell you what URL they are relative to -- so look that up. I expect you are making an assumption about what they are relative to, and it isn't correct.



These URLs are relative to the context path I think, and they have to be correct, because if I won't include the servlet - all of these elements are displayed corectly. When I Include the servlet, it makes that the page (index.jsp) is redirected to the servlet. And only servlet is displayed. I have saw that in the source code in my browser. There is no <head> tags, no <body> tags. There is only the div, <h1> and the links from just like in the the page.jsp .

If I will add pageContext.request.contextPath/WebContent/template/Header.jsp I get a RuntimeException file not found, if I will add /template/Header.jsp - it works exactly the same like '../template/Header.jsp'
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you have "WebContent" in that path? It looks to me like you're confusing the directory structure of the tool you are using to develop the application and the directory structure of the deployed application.

Presumably WebContent is the root folder for all of your artifacts -- that's how it works in my Eclipse-based IDE. But you only deploy everything underneath that folder into your web application. You are never going to have a URL with "WebContent" anywhere in its path.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
without the webcontent it's the same error. But paths must be good if this works without including servlet I think Of course i'm telling about '../template/Header.jsp'
 
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
"Works" doesn't always mean "good". As in this case. Page-relative paths like those that start with .. are fragile and easily broken. Your internal paths should start at the web context root.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok I will change it to the pageContext.request.contextPath

And still my problem with including servlet is not solved yet :> I'm still redirected to the page.jsp with servlet included :>
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know exactly if this solution is correct, but I have replace the <jsp:import> with <c:import> and now it's working as I wanted, but currently I don't know if this is correct according to the convention and programming rules.

I think that this is a bad idea, because in each request of the page, I get connection to the database, Im retriving data and I'm closing connection with this solution (i get menu from database).
 
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
The import action is fine to use. In fact, I'd say it'd be the recommended approach.
 
Marek Krokosinski
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have last question to this subject and it will be all of what I should know for the moment.
On each refresh of the page with the previous code the servlet made a connection to the database (to get the menuitems). I have added init(ServletConfig) method and I have something like this now:


Is this a good solution ? Of course, if my menu will change in database, I will have to redeploy whole app (to initialize servlet again). And the second question is - now I can't call the session.close(), because in the second request made to the page, the session will be already closed and I will get an exception. Should I leave the session open ? I have tried to close session in init(ServletConfig) method and reopen it in the doPost() but it still throws exception that session is already closed.
 
I want my playground back. Here, I'll give you this tiny ad for it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic