[b:d01b749036]测试页面testmvc.jsp[/b:d01b749036]示例:
/testmvc.jsp
<%@pagecon_tentType="text/Html;charset=gb2312"%
<html
<head
<title无标题文档</title
<metahttp-equiv="Content-Type"con_tent="text/html;charset=gb2312"
</head
<bodybgcolor="#FFFFFF"text="#000000"
PleasaLogin
<hrwidth="98%"
<!--formname="form1"method="post"action="<%=response.encodeURL("login")%"--
<formname="form1"method="post"action="<%=response.encodeURL("login-action.do")%"
<tablewidth="80%"border="0"cellspacing="0"cellpadding="0"
<tr
<td用户名:
<inputtype="text"name="userName"
</td
</tr
<tr
<td密码:
<inputtype="passWord"name="PassWord"size="12"
</td
</tr
<tr
<tdheight="37"
<inputtype="button"name="Submit"on_click="test()"value="登陆"
</td
</tr
</table
</form
</body
</html
<scriptlanguage="javascript"
functiontest()
{
alert("ok");
document.form1.submit();
}
</script
[b:d01b749036]测试页面:welcome.jsp[/b:d01b749036]
示例:/welcome.jsp
youarewelcome
说明:这里我偷了个懒,该页面只写这句话。
[b:d01b749036]BEAN:USER类[/b:d01b749036]
示例:/WEB-INF/Classes/beans/User.Java
packagebean;
publicclassUserimplementsjava.io.Serializable
{
privatefinalStringuserName,password,hint;
publicUser(StringuserName,Stringpassword,Stringhint)
{
this.userName=userName;
this.password=password;
this.hint=hint;
}
publicStringgetUserName()
{
returnuserName;
}
publicStringgetPassWord()
{
returnpassword;
}
publicStringgetHint()
{
returnhint;
}
publicbooleanequals(Stringuname,Stringpwd)
{
returngetUserName().equals(uname)&&getPassWord().equals(pwd);
}
}
该类表示了一个用户,并提供了一个equals的方法,当用户名和口令匹配的时候,返回true值。
[b:d01b749036]BEAN:LoginDB类[/b:d01b749036]
示例:/WEB-INF/Classes/beans/User.java
packagebean;
importjava.util.Iterator;
importjava.util.Vector;
importjava.io.*;
publicclassLoginDBimplementsSerializable
{
privateVectorusers=newVector();
publicvoidaddUser(Stringuname,Stringpwd,Stringhint)//添加用户的方法
{
users.add(newUser(uname,pwd,hint));
}
publicUsergetUser(Stringuname,Stringpwd)//检索用户的方法
{
Iteratorit=users.iterator();
Userbean=null;
synchronized(users){
while(it.hasNext())
{
bean=(User)it.next();
if(bean.equals(uname,pwd))
returnbean;
}
}
returnnull;
}
publicStringgetHint(Stringuname)//对指定的用户提供返回口令提示的方法
{
Iteratorit=users.iterator();
Userbean=null;
synchronized(users)
{
while(it.hasNext())
{
if(bean.getUserName().equals(uname))
returnbean.getHint();
}
}
returnnull;
}
}
[b:d01b749036]LoginServlet类:[/b:d01b749036]
示例:/WEB-INF/Classes/LoginServlet.java
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.jsp.*;
importbean.*;
publicclassLoginServletextendsHttpServlet
{
privateLoginDBloginDB;
publicvoidinit(ServletConfigconfig)throwsServletException
{
loginDB=newLoginDB();
}
publicvoidservice(HttpServletRequestreq,HttpServletResponseres)
throwsjava.io.IOException,ServletException
{
loginDB.addUser("long","long","long");
Useruser=loginDB.getUser(req.getParameter("userName"),req.getParameter("PassWord"));
System.out.println("ThenameofloginDBis"+loginDB.getClass().getName());
//Stringuser=req.getParameter("userName");
//System.out.println("getusername:"+user);
/*getServletContext().getRequestDispatcher(res.encodeURL("/index.jsp")).forward(req,res);*/
/*要注重getServletContext()和req两个对象的区别,经过实验应用getServletContext()进行重定向*/
/*总是不行,而应用req则可以*/
if(user!=null)
{
req.getRequestDispatcher(res.encodeURL("/welcome.jsp")).forward(req,res);
}
else
{
req.getRequestDispatcher(res.encodeURL("/adduser.jsp")).forward(req,res);
}
}
}
当testmvc.jsp的表单提交时,请求被发送到登录的Servlet,这段代码我没什么好说的,在使用mvc构架之前,我们喜欢用隐藏帧来处理表单提交的内容,实际LoginServlet.java就是替代了隐藏帧而已。
到此为止,请读者将所有的示例,按示例所示的路径存储好所有的类和jsp页面。下一步我们将讨论这个东东的玩法和原理。
对了,忘了告诉大家,我的测试环境是:
win2000servertomcat4.1jdk1.4没有数据库