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

📄 loginaction.java

📁 系统特色: 1,栏目无限级分类,形成树型结构
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.hao2007.struts.action;

import java.sql.ResultSet;

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.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.hao2007.struts.form.LoginForm;
import com.hao2007.struts.db.DataStore;
import com.hao2007.struts.bean.MD5;
import java.io.PrintWriter;

/**
 * MyEclipse Struts Creation date: 03-14-2007
 * 
 * XDoclet definition:
 * 
 * @struts.action path="/login" name="loginForm" input="/admin/login.jsp"
 *                scope="request" validate="true"
 * @struts.action-forward name="success" path="/success/index.jsp"
 *                        redirect="true"
 * @struts.action-forward name="fail" path="/admin/fail.jsp" redirect="true"
 */
public class LoginAction extends Action {
	/*
	 * Generated Methods
	 */

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method
		// stub

		HttpSession session = request.getSession(true);
		ActionErrors errors = new ActionErrors();
		ActionMessages messages = new ActionMessages();
		MD5 md5 = new MD5();

		String admin_name = (String) loginForm.getAdmin();
		String admin_password = (String) loginForm.getPassword();
		admin_password = new String(admin_password.getBytes("UTF-8"), "GBK");
		String validateCode = (String) loginForm.getValidateCode();

		admin_password = md5.getMD5ofStr(admin_password);

		try {
			String rand = (String) session.getAttribute("rand");
			if (!validateCode.equals(rand)) {
				errors.add("validateCode.error", new ActionMessage(
						"admin.login.validateCode.action.errors"));
				saveErrors(request, errors); // ��<html:errors
				// property="validateCode.error"/>
				return (mapping.findForward("fail"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		DataStore db = DataStore.getInstance();
		if (db == null) {
			errors.add("db.error", new ActionMessage("db.connection.error"));
			saveErrors(request, errors); // <html:errors
			// property="db.error"/>
			return (mapping.findForward("fail"));
		}

		ResultSet st = null;
		String sql = "select * from admin where admin_name='" + admin_name
				+ "' and admin_password='" + admin_password + "'";

		try {
			st = db.read(sql);

			if (st != null && st.next()) {

				// session

				session.setAttribute("current_user", admin_name);
				session.setAttribute("current_password", admin_password);
				
				return (mapping.findForward("success"));
			} else {

				messages.add("login.error", new ActionMessage(
						"admin.login.error"));
				saveMessages(request, messages);
				/*
				 * ��<html:messages id="messages" message="true"> <bean:write
				 * name="messages"/> </html:messages>
				 */
				return (mapping.findForward("fail"));

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

		return null;

	}
}

⌨️ 快捷键说明

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