admin管理员组文章数量:1302904
So in eclipse when you generate a .jsp file it automatically includes the following top line:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Is there a way to also include javascript code so that I can have some of the file written in java and some written in javascript?
So in eclipse when you generate a .jsp file it automatically includes the following top line:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Is there a way to also include javascript code so that I can have some of the file written in java and some written in javascript?
Share Improve this question asked Mar 15, 2013 at 2:21 user1782677user1782677 2,0075 gold badges27 silver badges48 bronze badges 4- 1 Um, include an external JavaScript file? Do you realize that Java and JavaScript do not run at the same time? – epascarello Commented Mar 15, 2013 at 2:21
- 2 But what if I need to use information from my java code? For example, I want to query a mysql database using java and then use the information I get there to do stuff with javascript – user1782677 Commented Mar 15, 2013 at 2:23
- 1 Learn about the page lifecycle. – epascarello Commented Mar 15, 2013 at 2:24
- 1 That's a very vague statement. – user1782677 Commented Mar 15, 2013 at 2:25
2 Answers
Reset to default 4Usually I include my JavaScript in JSP like this:
<script type="text/javascript" src="js/script.js"></script>
This way I can minimize the js code and also cache it differently than JSP files in Tomcat.
The answer is Yes, you can add the js in jsp. And this also has benefit. It helps you to use the values stored in request and session, because the js code which contains the jsp stuff in jsp will be piled while the *.js file will not. And also you should keep in mind that you'd better put the js code in .js file not in jsp, so the browser doesn't need to load it repeatly and code seems more clear, also good for debug. Another answer has give you the example code to import js file, so I just ignore it.
本文标签: javausing javascript in jsp fileStack Overflow
版权声明:本文标题:java - using javascript in .jsp file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741733364a2394948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论