0

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?

tckmn
  • 57,719
  • 27
  • 114
  • 156
user1782677
  • 1,963
  • 5
  • 26
  • 48
  • You need to provide more information to have an answer. Where is the second page located within the app? What 'url' do you use to click on first page? – udalmik Feb 19 '13 at 13:51
  • Well that's my question. The second page isn't located anywhere because I haven't created it. I want to know how to create it 'on the fly' when I click on the link (because the link in my program will change based on different factors). The first page is the homepage and so I start the program directly from there. No link required. – user1782677 Feb 19 '13 at 13:53
  • @user1782677 You shouldn't try to create a page on the fly. If your page will change based on different factors you can pass different parameters in the url to the page which will control the way the page loads. – Danny Feb 19 '13 at 14:09
  • So what I need to do is I have a bunch of products for a store (not a real store) and when the link to that product is clicked it should take you to a page that displays that product in more detail. So I guess from what you are saying I should have a default product-display page that displays different things based on the parameter I pass in along with the url? So I know I can do this by doing something like: filename.jsp?id=myID but then once i'm in the .jsp file what do I call to make use of that parameter I passed in? – user1782677 Feb 19 '13 at 14:18
  • See this: http://stackoverflow.com/questions/1890438/how-to-get-parameters-from-the-url-with-jsp – mavroprovato Feb 19 '13 at 14:20

2 Answers2

1

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.

udalmik
  • 7,838
  • 26
  • 40
0

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"

D.S
  • 1,110
  • 3
  • 11
  • 26