[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 没有数据库
闽公网安备 35060202000074号