• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Servlet does not redirect to the page  RSS feed

 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am a newbie in this environment, but I can not to undestand what is the problem.

Scenario:
I'm developing an e-commerce. I followed the MVC pattern, so I have the folder "views" where I placed the .jsp, and in the package "controller" is the Servlet named ControllerServlet.
In the homepage (index.jsp) I have to do this: When you click on a category of products, say Technology, what I expect is that the Servlet handle the request, then link at the appropriately category file .jsp

This is my snippet od HTML code in index.jsp:

In the aboe code, <a href="category"> should be managed from the ControllerServlet

Deployment Desciptor web.xml:


In the Servlet, I have write this snippet of code just for try in the doGet() method:


Thank you in advance for any support!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you are doing a forward, not a redirect. They are completely different concepts.

Secondly, if you placed the JSPs in a folder, would you not expect to have to specify that folder in the forward path?
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The category.jsp firs it was in WebContent/WEB-INF/Views.
Now to try I placed in WebContent
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:The category.jsp firs it was in WebContent/WEB-INF/Views.
Now to try I placed in WebContent

Why move the file? That's a bad idea. Using folders for organization is good thing.

Fix the path, don't move the file.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why I thought that could be the problem, since the files in WEB-INF they are not accessible..
I put it back in its place -> WEB-INF/Views, but I have not solved
I get 404
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files in WEB-INF are not served via URL, but they can be referenced by forwards. That's why it's a good place for JSPs.

What is your new forward call? Are you specifying the content-relative path?
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Files in WEB-INF are not served via URL, but they can be referenced by forwards. That's why it's a good place for JSPs.

What is your new forward call? Are you specifying the content-relative path?


ok.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't fix the path. The method can't find the file if you don't tell it where it is!

Hint: the context root is the folder that the WEB-INF is in. What's the path to get to the JSP from there?
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You didn't fix the path. The method can't find the file if you don't tell it where it is!

Hint: the context root is the folder that the WEB-INF is in. What's the path to get to the JSP from there?


"/WebContent/WEB-INF/Views/category.jsp"

This is the path
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to change the path but still the same problem!!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WebContent is not part of the context. It is an artifact of the IDE to hold the context, but is not part of the context.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then from WEB-INF?
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given what you have said so far, I would expect to see:

 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Given what you have said so far, I would expect to see:



Perfect, but I get yet 404..

is right?

Now the path is right, why not working?!
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Perfect, but I get yet 404..



"<a href="category">"
is right?

Now the path is right, why not working?!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the JspFaq on formatting resource URLs. Your in-page URLs (like links) should start with the context path.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Please read the JspFaq on formatting resource URLs. Your in-page URLs (like links) should start with the context path.


Oh thanks!

In part I can say that the I solved, but I think my code is not entirely "correct".
I still have some doubts ...

In the HTML index.jsp, in link I put simpy "category":
<a href="category">

And it's fine.

1. On the other hand, trying with context-relative path: /WEB-INF/Views/category See the blank page.

2. To do this, I put the forward in the service() method, since in the doGet() it does not work. Why?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:And it's fine.

It might happen to work, but it's fragile and easily broken. You should use a context-relative path like the JspFaq point out.

1. On the other hand, trying with context-relative path: /WEB-INF/Views/category See the blank page.

Because the path of a JSP has nothing at all to do with your servlet. The path to the servlet should be the context path follow by the servlet path. Please see the JspFaq I mentioned.

2. To do this, I put the forward in the service() method, since in the doGet() it does not work. Why?

You should not be defining a service method. If there's something wrong wit the get() you need to fix that.

Make the changes above and post back with results.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Because the path of a JSP has nothing at all to do with your servlet. The path to the servlet should be the context path follow by the servlet path. Please see the JspFaq I mentioned.


Ok, now works.


You should not be defining a service method. If there's something wrong wit the get() you need to fix that.


But the doGet() method not works

This is my HTML jsp:
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault, I solved!

The reasons why doGet() method not called is why I overrides the service() method that it should not be done in HttpServlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!