⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logonaction.java

📁 java struts source code
💻 JAVA
字号:
package emptyprj;

import javawebstudio.struts_db.ConnectionPool;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.ModuleException;
import org.apache.struts.util.MessageResources;
import org.apache.commons.beanutils.PropertyUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Collection;

public final class logonAction extends Action {
    private Log log =LogFactory.getLog("org.apache.struts.webapp.Example");
    private ConnectionPool pool;   
    public logonAction() { pool = ConnectionPool.getInstance(); }
    
    public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)	throws Exception 
	{
	    // Validate the request parameters specified by the user
	    ActionErrors errors = new ActionErrors();
	    String username = (String)PropertyUtils.getSimpleProperty(form, "username");
        String password = (String)PropertyUtils.getSimpleProperty(form, "password");            
	
       String getusername=CheckUser(username,password);
       if ("".equals(getusername))
	    {
               errors.add(ActionErrors.GLOBAL_ERROR,
                          new ActionError("error.password.mismatch"));
	    }
        getusername=username+getusername;
	    // Report any errors we have discovered back to the original form
	    if (!errors.isEmpty()) {
	        saveErrors(request, errors);
            return (mapping.getInputForward());
	    }
	    // Save our logged-in user in the session
	    HttpSession session = request.getSession();
	    session.setAttribute(Constants.USER_KEY, getusername);
	    session.setAttribute(Constants.USER_NAME, username);
	
        if (log.isDebugEnabled()) {
            log.debug("LogonAction: User '" + username +
                      "' logged on in session " + session.getId());
         }
         // Remove the obsolete form bean
	    if (mapping.getAttribute() != null) {
            if ("request".equals(mapping.getScope()))
                request.removeAttribute(mapping.getAttribute());
            else
                session.removeAttribute(mapping.getAttribute());
        }
   
	    // Forward control to the specified success URI
	    return (mapping.findForward("success"));
    }

    //检测数据库中是否存在对应的用户名和密码,如果有则读取该用户的角色。
    public String CheckUser(String username,String password)
    {
	     Connection con = null;
	     PreparedStatement ps = null;
         ResultSet rs = null;	
	     try 
         {
	          con = pool.getConnection();
      	      String sql = "SELECT * from users  WHERE username = ? AND password= ?";
      	   
      	      if (con.isClosed()) {
                   throw new IllegalStateException("error.con.isClosed");
                }
               ps = con.prepareStatement(sql);
               ps.setString(1,username);
               ps.setString(2,MD5.getMD5(password));
               rs = ps.executeQuery();
  
               String returnstr="";
               while(rs.next())
               { 
                    returnstr+=";"+rs.getString("role");
               }
               return returnstr;    
    	    } 
	        catch (SQLException e) 
            {
      		    e.printStackTrace();
      		    throw new RuntimeException("Unable to get connection.");
    	    } 
            finally
            {
     	         try 
                 { 
                     if (rs != null)rs.close();
                     if (ps != null)ps.close();
        	         if (con != null)con.close();
      	         } 
                 catch (SQLException e) 
                 {
        	          throw new RuntimeException(e.getMessage());
      	         }
             } 
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -