读书人

jsp开发为什么不能运行javascript脚本

发布时间: 2012-03-06 20:47:55 作者: rapoo

jsp开发为什么不能运行javascript脚本?
检查登陆的jsp页面!代码如下:

<%@ page contentType= "text/html; charset=GBK " language= "java " import= "java.sql.*,java.util.*,java.io.* "%>
<html>
<head>
</head>
<body bgcolor= "white ">
<%
String userid=new String(request.getParameter( "id ").getBytes( "iso-8859-1 "));
String password=request.getParameter( "pwd ");

if((userid!=null)&(password!=null)){
out.print( " <script language= 'JavaScript '> alert( '帐号和密码不能为空! '); </script> ");
response.sendRedirect( "index.html ");
}

try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
Connection con=DriverManager.getConnection( "jdbc:odbc:myweb ", " ", " ");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery( "select * from user_info where userid= ' "+userid+ " ' and password= ' "+password+ " ' ");

while(rst.next()){
session.setAttribute( "name ",rst.getString( "username "));
response.sendRedirect( "homepage.jsp ");
}
if(!rst.next()){
out.print( " <script language= 'JavaScript '> alert( '帐号或密码错误! '); </script> ");
response.sendRedirect( "index.html ");
}

rst.close();
stmt.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}


%>
<body>
</html>

在测试的过程中!用空用户与空密码登陆时! 不能弹出提示框!而是直接跳转到index.html.

即 out.print( " <script language= 'JavaScript '> alert( '帐号和密码不能为空! '); </script> ");这没有执行

而是执行下一句....我用的是Tomcat服务器!不知道什么原因!请高手指点!


[解决办法]
msg= " <script> window.alert( '验证码错误 '); window.location= '../login.jsp '; </script> ";
response.getWriter().write(msg);

读书人网 >Java面试

热点推荐