|
//1、用户登录界面 - login.jsp<BR>
<TABLE width="100%" bgColor=#ffffff>
<TR>
<TD><!- 部分HTML代码此处省略 -- ><BR><form name="teaForm" method="post" action="<%=contextPath%>/login.do"><BR> <table border="0" cellspacing="0" cellpadding="0"><BR> <tr><BR> <td width="60"><font size="2">用户类型</font></td><BR> <td><BR> <input type="radio" value="0" name="actortype" checked><font size="2">系统管理员 </font><BR> <input type="radio" value="1" name="actortype"><font size="2">学生 </font><BR> <input type="radio" value="2" name="actortype"><font size="2">老师 </font><BR> </td><BR> </tr><BR> <tr><BR> <td width="60"><font size="2">用户名</font></td><BR> <td><input name="username" value="" size="12"> *</td><BR> </tr><BR> <tr><BR> <td width="60"><font size="2">口令</font></td><BR> <td><input type="password" name="password" value="" size="8" maxlength="8"> *</td><BR> </tr><BR> </table><BR> <br><BR> <table border="0" cellspacing="0" cellpadding="0"><BR> <tr><BR> <td width="50">&nbsp;</td><BR> <td><input type="submit" value="登 录"></td><BR> </tr><BR> </table><BR></form></TD></TR></TABLE><BR>/*--------------------------------------------------------------------------------------*/<BR>//2、Struts程序 <BR>//2.1 LoginForm.java(存储用户在页面上提交的用户名、口令和用户类型)<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TR>
<TD>package com.chenxc.coursesonline.struts;<BR>/**<BR>* Title: LoginForm<BR>* Description: 存储登录信息<BR>* Time: 2004-3-20<BR>* Company: <BR>* Author: chenxc<BR>* version 1.0<BR>*/<BR>import java.util.*;<BR>public class LoginForm extends org.apache.struts.action.ActionForm{<BR> private String username; //用户名<BR> private String password; //口令<BR> private int actortype; //用户类型<BR> //用户名<BR> public void setUsername(String username) {<BR> this.username = username;<BR> }<BR><BR> public String getUsername() {<BR> return username;<BR> } <BR><BR> //口令<BR> public void setPassword(String password) {<BR> this.password = password;<BR> }<BR><BR> public String getPassword() {<BR> return password;<BR> }<BR> //用户类型<BR> public void setActortype(int actortype)<BR> {<BR> this.actortype = actortype;<BR> }<BR> public int getActortype()<BR> {<BR> return actortype;<BR> }<BR>}</TD></TR></TABLE><BR>//2.2 LoginAction.java(控制登录流程的走向)<BR>
<TABLE width="100%" bgColor=#ffffff>
<TR>
<TD>package com.chenxc.coursesonline.struts;<BR>/**<BR>* Title: LoginAction<BR>* Description: 登录验证<BR>* Time: 2004-3-20<BR>* Company: <BR>* Author: chenxc<BR>* version 1.0<BR>*/<BR>import java.io.*;<BR>import javax.servlet.*;<BR>import javax.servlet.http.*;<BR>import java.util.*;<BR>import org.apache.struts.action.*;<BR>import javax.servlet.http.HttpServletRequest;<BR>import javax.servlet.http.HttpServletResponse;<BR>import javax.servlet.ServletException;<BR>import com.chenxc.coursesonline.struts.*;<BR><BR>public class LoginAction extends org.apache.struts.action.Action{<BR>public ActionForward execute(ActionMapping mapping,ActionForm actionForm,HttpServletRequest request,HttpServletResponse response) throws Exception<BR>{<BR> ActionErrors errors = new ActionErrors();<BR> HttpSession session = request.getSession();<BR> ActionForward forward = null;<BR><BR> LoginForm loginForm = (LoginForm)actionForm;<BR> FacadeBean facadeBean = new FacadeBean();<BR> if(facadeBean.actorlogin(loginForm.getUsername(),loginForm.getPassword(),loginForm.getActortype()))<BR>{<BR> forward = mapping.findForward("success");<BR> return forward;<BR>}<BR> return (new ActionForward(mapping.getInput()));<BR> }<BR>}</TD></TR></TABLE><BR>//2.3 FacadeBean.java(实现登录验证)<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TR>
<TD>package com.chenxc.coursesonline.struts;<BR>/**<BR>* Title: FacadeBean<BR>* Description: 负责与Session Bean沟通<BR>* Time: 2004-3-20<BR>* Company: <BR>* Author: chenxc<BR>* version 1.0<BR>*/<BR>import com.chenxc.coursesonline.ejb20.*;<BR>import javax.naming.*;<BR>import java.util.Properties;<BR>import javax.rmi.PortableRemoteObject;<BR>import javax.ejb.CreateException;<BR>import java.rmi.RemoteException;<BR><BR>public class FacadeBean<BR>extends Object {<BR> 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.";<BR> private static final int MAX_OUTPUT_LINE_LENGTH = 100;<BR> private CoursesFacadeHome coursesFacadeHome = null;<BR> private CoursesFacade coursesFacade = null;<BR><BR> //Construct the FacadeBean<BR> public FacadeBean() {<BR> initialize();<BR> }<BR><BR> public void initialize() {<BR> try {<BR> //get naming context<BR> Context context = getInitialContext();<BR> //look up jndi name<BR> Object ref = context.lookup("CoursesFacade");<BR> //look up jndi name and cast to Home interface<BR> coursesFacadeHome = (CoursesFacadeHome) PortableRemoteObject.narrow(ref,CoursesFacadeHome.class);<BR><BR> }<BR> catch (Exception e) {<BR> e.printStackTrace();<BR> }<BR> }<BR><BR> private Context getInitialContext() throws Exception {<BR> String url = "t3://192.168.100.134:7001";<BR> String user = null;<BR> String password = null;<BR> Properties properties = null;<BR> try {<BR> properties = new Properties();<BR> properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");<BR> properties.put(Context.PROVIDER_URL, url);<BR> if (user != null) {<BR> properties.put(Context.SECURITY_PRINCIPAL, user);<BR> properties.put(Context.SECURITY_CREDENTIALS,<BR> password == null ? "" : password);<BR> }<BR> <BR> return new InitialContext(properties);<BR> }<BR> catch (Exception e) {<BR> throw e;<BR> }<BR> }<BR> //actorlogin<BR> public boolean actorlogin(String username, String password, int actortype) {<BR> try {<BR> coursesFacade = coursesFacadeHome.create();<BR> if (coursesFacade.actorLogin(username, password, actortype)) {<BR> return true;<BR> }<BR> else<BR> {<BR> return false;<BR> }<BR> }<BR> catch (CreateException ex) {<BR> ex.printStackTrace();<BR> }<BR> catch (RemoteException ex) {<BR> ex.printStackTrace();<BR> }<BR> return false;<BR> }<BR>}</TD></TR></TABLE><BR>/*--------------------------------------------------------------------------------------*/<BR>//3、Session Bean程序 <BR>//3.1 SessionFacadeHome.java<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TR>
<TD>package com.chenxc.coursesonline.ejb20;<BR><BR>import javax.ejb.*;<BR>import java.util.*;<BR>import java.rmi.*;<BR><BR>public interface SessionFacadeHome extends javax.ejb.EJBHome {<BR> public SessionFacade create() throws CreateException, RemoteException;<BR>}</TD></TR></TABLE><BR>//3.2 SessionFacade.java<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TR>
<TD>package com.chenxc.coursesonline.ejb20;<BR><BR>import javax.ejb.*;<BR>import java.util.*;<BR>import java.rmi.*;<BR><BR>public interface SessionFacade extends javax.ejb.EJBObject {<BR> public boolean actorLogin(String actor, String password, int actortype) throws RemoteException;<BR>}</TD></TR></TABLE><BR>//3.2 SessionFacadeBean.java<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TR>
<TD>package com.chenxc.coursesonline.ejb20;<BR><BR>import javax.ejb.*;<BR>import javax.naming.*;<BR>import java.rmi.RemoteException;<BR><BR>public class SessionFacadeBean implements SessionBean {<BR> SessionContext sessionContext;<BR><BR> ActorHome actorHome;<BR> Actor actor;<BR><BR> public void ejbCreate() throws CreateException {<BR> try<BR> {<BR> Context ctx = new InitialContext();<BR> actorHome = (ActorHome)ctx.lookup("Actor");<BR> }<BR> catch (Exception ex) {<BR> throw new EJBException(ex);<BR> }<BR> }<BR> public void ejbRemove() {<BR> /**@todo Complete this method*/<BR> }<BR> public void ejbActivate() {<BR> /**@todo Complete this method*/<BR> }<BR> public void ejbPassivate() {<BR> /**@todo Complete this method*/<BR> }<BR> public void setSessionContext(SessionContext sessionContext) {<BR> this.sessionContext = sessionContext;<BR> }<BR> public boolean actorLogin(String username, String password, int actortype) {<BR> try<BR> {<BR> actor = actorHome.findByName(username,actortype);<BR> if(actor.getPassword().equals(password))<BR> {<BR> System.out.println(username + " 登录成功!");<BR> return true;<BR> }<BR> }catch(ObjectNotFoundException ex) {<BR> }<BR> catch(Exception ex) {<BR> ex.printStackTrace();<BR> }<BR> return false;<BR> }<BR>}</TD></TR></TABLE><BR>/*--------------------------------------------------------------------------------------*/<BR>//4、为Entity Bean(Actor)新增一个findByName的Finder,如下图<BR><BR><IMG src="http://dev.yesky.com/image20010518/123437.jpg" align=center border=1><BR><BR> Finder名findByName,返回值Actor,输入参数用户名,用户类型,接口local home,EJB-QL: SELECT OBJECT(a) FROM Actor AS a WHERE a.username = ?1 AND a.actortype = ?2<BR><BR> 6.3.3 阶段总结<BR><BR> Client -> Struts -> Session Bean -> Entity Bean,实现一个登录流程用了十几个java程序,真是累人的说。一个JSP程序就可以实现的功能却要大阵战对待,杀鸡用牛刀乎?<BR><BR> 答案是否定的,因为EJB存在的意义在于分布式计算和分层的思想。<BR><BR> 6.4未完成的<BR><BR> 写到这里,CoursesOnline系统只实现了系统登录验证的功能,主要的业务逻辑基本上没实现,但MVC框架,Facade Design Pattern都或多或少有所描述,继续完成CoursesOnline系统应该不会太困难。<BR><BR> 另外由于使用CMP查询记录集时处理起来比较麻烦,所以在浏览课程或浏览学生等记录集查询时可以考虑用BMP来实现,或者干脆在Session Bean实现。 |
|