wood burning stoves 2.0*
The moose likes JSP and the fly likes Dynamically generation of JSP/HTML file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Dynamically generation of JSP/HTML file" Watch "Dynamically generation of JSP/HTML file" New topic
Author

Dynamically generation of JSP/HTML file

Hernan Blanco
Greenhorn

Joined: Aug 23, 2010
Posts: 28
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Best practice would be to use a RESTful URL: /competitions/id

P.S. Using "servlet" anywhere in the URL is not considered professional.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

ALso, forward from the page controller to the JSP, not redirect. The JSP should never be directly addressed.
Hernan Blanco
Greenhorn

Joined: Aug 23, 2010
Posts: 28
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

Joined: Jan 10, 2002
Posts: 61447
    
  67

You can use RESTful URLs without a RESTful web service.
Hernan Blanco
Greenhorn

Joined: Aug 23, 2010
Posts: 28
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

Joined: Jan 10, 2002
Posts: 61447
    
  67

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

Joined: Aug 23, 2010
Posts: 28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically generation of JSP/HTML file