1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| package org.apache.struts.webapp.example;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import javax.sql.RowSet;
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;
public final class LogonAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Locale locale = getLocale(request);
DataSource dataSource = this.getDataSource(request, "webserver");
if (dataSource == null) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.database.missing"));
saveErrors(request, errors);
return (mapping.getInputForward());
}
MessageResources messages = getResources(request);
Connection connection = null;
Statement st = null;
ResultSet rs = null;
String query = null;
RowSet rowset = null;
int ResultCount = 0;
String username = (String)PropertyUtils.getSimpleProperty(form, "username");
String password = (String)PropertyUtils.getSimpleProperty(form, "password");
try {
connection = dataSource.getConnection();
query = "select count(*) as rowcount from tb_users where login='"+username+"' and password='"+password+"'";
st = connection.createStatement();
rs = st.executeQuery(query);
rs.next();
ResultCount = rs.getInt("rowcount") ;
rs.close();
st.close();
} catch (Exception ex) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.database.connection"));
this.saveErrors(request, errors);
return (mapping.getInputForward());
} finally {
try {
connection.close();
} catch (Exception ignored) {
}
}
if(ResultCount == 0) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.password.mismatch"));
this.saveErrors(request, errors);
return (mapping.getInputForward());
}
else
{
HttpSession session = request.getSession();
session.setAttribute(Constants.USER_KEY, username);
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
return (mapping.findForward("success"));
}
}
} |
Partager