找回密码
 入学

QQ登录

只需一步,快速开始

查看: 2503|回复: 3

[转帖]利用JSP+Struts+EJB) 登录流程的代码

[复制链接]
发表于 2005-9-18 20:13:03 | 显示全部楼层 |阅读模式
//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">&amp;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实现。
发表于 2005-10-13 04:59:20 | 显示全部楼层
<>总体不错?</P>
<>如果客户端能加上javascript验证就更好了。</P>
<>不过你能用cmp解决持久层已经是非常厉害了。</P>
<>至于提倡用bmp做记录集查询却有点难?</P>
<>至少要用500行代码才能实现很简单的功能,而且写XML时还易于出错啊..</P>
<>你可以考虑用轻量及如<FONT size=2>HIBERNATE</FONT></P>
回复

使用道具 举报

发表于 2005-10-13 11:14:14 | 显示全部楼层
<>高手!看了!</P>
<>一个词!不懂!</P>
<>嘿嘿!</P>
<>代码很精辟!不过我也才涉及这些东西1</P>
<>收了!自己慢慢弄!</P>
回复

使用道具 举报

发表于 2009-4-10 09:34:06 | 显示全部楼层

我保持沉默,我只是路过!

我保持沉默,我只是路过!
















古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。---魔兽剑圣异界纵横
小游戏 极品家丁 龙蛇演义 恶魔法则 飞升之后 异界枪神 凡人修仙传 魔兽领主 超级农民 成人小游戏 极品公子
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 入学

本版积分规则

QQ|Archiver|手机版|小黑屋|校园天空成立于2004年2月24日 ( 陕ICP备08000078号-8 )

GMT+8, 2025-5-11 13:22 , Processed in 0.105788 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表