答:jsp中java代码可以写在js中。
示例如下:
<script type="text/javascript">
function check(){
var username=document.getElementById("username")
var username=document.getElementById("password")
//java代码块
<%
String sql="select * from Manger"
String user
String pass
ResultSet rs=DBHelper.query(sql)//sql代码已经写好在被的类中,此处为直接调用!!
while(rs.next()){
user=rs.getString("username")
pass=rs.getString("password")%>
if(username.value=="<%=user %>" &&passsword.value=="<%=pass%>")
{
return true
}else if(username.value==""|| password.value==""){
username.setCustomValidity("账号或密码不能为空")
return false
}else{
username.setCustomValidity("账号或密码错误,请从新输入")
return false
}
<%
}
%>
}
</script>
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!>