• 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

Dynamically generation of JSP/HTML file

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
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
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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 can use RESTful URLs without a RESTful web service.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic