• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
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.
 
Marshal
Posts: 67366
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Bartender
Posts: 1845
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:
 
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.
 
Bartender
Posts: 21737
148
Android Eclipse IDE Tomcat Server Redhat Java 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.
 
Attractive, successful people love this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!