• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic