最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - using javascript in .jsp file - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

Usually 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.

发布评论

评论列表(0)

  1. 暂无评论