如何在jsp中插入js

JavaScript05

如何在jsp中插入js,第1张

jsp代码中间插入JS代码的格式,代码如下:

<script lanuage="javascript">function justiice(t){

if (t>0){

alert("account is exsiting")

 }

}

else{

'redirect the other page!'

}

</script>

<%int i=conn.executeQuery("select count(1) fromtable where id='输入帐户'")%>

<input type=button onclick=justiice('<%=i%>')>

<! But i want to tell you that the sql statement you can't insert into your current page, it may incur SQL injection,Be cautious!>

在javascript中嵌入jsp代码和在jsp中动态组装javascript代码是不一样的概念。<br><br>如果你的javascript写在了html页面中,那么你的jsp代码是不会被编译的。<br>如果你在jsp中动态组装了javascript代码,那么jsp页面会被编译为一个class类,这个类会跟你写的逻辑会输出(out.println()方式)javascript代码。举例来说:<br><br><br>1. 在javascript中写jsp代码:<br><javascript><br><br> alert(“<% int i=1i++out.println(i)%>”)<br></javascript><br>上面的代码段如果放在jsp页面中<% int i=1i++out.println(i)%>会被编译,但是放在html页面中不会被编译。<br><br><br>2. 在jsp页面中动态组装javascript代码:<br><% if(logintype =="admin"){<br>out.println("<javascript>function showtype{document.getelementbyid('type').innerhtml = "admin" } </javascript>")<br>}else{<br>out.println("<javascript>function showtype{ document.getelementbyid('type').innerhtml = "admin" } </javascript>")<br><br>}%><br><br>上面这个jsp代码段放在合适的jsp页面中这个javascript是会被发送到client的,你就可以在client使用这个showtype方法。<br><br><br><br><br>总之一句话:javascript代码是给浏览器执行的,jsp代码是在server端编译的,jsp代码必须放在jsp页面中经server编译,而javascript只要发送给client就行。