logonAction.java~6~
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- package strutsds;
- import java.io.IOException;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionErrors;
- import org.apache.struts.action.ActionError;
- import javax.sql.DataSource;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class logonAction extends Action {
- java.util.ArrayList list=new java.util.ArrayList();
- protected String getUser(String username, String password) {
- String user = null;
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- ServletContext context = servlet.getServletContext();
- DataSource dataSource = (DataSource)context.getAttribute(Action.DATA_SOURCE_KEY);
- try {
- conn = dataSource.getConnection();
- stmt = conn.createStatement();
- rs =stmt.executeQuery("select * from employees where "+ "username='" + username + "' "+ "and password='" + password + "'");
- if ( rs.next() ) {
- user = rs.getString("username");
- // Iterate over the results
- System.err.println("Username : "+ rs.getString("username")+ " Password : " + rs.getString("password"));
- rs.get
- }
- else {
- System.err.println("....>User not found<....");
- }
- }
- catch (SQLException e) {
- System.err.println(e.getMessage());
- }
- finally {
- if (rs != null) {
- try {
- rs.close();
- }
- catch (SQLException sqle) {
- System.err.println(sqle.getMessage());
- }
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- }
- catch (SQLException sqle) {
- System.err.println(sqle.getMessage());
- }
- stmt = null;
- }
- if (conn != null) {
- try {
- conn.close();
- }
- catch (SQLException sqle) {
- System.err.println(sqle.getMessage());
- }
- conn = null;
- }
- }
- return user;
- }
- public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpServletResponse) {
- String user = null;
- // Default target to success
- String target = "success";
- // Use the LoginForm to get the request parameters
- String username = ((logonActionForm)form).getUsername();
- String password = ((logonActionForm)form).getPassword();
- user = getUser(username, password);
- // Set the target to failure
- if ( user == null ) {
- target = "login";
- ActionErrors errors = new ActionErrors();
- errors.add(ActionErrors.GLOBAL_ERROR,
- new ActionError("errors.login.unknown",username));
- // Report any errors we have discovered back to the
- // original form
- if (!errors.empty()) {
- saveErrors(request, errors);
- }
- }
- else {
- HttpSession session = request.getSession();
- session.setAttribute("USER", user);
- }
- // Forward to the appropriate View
- return (mapping.findForward(target));
- }
- }