JSP里面插入JAVAscript代码怎么不管用

我这样写不管用
<body>
<%
out.print("<script type='text/javascript'>alert('添加失败!');</script>");
response.sendRedirect("../index.jsp");
%>
</body>

这样写也不管用
<head>
<script type='text/javascript'>
function load()
{
alert('添加失败!');
}

</script>
</head>

<body>
<%
response.sendRedirect("../index.jsp");
%>
</body>

到底要怎么写才可以实现JSP页面弹出消息框呢????

你把页面输出的权限弄错了。
jsp代码永远高于js是从服务端返回的。以后才会执行js。
response.sendRedirect("../index.jsp");是服务端执行跳转的
你的跳转要用js的window.location.href来写就可以的

<%
out.print("<script type='text/javascript'>alert('添加失败!');window.location.href='../index.jsp';</script>");
%>
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-09-14
jsp里面直接在head里面添加
<script type='text/javascript'>

alert('添加失败!');
</script>
是可以弹的
第2个回答  2013-09-14
直接定义function load(){alert("添加失败!");} 这在页面加载后是不会执行的,在加一句:
window.onload=load; 这样在页面加载后就会显示弹出框
相似回答