Some Important Actions

xiaoxiao2021-03-06  100

Loginaction.java

Package vincent.Actions;

Import java.io.unsupportedEncodingexception; import java.util.locale; import java.util.resourceBundle;

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.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;

Public class loginaction extends action {

public ActionForward perform = false (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {// can logon boolean isValidate; // get the LoginForm attribute String userName = ((LoginForm) form) .getUsername ();? String passWord = (Loginform) .Getpassword (); // validate if (username.equals ("admin") && password.equals ("admin")) {isvalidate = true;} else} (username.equals ("Customer" ) && passWord.equals ( "Customer")) {isValidate = true;} // add to ActionErrors if (isValidate == false) {ActionErrors errors = new ActionErrors (); errors.add (ActionErrors.GLOBAL_MESSAGE, new ActionError ( " Error.login.failed "); this.saveError (Request, Errors); Return mapping.findforward (" logon ");} // get the session httpsession session = request.getSession (); session.settribute (" Username " Username; session.setttribute ("password", password); session.setttribute ("loginform", (loginform) Form); // put it in the session cause the JSP May use it return (mapping.findf ORWARD ("View"));

}

Loginform.javaPackage Vincent.Actions;

Import javax.servlet.http.httpservletRequest;

Import org.Apache.struts.Action.Actionerror; import org.apache.struts.Action.Apache.struts.Act organizationmapping; import org.apache.struts.validator.validatorForm;

Public class loginform extends Validatorform {// Declare the Fields of the form here = null; private string password = null;

public void setUsername (String username) {this.username = username;} public void setPassword (String password) {this.password = password;} public String getUsername () {return (this.username);} public String getPassword () { return (this.password);} / ** * This is the validation * It must not be omitted.It MUST not be implemented cause the ValidationPlugin will do that * / public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {return null. }}

ViewAction.java

Package vincent.Actions;

Import java.io.ioException; import java.rmi.remoteException; import java.util.Ashtable; import java.util.ITERATOR;

import javax.ejb.CreateException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; 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 .act.dynaactionform;

Import vincent.cmp.productlocal; import vincent.slsbean. *; import vincent.util.pagecontrol;

public final class ViewAction extends Action {// cache the accessManagerHome here AccessManagerHome accessManagerHome = null; ArrayList productList = null; ArrayList array = new ArrayList (); / ** * Initializing the accessManagerHome * * / public void initAccessManager () {System.out .println ( "Loading AccessManager in the servlet"); try {accessManagerHome = getHome ();} catch (NamingException e) {// Have to add something later e.printStackTrace ();}} / ** * Get the AccessManagerHome * @return * @throws NamingException * / public AccessManagerHome getHome () throws NamingException {Object result = getContext () lookup (AccessManagerHome.JNDI_NAME);. return (AccessManagerHome) PortableRemoteObject.narrow (result, AccessManagerHome.class);} / ** * Get the Context for JNDI * @return * @throws NamingException * / public InitialContext getContext () throws NamingException {Hashtable props = new Hashtable (); props.put (InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfac Es.namingContextFactory "); PrOPS.PUT (InitialContext.Provider_URL," JNP: //127.0.0.1: 1099 ");

// this Establishes the security for authorization / automation // props.put (InitialContext.security_principal, "username"); // props.put (InitialContext.security_credentials, "password");

InitialContext initialContext = new InitialContext (props); return initialContext;} public void setArrayList (ArrayList arrayList, PageControl pageControl) {int startRow = pageControl.getPageStartRow (); int endRow = pageControl.getPageEndRow (); Iterator iterator = arrayList.iterator () For (int i = 1; i

// Validate Here / * if (username == null || Password == null) {return ("logon"));} * / / * else if (! Username.equals ("admin") | |! Username.equals ("Customer")) {Return ("Logon"));} * / // get the accessmanagerHome Initialized InitaccessManager (); // reset Your array first array = new arraylist (); try {accessManagerHome.create () calculateDiscount ((float) 0.75);.} catch (RemoteException e) {// TODO Auto-generated catch block e.printStackTrace ();} catch (CreateException e) {// TODO Auto-generated catch block e.printStackTrace ();.} try {productList = accessManagerHome.create () getAllRecords ();} catch (RemoteException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (CreateException e1) {// Todo auto-generated catch block e1.printStackTrace ();} // get the page information from request string pageinfo = request.getParameter ("pageInfo"); // if null create the New PageControl if (pageInfo == null) {pageControl = new PageControl (productList.size ()); session.setAttribute ( "pageControl", pageControl);} else if (pageInfo.equals ( "next")) {pageControl.setCurrentPage ( pageControl.getNextPage ());} else if (pageInfo.equals ( "previous")) {pageControl.setCurrentPage (pageControl.getPreviousPage ());} // transfer the productlist to the array // in array the element in "ProductForm "SetArrayList (ProductList, PageControl); // add the pageform info pageform (); string hasnext = null; if (pageControl.hasnext) {Hasnext =" YES ";

转载请注明原文地址:https://www.9cbs.com/read-123649.html

New Post(0)