Servlet学习笔记(五)-----使用Servlet处理用户注册和登陆(使用了Cookie)

类别:Java 点击:0 评论:0 推荐:

1.       和实例3差不多,唯一的区别是在LoginForm.java中多了对Cookie的调用,当你注册号了之后,第一次登陆时会出现提示语”This is the first time you have logged on”,以后每次登录都会告诉你登陆的次数,当你登陆到第五次时会出现"Congratulations!!!,You have a gife"

2.       LoginForm.java的代码如下:

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.sql.*;

///import javax.sql.*;

 

public class LoginForm extends HttpServlet

{

       Connection dbcon;

      

       protected void doPost(HttpServletRequest req,

                      HttpServletResponse resp)

               throws ServletException,

                      java.io.IOException

    {

          

           try

           {

                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                  dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");

                  System.out.println("Connection established");

           }

           catch(Exception e)

           {

                  System.out.println("Database not found!");

           }

           resp.setContentType("text/html");

           PrintWriter out=resp.getWriter();

          

           String username=req.getParameter("username").trim();

           String pass=req.getParameter("pass").trim();

          

          

          

           try

           {

                  PreparedStatement stat=dbcon.prepareStatement(

                           "select * from UserData where username=? and pass=?");

                      stat.setString(1,username);

                      stat.setString(2,pass);

                      ResultSet result=stat.executeQuery();

                      if(result.next())

                    {

                           out.println("<html><body bgcolor=pink>");

                         out.println(" Login Successful");

                        

                         out.println("<br><br>");

                         //Cookie

                        

                         boolean cookieFound=false;

                         Cookie myCookie=null;

                         Cookie[] cookieSet=req.getCookies();

                        

                         for(int i=0;i<cookieSet.length;i++)

                         {

                               

                                if(cookieSet[i].getName().equals("logincount"))

                                {

                                       cookieFound=true;

                                       myCookie=cookieSet[i];

                                }

                         }

                         if(cookieFound)

                         {

                                int temp=Integer.parseInt(myCookie.getValue());

                                temp++;

                                if(temp==5)

                                {

                                       out.println("Congratulations!!!,You have a gife");

                                }

                                out.println("This is the "+temp+" time you have logged on");

                                myCookie.setValue(String.valueOf(temp));

                                int age=60*60*24*30;

                                myCookie.setMaxAge(age);

                                resp.addCookie(myCookie);

                                cookieFound=false;

                         }

                        

                         else

                         {

                               

                                int temp=1;

                                out.println("This is the first time you have logged on");

                                myCookie=new Cookie("logincount",String.valueOf(temp));

                               

                                int age=60*60*24*30;

                                myCookie.setMaxAge(age);

                                resp.addCookie(myCookie);

                               

                         }

                 

                        

                         //end Cookie

                         out.println("</body></html>");

                    }

                      else

                    {

                           out.println("<html><body bgcolor=pink>");

                         out.println(" Login ERROR");

                         out.println("<br><br>");

                         out.println("</body></html>");

                    }

           }

           catch(Exception e)

           {

                  e.toString();

           }

          

          

           try

           {

                  dbcon.close();

           }

           catch(Exception e)

           {

                  System.out.println(e.toString());

           }

    }

}

本文地址:http://com.8s8s.com/it/it15923.htm