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

📄 editaccountaction.java

📁 如题ServletJSP.rar 为网络收集的JSP网站源文件
💻 JAVA
字号:
/*
 * XP Forum
 *
 * Copyright (c) 2002-2003 RedSoft Group.  All rights reserved.
 *
 */
package org.redsoft.forum.web;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.security.auth.Subject;

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.action.ActionServlet;
import org.apache.struts.util.MessageResources;
import org.redsoft.forum.web.Account;
import org.redsoft.forum.dao.AccountDAO;
import org.redsoft.forum.dao.DAOFactory;
import org.redsoft.forum.exception.AccountNotFoundException;
import org.redsoft.forum.ForumConstants;
import org.redsoft.forum.security.User;

/**
 * Edit a user account
 *
 * @author Eric Lao
 * @version 1, April 17,2002
 */
public final class EditAccountAction extends Action {


   /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param actionForm The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
    public ActionForward perform(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws IOException, ServletException {

        final HttpSession session = request.getSession();
        final ActionErrors errors = new ActionErrors();
        final String oldpassword = ( ( EditAccountForm) form ).getOldpassword();
	    final String password = ( ( EditAccountForm) form ).getPassword();
        final String email = ( ( EditAccountForm) form ).getEmail();

        // Get the current user from session
	    final Subject subject = (Subject)session.getAttribute(ForumConstants.USER_KEY);
        final String userName = ( (User)subject.getPrincipals( User.class ).iterator().next() ).getName();
/*
        // If the user is null which means the user has not loged in,forward it to logon screen
        if( userName == null || userName.length() == 0 ){
			final String url = "/editAccount.go";
			request.setAttribute( ForumConstants.DEST_URL, url );
			return (mapping.findForward("logon"));
        }
*/
        try{
            final AccountDAO dao = DAOFactory.getInstance().getAccountDAO();
			final Account account = dao.findByUserName( userName );

            // Verify if the input old password is the same as that in the DB
            if( account.getPassword().equals( oldpassword ) ){
            	dao.updateAccount( new Account( userName,password,email ) );
			}else{
				errors.add("Edit Account", new ActionError("error.account.password") );
			}
        }catch( final SQLException sqlException ){
            sqlException.printStackTrace();
            return (mapping.findForward("error"));
        }catch( final AccountNotFoundException accountAlreadyExistException ){
			accountAlreadyExistException.printStackTrace();
            return (mapping.findForward("error"));
		}

        // Report any errors we have discovered back to the original form
    	if (!errors.empty()) {
	        saveErrors(request, errors);
	        return (new ActionForward(mapping.getInput()));
	    }

        // 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"));
    }
}//EOC

⌨️ 快捷键说明

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