该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这是HandleLoogin.java文件
package loog;
import up.*;//bean的包
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleLoogin extends HttpServlet
{ public void init(ServletConfig config) throws ServletException
{ super.init(config);
try { Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
}
public String handleString(String s)
{ try{ byte bb[]=s.getBytes("iso-8859-1");
s=new String(bb,"GB2312");
}
catch(Exception ee){}
return s;
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ Connection con;
PreparedStatement sql;
Loogin looginBean=null;
String backNews="";
HttpSession session=request.getSession(true);
try{ looginBean=(Loogin)session.getAttribute("loogin");
if(looginBean==null)
{ looginBean=new Loogin();
session.setAttribute("loogin",looginBean);
}
}
catch(Exception ee)
{ looginBean=new Loogin();
session.setAttribute("loogin",looginBean);
}
String loogname=request.getParameter("loogname").trim(),
password=request.getParameter("password").trim();
boolean ok=looginBean.getSuccess();
loogname=handleString(loogname);
password=handleString(password);
if(ok==true&&loogname.equals(looginBean.getLoogname()))
{ backNews=loogname+"已经登录了";
looginBean.setBackNews(backNews);
}
else
{ String uri="jdbc:mysql://localhost/test";
boolean boo=(loogname.length()>0)&&(password.length()>0);
try{ con=DriverManager.getConnection(uri,"shuju","");
String condition="select * from student where logname =? and password =?";
sql=con.prepareStatement(condition);
if(boo)
{ sql.setString(1,loogname);
sql.setString(2,password);
ResultSet rs=sql.executeQuery();
boolean m=rs.next();
if(m==true)
{ backNews="登录成功";
looginBean.setBackNews(handleString(backNews));
looginBean.setSuccess(true);
looginBean.setLoogname(loogname);
}
else
{ backNews="您输入的用户名不存在,或密码不般配";
looginBean.setBackNews(handleString(backNews));
looginBean.setSuccess(false);
looginBean.setLoogname(loogname);
looginBean.setPassword(password);
}
}
else
{ backNews="您输入的用户名不存在,或密码不般配";
looginBean.setBackNews(handleString(backNews));
looginBean.setSuccess(false);
looginBean.setLoogname(loogname);
looginBean.setPassword(password);
}
con.close();
}
catch(SQLException exp)
{ backNews=""+exp;
looginBean.setBackNews(backNews);
looginBean.setSuccess(false);
}
}
RequestDispatcher dispatcher=request.getRequestDispatcher("showLooginMess.jsp");
dispatcher.forward(request, response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ doPost(request,response);
}
}