?? logonaction.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"));
}
//檢測數(shù)據(jù)庫中是否存在對應(yīng)的用戶名和密碼,如果有則讀取該用戶的角色。
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());
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -