user1782677
user1782677

Reputation: 2007

Dynamically create url in jsp file

So I have a jsp file and inside this file I want to dynamically generate the url to a new jsp file.

How do I do this? If I simply write <a href="newfile.jsp"> " Click here" </a>, and then run my program, when I go to click on the url I just get a 404 error instead of a blank new page. What else is necessary to set this up?

Upvotes: 0

Views: 1829

Answers (2)

D.S
D.S

Reputation: 1150

Either do it using Servlet as mudalov explained or you could use .htaccess file and define any url e.g. something.html or something.jsp to be redirected to desired JSP/SERVLET

RewriteRule ^/(.*)\.html /servlets/controllerServlet/id=$1

So if your url is newfile.html then your controllerServlet will receive the request with parameter id="newfile"

Upvotes: 0

udalmik
udalmik

Reputation: 7988

So, as I see you need to create some Web resource on the fly. I don't think that generating of JSP pages is the right way to approach that. You could have one Servlet to handle that requests, also it could have some Web resources registry. Simple scenario:

  1. User clicks on the link /createProductA => Servlet saves this 'productA' with some corresponding information (some details, info, corresponding template to render, etc)
  2. Then you hit /vewProductA => Servlet handles this too, but know that need to provide the details page. It grabs information from its registry and renders required template. For client it would be the same as a page.

This scenario could help if you need to change rendering templates on the fly, e.g. in database. If your details page layout is static, then you can have one page, e.g. /viewProduct.jsp and pass the id to it. It is a common approach for Java web applications.

Upvotes: 1

Related Questions