0

I have a jsp file that passes a parameter to another jsp file. This parameter is of type string and takes the value of a folder name in the web server path (i.e. http://myserver.com/Page/Folder_Name).

In the second jsp file, I have quite a few js and css files I'm attaching using script and link tags, respectively. I need to make their paths relative to the parameter passed. My thought-process is construct dynamic source (src) paths using jsp tags and javascript.

<head>
  <link rel="stylesheet" type="text/css" href="Folder_Name/css/style.css" />
  <script type="text/javascript" src="Folder_Name/js/script.js"></script>
</head>

Except Folder_Name will be dynamic, utilizing JSP somehow. At least, that's my mindset.
The question is how may I accomplish this? I'm open to other suggestions, with the initial condition of said parameter is passed to the second jsp page.

Thank you.

2
  • 2
    How, exactly, is the parameter passed? <jsp:param> inside a <jsp:include>? It would be very helpful if you could show some code. Commented Nov 7, 2011 at 15:48
  • Thank you. I'm passing it via querystring and retrieving it using request.getParameter("FolderName") Commented Nov 7, 2011 at 16:11

1 Answer 1

2

Why do this with JavaScript? If it's already a JSP page, you're already doing server-side processing, so it seems like you'd want to use Java. E.g., once you have the folder name in a variable (say, folderName), just do this to output the links:

<head>
  <link rel="stylesheet" type="text/css" href="<%=folderName%>/css/style.css" />
  <script type="text/javascript" src="<%=folderName%>/js/script.js"></script>
</head>

(Note the <%=folderName%> bit, which runs on the server and outputs the value of the server-side folderName variable.) Or if you're using a container that supports the JSP expression language, use ${folderName} instead:

<head>
  <link rel="stylesheet" type="text/css" href="${folderName}/css/style.css" />
  <script type="text/javascript" src="${folderName}/js/script.js"></script>
</head>

You certainly could use JavaScript for the task, if you want, but I'm not sure it makes sense. You could use Rhino (JavaScript for the JVM) on the server, of course. If you're talking client-side, you'd basically have to echo the folder name into a place where the client-side JavaScript can find it (so, output it to a dynamically-generated global variable — <script>var folderName = '<%=folderName%>';</script> for example — or to a hidden input, or whatever), and then have your JavaScript add the necessary script tags via document.createElement, set their src, and append them to the head or body (doesn't matter where), but it seems a very round-about way to do it if you're already doing server-side processing.

Sign up to request clarification or add additional context in comments.

13 Comments

Ew, scriptlets? s/<%=folderName%>/${folderName}
@MattBall: Sure, if he's using JSTL, he could do it that way.
Thank you very much to you both. This is a great solution. The reason I brought up JavaScript is I am referencing paths in a separate js file and I would like to make those relative, as well. If I should post another question for that, then I will delete this comment and do so. Thank you again.
@user717236: If you need the folder path client-side as well, you can still do the <script>var folderName = '<%=folderName%>';</script> or <script>var folderName = '${folderName}';</script> thing (as long as the folder names are such that that will result in a valid JavaScript string literal, which seems likely in this specific case). Then you can use that 'folderName' variable in your client-side code. Downside is that it's a global, and I tend to shy away from globals, but...
@T.J.Crowder: you don't need the JSTL to use the JSP EL.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.