validate_code.append(getOneChar(type));
}
returnvalidate_code.toString();
}
//根据验证码类型取得实际验证字符
//类型0-取得0-9之间的数字
//类型1-取得A-Z之间的字母
//类型2-随机取得数字或是字母的组合
privateStringgetOneChar(inttype){
Stringresult=null;
switch(type){
caseTYPE_NUMBER:
result=String.valueOf(random.nextInt(10));
break;
caseTYPE_LETTER:
result=String.valueOf((char)(random.nextInt(26)+65));
break;
caseTYPE_MULTIPLE:
if(random.nextBoolean()){
result=String.valueOf(random.nextInt(10));
}else{
result=String.valueOf((char)(random.nextInt(26)+65));
}
break;
default:
result=null;
break;
}
if(result==null)
{
thrownewNullPointerException("获取验证码出错");
}
returnresult;
}
/**
*ThedoPostmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
/**
*Initializationoftheservlet.
*
*@throwsServletExceptionifanerroroccurs
*/
publicvoidinit(ServletConfigconfig)throwsServletException{
super.init(config);
width=150;
height=50;
count=4;
type=TYPE_NUMBER;
random=newRandom();
line=200;
}
}
3.配置web.xml,添加对CheckCodeGet的配置,配置代码如下:
ThisisthedescriptionofmyJ2EEcomponent
ThisisthedisplaynameofmyJ2EEcomponent
CheckCodeGet
cn.javaee.session.CheckCodeGet
CheckCodeGet
/CheckCodeGet
4.可以发布工程进行测试登录页面,能够打开如图1所示的界面。
输入如图2所示的正确结,点击“提交”按钮后,读取数据库中的记录,查找和用户账号及密码匹配的记录,如果找到,转到主页mainPage.jsp,提示用户登录成功,如图3所示。
如果找不到,再次转到登录界面。
在src下新建一个Servelt类LoginServlet用于处理“提交”表单后的处理,LoginServlet.java的代码如下:
packagecn.javaee.session;
importjava.io.IOException;
importjava.io.PrintWriter;
import.URLEncoder;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.Cookie;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
publicclassLoginServletextendsHttpServlet{
privateConnectioncn=null;
publicLoginServlet(){
super();
}
//销毁方法,关闭数据库连接,释放对象占用内存
publicvoiddestroy(){
//TODOAuto-generatedmethodstub
super.destroy();
try{
if(cn!
=null&&(!
cn.isClosed())){
cn.close();
cn=null;
}
}catch(Exceptione){
System.out.println("DestroyError:
"+e.getMessage());
}
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
StringuserID=request.getParameter("userName");
Stringpassword=request.getParameter("password");
Stringsql="select*fromstudent_infowherestu_id='"
+userID+"'";
Statementst=null;
try{
st=cn.createStatement();
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
ResultSetrs=null;
try{
rs=st.executeQuery(sql);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
try{
if(rs.next()){
StringdbPassword=rs.getString("stu_pwd").toString();
StringdbUserName=rs.getString("stu_name").toString();
if(dbPassword.equals(password)){//输入的密码正确
StringcookSelected=request.getParameter("chkSelect");
if(cookSelected!
=null){//设置Cookie的保存时间
Cookiename=newCookie("myname",URLEncoder.encode(userID,"utf-8"));
Cookiepass=newCookie("mypass",URLEncoder.encode(password,"utf-8"));
StringageString=newString(request.getParameter("selectTime").getBytes("ISO-8859-1"),"UTF-8");
//System.out.println(ageString);
if(ageString.equals("一天")){
name.setMaxAge(24*60*60);
pass.setMaxAge(24*60*60);
}elseif(ageString.equals("一周")){
name.setMaxAge(7*24*60*60);
pass.setMaxAge(7*24*60*60);
}elseif(ageString.equals("一月")){//假定一月就按30天算,不计详细的哪个月份
name.setMaxAge(30*24*60*60);
pass.setMaxAge(30*24*60*60);
}
response.addCookie(name);
response.addCookie(pass);
}
HttpSessionsession=request.getSession(true);
session.setAttribute("userName",dbUserName);