6.3.2 登录流程的代码
/*--------------------------------------------------------------------------------------*/ //1、用户登录界面 - login.jsp
<!- 部分HTML代码此处省略 -- > <form name="teaForm" method="post" action="<%=contextPath%>/login.do"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td width="60"><font size="2">用户类型</font></td> <td> <input type="radio" value="0" name="actortype" checked><font size="2">系统管理员 </font> <input type="radio" value="1" name="actortype"><font size="2">学生 </font> <input type="radio" value="2" name="actortype"><font size="2">老师 </font> </td> </tr> <tr> <td width="60"><font size="2">用户名</font></td> <td><input name="username" value="" size="12"> *</td> </tr> <tr> <td width="60"><font size="2">口令</font></td> <td><input type="password" name="password" value="" size="8" maxlength="8"> *</td> </tr> </table> <br> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td width="50"> </td> <td><input type="submit" value="登 录"></td> </tr> </table> </form> | /*--------------------------------------------------------------------------------------*/ //2、Struts程序 //2.1 LoginForm.java(存储用户在页面上提交的用户名、口令和用户类型)
package com.chenxc.coursesonline.struts; /** * Title: LoginForm * Description: 存储登录信息 * Time: 2004-3-20 * Company: * Author: chenxc * version 1.0 */ import java.util.*; public class LoginForm extends org.apache.struts.action.ActionForm{ private String username; //用户名 private String password; //口令 private int actortype; //用户类型 //用户名 public void setUsername(String username) { this.username = username; }
public String getUsername() { return username; }
//口令 public void setPassword(String password) { this.password = password; }
public String getPassword() { return password; } //用户类型 public void setActortype(int actortype) { this.actortype = actortype; } public int getActortype() { return actortype; } } | //2.2 LoginAction.java(控制登录流程的走向)
package com.chenxc.coursesonline.struts; /** * Title: LoginAction * Description: 登录验证 * Time: 2004-3-20 * Company: * Author: chenxc * version 1.0 */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import org.apache.struts.action.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import com.chenxc.coursesonline.struts.*;
public class LoginAction extends org.apache.struts.action.Action{ public ActionForward execute(ActionMapping mapping,ActionForm actionForm,HttpServletRequest request,HttpServletResponse response) throws Exception { ActionErrors errors = new ActionErrors(); HttpSession session = request.getSession(); ActionForward forward = null;
LoginForm loginForm = (LoginForm)actionForm; FacadeBean facadeBean = new FacadeBean(); if(facadeBean.actorlogin(loginForm.getUsername(),loginForm.getPassword(),loginForm.getActortype())) { forward = mapping.findForward("success"); return forward; } return (new ActionForward(mapping.getInput())); } } | //2.3 FacadeBean.java(实现登录验证)
package com.chenxc.coursesonline.struts; /** * Title: FacadeBean * Description: 负责与Session Bean沟通 * Time: 2004-3-20 * Company: * Author: chenxc * version 1.0 */ import com.chenxc.coursesonline.ejb20.*; import javax.naming.*; import java.util.Properties; import javax.rmi.PortableRemoteObject; import javax.ejb.CreateException; import java.rmi.RemoteException;
public class FacadeBean extends Object { private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first."; private static final int MAX_OUTPUT_LINE_LENGTH = 100; private CoursesFacadeHome coursesFacadeHome = null; private CoursesFacade coursesFacade = null;
//Construct the FacadeBean public FacadeBean() { initialize(); }
public void initialize() { try { //get naming context Context context = getInitialContext(); //look up jndi name Object ref = context.lookup("CoursesFacade"); //look up jndi name and cast to Home interface coursesFacadeHome = (CoursesFacadeHome) PortableRemoteObject.narrow(ref,CoursesFacadeHome.class);
} catch (Exception e) { e.printStackTrace(); } }
private Context getInitialContext() throws Exception { String url = "t3://192.168.100.134:7001"; String user = null; String password = null; Properties properties = null; try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); } return new InitialContext(properties); } catch (Exception e) { throw e; } } //actorlogin public boolean actorlogin(String username, String password, int actortype) { try { coursesFacade = coursesFacadeHome.create(); if (coursesFacade.actorLogin(username, password, actortype)) { return true; } else { return false; } } catch (CreateException ex) { ex.printStackTrace(); } catch (RemoteException ex) { ex.printStackTrace(); } return false; } } | /*--------------------------------------------------------------------------------------*/ //3、Session Bean程序 //3.1 SessionFacadeHome.java
package com.chenxc.coursesonline.ejb20;
import javax.ejb.*; import java.util.*; import java.rmi.*;
public interface SessionFacadeHome extends javax.ejb.EJBHome { public SessionFacade create() throws CreateException, RemoteException; } | //3.2 SessionFacade.java
package com.chenxc.coursesonline.ejb20;
import javax.ejb.*; import java.util.*; import java.rmi.*;
public interface SessionFacade extends javax.ejb.EJBObject { public boolean actorLogin(String actor, String password, int actortype) throws RemoteException; } | //3.2 SessionFacadeBean.java
package com.chenxc.coursesonline.ejb20;
import javax.ejb.*; import javax.naming.*; import java.rmi.RemoteException;
public class SessionFacadeBean implements SessionBean { SessionContext sessionContext;
ActorHome actorHome; Actor actor;
public void ejbCreate() throws CreateException { try { Context ctx = new InitialContext(); actorHome = (ActorHome)ctx.lookup("Actor"); } catch (Exception ex) { throw new EJBException(ex); } } public void ejbRemove() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } public boolean actorLogin(String username, String password, int actortype) { try { actor = actorHome.findByName(username,actortype); if(actor.getPassword().equals(password)) { System.out.println(username + " 登录成功!"); return true; } }catch(ObjectNotFoundException ex) { } catch(Exception ex) { ex.printStackTrace(); } return false; } } | /*--------------------------------------------------------------------------------------*/ //4、为Entity Bean(Actor)新增一个findByName的Finder,如下图

Finder名findByName,返回值Actor,输入参数用户名,用户类型,接口local home,EJB-QL: SELECT OBJECT(a) FROM Actor AS a WHERE a.username = ?1 AND a.actortype = ?2
6.3.3 阶段总结
Client -> Struts -> Session Bean -> Entity Bean,实现一个登录流程用了十几个java程序,真是累人的说。一个JSP程序就可以实现的功能却要大阵战对待,杀鸡用牛刀乎?
答案是否定的,因为EJB存在的意义在于分布式计算和分层的思想。
6.4未完成的
写到这里,CoursesOnline系统只实现了系统登录验证的功能,主要的业务逻辑基本上没实现,但MVC框架,Facade Design Pattern都或多或少有所描述,继续完成CoursesOnline系统应该不会太困难。
另外由于使用CMP查询记录集时处理起来比较麻烦,所以在浏览课程或浏览学生等记录集查询时可以考虑用BMP来实现,或者干脆在Session Bean实现。
|
|