不好意思,上面代码有误
应为以下代码<html>
<body>
<%@page import="java.util.*,java.sql.*" contentType="text/html;charset=gb2312"%>
<jsp:useBean id = "pool" scope="application" class="glasses.ConnPool"/>
<%
boolean success=true; //标志登录是否成功
String name=new String(request.getParameter("usename").getBytes("ISO8859_1"),"GBK");
String password = new String(request.getParameter("password").getBytes("ISO8859_1"),"GBK");
%>
<%@include file="conn.jsp"%>//连接数据库页面
<%// 在数据库中查询该用户
sql="select * from user where user_name='"+name+"' and user_password='"password+"'";
ResultSet rs=statement.executeQuery(sql);
//如果存在该用户,则判断
if(rs.next())
success=true;
else success=false;%>
<%@include file="disconn.jsp"%>//释放数据库页面
<% if(success){
session.setAttribute("Name",name);
response.sendRedirect("list.html");
}
else {
%>用户名或密码有误,请检查大小写,重新登录!<%}%>
<a href="javascript:history.back();">返回</a>
</body>
</html>
应为以下代码<html>
<body>
<%@page import="java.util.*,java.sql.*" contentType="text/html;charset=gb2312"%>
<jsp:useBean id = "pool" scope="application" class="glasses.ConnPool"/>
<%
boolean success=true; //标志登录是否成功
String name=new String(request.getParameter("usename").getBytes("ISO8859_1"),"GBK");
String password = new String(request.getParameter("password").getBytes("ISO8859_1"),"GBK");
%>
<%@include file="conn.jsp"%>//连接数据库页面
<%// 在数据库中查询该用户
sql="select * from user where user_name='"+name+"' and user_password='"password+"'";
ResultSet rs=statement.executeQuery(sql);
//如果存在该用户,则判断
if(rs.next())
success=true;
else success=false;%>
<%@include file="disconn.jsp"%>//释放数据库页面
<% if(success){
session.setAttribute("Name",name);
response.sendRedirect("list.html");
}
else {
%>用户名或密码有误,请检查大小写,重新登录!<%}%>
<a href="javascript:history.back();">返回</a>
</body>
</html>
先把boolean success=true; //标志登录是否成功
预设为boolean success=false;
我的习惯做成如下的格式:
public boolean ExistUser(String username){
String str_sql="select * from UserInfo where UserName='"+username+"'";
boolean exist=false;
try{
Table t =ExecSQL(str_sql);
Iterator it = t.getDataIterator();
if (it.hasNext())
{ exist=true;}
else
{ exist=false;}
}catch(Exception e){e.printStackTrace();}
return exist;
}