• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Michael Stitch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1782
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18282
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic