Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Importing javascript in a JSP  RSS feed

 
sunil kumbs
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to import java script into my jsp as below:

<script type="text/javascript" src="/My_Development/JS/validate.js"></script>

When the jsp page loads for the first time, all the js functions I'm calling in my jsp works fine.

I've some validations in my jsp page, like if you enter a name which already exists in DB, i load the same jsp page with the error message.

Now, here comes the problem. When I load the jsp second time, the js code doesn't work.

Any ideas why the js code doesn't work second time???

Thanks
Sunil
 
Devaka Cooray
Marshal
Posts: 5564
712
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to identify what was happening without having look into the actual source.
Sounds like much related to JavaScript. Moving to the JavaScript forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "/Development" the cpntext path?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to the JSP forum, as it's not related to the script at all -- but the URL to address it.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sunil kumbs wrote:
I've some validations in my jsp page, like if you enter a name which already exists in DB, i load the same jsp page with the error message.


Are you reloading the page using RequestDispatcher?
If yes, the URL would be the url-pattern of the servlet you'd called and not the jsp page.

And if the url-pattern specifies different folder, browser may try to find the javascript page at wrong place.
But it shouldn't give any probs, as you use absolute URL in:
<script type="text/javascript" src="/My_Development/JS/validate.js"></script>

See, whether you can access the Javascript page when you load the JSP for second time.
If no, then Javascript page is not available and JS code won't work.
If yes, then there is some different issue associated with this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trivikram Kamat wrote:But it shouldn't give any probs, as you use absolute URL in:

Terminology nit: an "absolute" URL is one that starts with http: or https:

What's needed is a"server-relative" URL that begins with the context path.

Guaranteed to cause problems are "page-relative" URLs.
 
sunil kumbs
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear members,

I think they way I'm specifying the URL, is server-relative only.

What my doubt is if the jsp page is able to render the js code when it loads first time, why the js code is not working the second time the jsp page loads.

If there is a problem with the URL of js file, it will not work in the first time also.

There is one alternate solution i can find and that is writing the js code in my jsp, but i have a lot of js code which is referred by so many other jsps and it's not good to put all of same js code in all the jsps.

I'm using IE 6.0 as browser(I don't know if my issue has got anything to do with browser version), and I can't upgrade it due to some restrictions.

Thanks
Sunil

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sunil kumbs wrote:What my doubt is if the jsp page is able to render the js code when it loads first time, why the js code is not working the second time the jsp page loads.

Is the address shown in the URL exactly the same for both instances? For example, if you address the JSP directly the first time, and forward to it from a servlet controller the second time, there will be different addresses.

If there is a problem with the URL of js file, it will not work in the first time also.

Incorrect. Page-relative URLs will be interpreted relative to the URL of the page and may have inconsistent results.

You still haven't answered my question. What is the context path, and how are you using it to form the reference to the JS files?

There is one alternate solution i can find and that is writing the js code in my jsp, but i have a lot of js code which is referred by so many other jsps and it's not good to put all of same js code in all the jsps.

That's not a solution. That's a poor band-aid.

I'm using IE 6.0 as browser(I don't know if my issue has got anything to do with browser version), and I can't upgrade it due to some restrictions.

My condolences.
 
sunil kumbs
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Mr. Beer Bibeault

Thanks a lot.

After analyzing your first two comments I knew where I've made the mistake.

The code where I load the js with <script type="text/javascript" src="/My_Development/JS/validate.js"></script> , loads the js correctly everytime, however in my page i load one more js with <script language="JavaScript" src="../../JS/calendar_eu.js"></script>.

The second one is the culprit, as you have mentioned rightly in your second comment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!