This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Tomcat and the fly likes interpret java code <% ..%> in .js file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Tomcat
Bookmark "interpret java code <% ..%> in .js file" Watch "interpret java code <% ..%> in .js file" New topic
Author

interpret java code <% ..%> in .js file

Michael Stitch
Greenhorn

Joined: Nov 09, 2011
Posts: 4
Hi:
In my .jsp file I included a JS file 'abc.js'. This .js file has embedded java code to retrieve data value from a java class. For some reason Tomcat does not like the embedded java code. I was told that I have to change the Tomcat configuraiton to allow pasrsing Java inside the JSP .js file. Can someone tell me how do I configure Tomcat to do that?

abc.js -

function displaySession(){
document.getElementById("update_field" ).style.display = "<%=student.getUpdateField()%>";

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Michael Stitch wrote:In my .jsp file I included a JS file 'abc.js'. This .js file has embedded java code to retrieve data value from a java class.

Not recommend and considered a poor practice.

For some reason Tomcat does not like the embedded java code.

Of course not. JSP mechanisms only work in a JSP.

I was told that I have to change the Tomcat configuraiton to allow pasrsing Java inside the JSP .js file. Can someone tell me how do I configure Tomcat to do that?

I believe that it is possible to do that. But again, I would not recommend it.

A better practice would be to pass the dynamic data to the function from a call in the JSP rather than trying to embed it into the .js file.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
As Bear said, it is not recommended.
But if you MUST, I would recommend to do it by adding a servlet mapping into your web.xml file.

Rename your file to abc.jsp



As a result of this config
- requests to /abc.js get directed to the dynamicJavascriptFile servlet
- That servlet is served by invoking abc.jsp.


The better approach:
Miku Ranjan
Ranch Hand

Joined: Oct 11, 2011
Posts: 98
Hi,
As it is advice before as not recommended ,I ll say the same still if you require it then you can put your js file code in side a script tag of jsp and there you can use the jsp scriptlet tag.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

Just in case enough people haven't said it already, a ".js" file is not a ".jsp" file. So, byt definition, it cannot contain Java code.

In any event, J2EE long passed the stage where it was expected to be a high-priced replacement for IIS ASPs, and the very thought of having View components (JSPs) contain executable (Java) code sets off a lot of people's blood pressure monitors. We've been there (long ago), done that (when we had no alternatives) and found better ways.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
GeeCON Prague 2014
 
subject: interpret java code <% ..%> in .js file