This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Dynamically generation of JSP/HTML file  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm developing a web application focused in DB managing. For that, I'm trying to be as strict as I could with the MVC model.

I've created some jsp templates for the different objects hold by the DB. e.g, I have a JSP called "competition.jsp" where I show all the details stored about a sport competition. I reach this page from a link, passing as parameter the competition ID to the servlet, which creates a JavaBean with the data from the DB associated with this competition ID and redirect it to the "competition.jsp" in order to show the data. So, in the browser I get a page named "www.example.com/servlet/competition.jsp", which will be the same for all the different competitions.

The problem is that I think having different URLs for different competitions is quite a lot important. I rejected adding a key to the query like "www.example.com/servlet/competition.jsp?id=123", would prefer to have an URL like "www.example.com/servlet/competitions/world-cup.jsp" or "www.example.com/servlet/competitions/world-cup.html", so I thought about generate a new HTML file from the "competition.jsp", calling it "world-cup.html".

It would be a good practice? Which could be the best practice in this case? Remember that I'm using the MVC pattern.


Simplifying, I'm doing something like that at now:

I have a link in a page with a javascript that submit the ID to the servlet:

Then in the servlet, I instantiate a bean with the data from de DB, set it as session attribute and redirect to "competition.jsp":

And finally, I show the data of the competition using JSTL:

Thank you in advance,
Hernán.
 
Author and ninkuma
Marshal
Posts: 66809
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best practice would be to use a RESTful URL: /competitions/id

P.S. Using "servlet" anywhere in the URL is not considered professional.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66809
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ALso, forward from the page controller to the JSP, not redirect. The JSP should never be directly addressed.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the answer!

Yes, I already knew that's not professional using the word "servlet" in the URL, it was only for illustrate the example. I heard something about REST, I will take your advice and take a look about this!

Hernán.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66809
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use RESTful URLs without a RESTful web service.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing that you said, trying to implement a friendly URL pattern in my application. I'm doing it with a Filter that parses the URL into attributes and then forwarding to the servlet.

But now, my problem is different. I added it in my web.xml:


When a JSP is loaded, it tries to download some JavaScript and CSS files, like that:


So the script or stylesheet requests will pass another time across the filter, because the <url-pattern> is "/*". I was thinking in a solution for that (defining another filter, using differents url-patterns...) but I don't know what is the most correct and easy way to solve it. Can anyone help me?

Hernán.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66809
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never, ever, ever map "/*".

Use a mapping for your servlets such as "/whatever/*" where whatever is something that makes sense to the domain of your data. Then map your filter to the servlet(s).
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you are right once more... redifining the estructure of the projec was sufficient (I had a very basic structure because those are my firsts steps with the project). Anyway, maintaining the relatives URLs in the project (mainly in <c:import> tags) seems to be so difficult, as the project grows with the time.

Thank you!
Hernán.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!