[b:d01b749036]
测试页面testmvc.jsp[/b:d01b749036]示例:
/testmvc.jsp
<%@
page
con_tentType="text/html;charset=gb2312"
%
<html
<head
<title无标题文档</title
<meta
http-equiv="Content-Type"
con_tent="text/html;
charset=gb2312"
</head
<body
bgcolor="#FFFFFF"
text="#000000"
Pleasa
Login
<hr
width="98%"
<!--form
name="form1"
method="post"
action="<%=response.encodeURL("login")%"--
<form
name="form1"
method="post"
action="<%=response.encodeURL("login-action.do")%"
<table
width="80%"
border="0"
cellspacing="0"
cellpadding="0"
<tr
<td
用户名:
<input
type="text"
name="userName"
</td
</tr
<tr
<td
密
码:
<input
type="password"
name="PassWord"
size="12"
</td
</tr
<tr
<td
height="37"
<input
type="button"
name="Submit"
on_click="test()"
value="登
陆"
</td
</tr
</table
</form
</body
</html
<script
language="javascript"
function
test()
{
alert("ok");
document.form1.submit();
}
</script
[b:d01b749036]测试页面:welcome.jsp[/b:d01b749036]
示例:
/welcome.jsp
you
are
welcome
说明:这里我偷了个懒,该页面只写这句话。
[b:d01b749036]BEAN:USER类[/b:d01b749036]
示例:
/WEB-INF/Classes/beans/User.java
package
bean;
public
class
User
implements
java.io.Serializable
{
private
final
String
userName,password,hint;
public
User(String
userName,String
password,String
hint)
{
this.userName=userName;
this.password=password;
this.hint=hint;
}
public
String
getUserName()
{
return
userName;
}
public
String
getPassWord()
{
return
password;
}
public
String
getHint()
{
return
hint;
}
public
boolean
equals(String
uname,String
pwd)
{
return
getUserName().equals(uname)&&
getPassWord().equals(pwd);
}
}
该类表示了一个用户,并提供了一个equals的方法,当用户名和口令匹配的时候,返回true值。
[b:d01b749036]BEAN:LoginDB类[/b:d01b749036]
示例:
/WEB-INF/Classes/beans/User.java
package
bean;
import
java.util.Iterator;
import
java.util.Vector;
import
java.io.*;
public
class
LoginDB
implements
Serializable
{
private
Vector
users=new
Vector();
public
void
addUser(String
uname,String
pwd,String
hint)//添加用户的方法
{
users.add(new
User(uname,pwd,hint));
}
public
User
getUser(String
uname,String
pwd)//检索用户的方法
{
Iterator
it=users.iterator();
User
bean=null;
synchronized
(users){
while(it.hasNext())
{
bean=(User)it.next();
if
(bean.equals(uname,pwd))
return
bean;
}
}
return
null;
}
public
String
getHint(String
uname)//对指定的用户提供返回口令提示的方法
{
Iterator
it=users.iterator();
User
bean=null;
synchronized
(users)
{
while(it.hasNext())
{
if
(bean.getUserName().equals(uname))
return
bean.getHint();
}
}
return
null;
}
}
[b:d01b749036]LoginServlet类:[/b:d01b749036]
示例:
/WEB-INF/Classes/LoginServlet.java
import
javax.servlet.ServletConfig;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.jsp.*;
import
bean.*;
public
class
LoginServlet
extends
HttpServlet
{
private
LoginDB
loginDB;
public
void
init(ServletConfig
config)
throws
ServletException
{
loginDB=new
LoginDB();
}
public
void
service(HttpServletRequest
req,
HttpServletResponse
res)
throws
java.io.IOException,
ServletException
{
loginDB.addUser("long","long","long");
User
user=loginDB.getUser(req.getParameter("userName"),req.getParameter("PassWord"));
System.out.println("The
name
of
loginDB
is"+loginDB.getClass().getName());
//String
user=req.getParameter("userName");
//System.out.println("get
user
name:"+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页面。下一步我们将讨论这个东东的玩法和原理。
对了,忘了告诉大家,我的测试环境是:
win2000server
tomcat
4.1
jdk1.4
没有数据库