2014-07-27 78 views
0

我有這些驗證用戶名是否與密碼匹配的登錄碼。登錄後,我希望在登錄和註銷時終止Cookie。我將如何做到這一點?如何在我登錄時終止cookie?

LoginAuthentication.java

package com.dls.csb.process.web.controller; 

import java.io.IOException; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

import com.ats.model.Account; 
import com.dls.csb.utility.SQLOperations; 
import com.dls.csb.utility.Security; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 


@WebServlet("/loginauthentication.html") 
public class LoginAuthentication extends HttpServlet { 
private static final long serialVersionUID = 1L; 


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  
ServletException, IOException { 
    doPost(request, response); 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException { 
     String userName = request.getParameter("userName"); 
     String passWord = Security.encrypt(request.getParameter("passWord")); 
     String passwordInput = request.getParameter("passWord"); 
     Account acct = new Account(userName, passWord); 
     HttpSession session = request.getSession(); 
     if(userName.equals("admin103457") && passwordInput.equals("juliefefe")) { 
      response.sendRedirect("admin.jsp"); 
      session.setAttribute("userName", acct); 

     } 

     else { 

     boolean isValidLogon = false; 
     try { 

     isValidLogon = SQLOperations.authenticateLogin(acct); 
     if(isValidLogon) { 
      session.setAttribute("userName", acct); 
      response.sendRedirect("main.jsp"); 

      } 

     else { 
      response.sendRedirect("index.jsp"); 

     } 
     } 
     catch(Exception e) { 
     e.getMessage(); 
     } 
     } 

} 
} 

ProcessRegistration.java

package com.dls.csb.process.web.controller; 

import java.io.IOException; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 


import com.ats.model.Account; 
import com.dls.csb.utility.SQLOperations; 
import com.dls.csb.utility.Security; 

@WebServlet("/processregistration.html") 
public class ProcessRegistration extends HttpServlet { 
private static final long serialVersionUID = 1L; 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException { 
    doPost(request, response); 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException { 
    String userName = request.getParameter("userName"); 
    String passwordInput = request.getParameter("passWord"); 
    String passWord = Security.encrypt(request.getParameter("passWord")); 
    String lastName = request.getParameter("lastName"); 
    String firstName = request.getParameter("firstName"); 
    String middleName = request.getParameter("middleName"); 
    String nationality = request.getParameter("nationality"); 
    String gender = request.getParameter("gender"); 
    String country = request.getParameter("country"); 
    String email = request.getParameter("email"); 

    int statusID = 1; 
    String checkPassword = request.getParameter("cPassword"); 
    Account acct = new Account(userName, passWord, lastName, firstName, middleName,  
nationality, gender, country, email, statusID); 
    RequestDispatcher dispatcher; 

    if (checkPassword.equals(passwordInput)) { 


      if (SQLOperations.addAccount(acct)){ 
      System.out.println("successful insert"); 
      request.setAttribute("account", acct); 
      dispatcher = request.getRequestDispatcher("status.jsp?success=true"); 
      dispatcher.forward(request, response); 
      } 
      else { 
      System.out.println("failed insert"); 
      dispatcher = request.getRequestDispatcher("status.jsp?success=false"); 
      dispatcher.forward(request, response); 
      } 
     } 
     else { 
      System.out.println("Must matched password"); 
      dispatcher = request.getRequestDispatcher("status.jsp?success=false"); 
      dispatcher.forward(request, response); 
     }   
    }   
} 

回答