mvc的概念,大家都清楚吧,model,view,control
首先我们看看这个目录结构
--+login
----------+web-inf
-----------------------+classes
-beans
-tags
-----------+tlds
login 是主目录放jsp文件,在例子login.jsp,loginfailed.jsp,login_form.jsp,newaccount.jsp,welcome.jsp,accountcreated.jsp
web-inf下面有web.xml配置文件,classes文件夹放类,tlds文件夹放自定义标签
由于我没有用到数据库,所以没有用lib文件夹,是来放置 *.jar 文件的。
classes目录下,有beans,tags文件夹,分别放置user,logindb类,和自定义标签类getrequestparametertag,classes目录下还直接放了loginservlet,newaccountservlet控制器类
我们先看beans下的两个业务对象类
user.java
package beans;
public class user implements java.io.serializable {
private final string username, password, hint;
//final强调此属性初始化后,不能修改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 upwd) {
return getusername().equals(uname) && getpassword().equals(upwd);
}
}
logindb.java
package beans;
import java.util.iterator;
import java.util.vector;
public class logindb implements java.io.serializable {
private vector users = new vector();
//vector类是同步的,所以adduser就不需要同步了
public void adduser(string name, string pwd, string hint) {
users.add(new user(name, pwd, hint));
}
//下面方法判断是否存在正确的user
public user getuser(string name,string pwd) {
iterator it = users.iterator();
user user;
//迭代需要同步
synchronized(users) {
while(it.hasnext()){
user = (user)it.next();
if(user.equals(name,pwd))
return user; //如果返回真,就返回当前user
}
}
return null;
}
public string gethint(string name) {
iterator it = users.iterator();
user user;
synchronized(users) {
while(it.hasnext()){
user = (user)it.next();
if(user.getusername().equals(name))
return user.gethint();
}
}
return null;
}
}
login.jsp
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>login page</title>
</head>
<body>
<%@ include file="login_form.jsp" %>
</body>
</html>
被包含的login_form.jsp
<%@ taglib uri="utilities" prefix="util" %>
<!--调用自定义标签,引用为util,uri的utilities在web.xml映射了-->
<p><font color="#6666cc">请登陆</font></p>
<hr>
<form name="form1" method="post" action="<%=response.encodeurl("login")%>"><!--login是loginsevlet通过在web.xml映射了-->
<table width="68%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td width="33%" align="right">用户名:</td>
<td width="67%">
<input type="text" name="username" value="<util:requestparameter property='username'/>"></td><!--注意这里用了自定义标签,如果有值就显示-->
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="text" name="userpwd" ></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="submit" value="登陆">
</td>
</tr>
</table>
</form>
loginservlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.ioexception;
import beans.user;
import beans.logindb;
public class loginservlet extends httpservlet {
private logindb logindb;
public void init(servletconfig config) throws servletexception {
super.init(config);
logindb = new logindb();
config.getservletcontext().setattribute("logindb",logindb);
}
public void doget(httpservletrequest request, httpservletresponse response) throws servletexception,ioexception{
string name = request.getparameter("username"); //从login_form 表单得到值
string pwd = request.getparameter("userpwd");
user user = logindb.getuser(name,pwd);
if(user != null){ //说明存在用户
request.getsession().setattribute("user",user); //放到session 里面
request.getrequestdispatcher(response.encodeurl("/welcome.jsp")).forward(request,response); //成功转发到welcome.jsp
}else{
request.getrequestdispatcher(response.encodeurl("/loginfailed.jsp")).forward(request, response);
}
}
public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception,ioexception{
doget(request,response);
}
}
web.xml添加
<servlet>
<servlet-name>login</servlet-name> <!--名字-->
<servlet-class>loginservlet</servlet-class> <!--指定类-->
</servlet><servlet>
<servlet-name>new_account</servlet-name>
<servlet-class>newaccountservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>new_account</servlet-name>
<url-pattern>/new_account</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name> <!--和上面的名字一致-->
<url-pattern>/login</url-pattern> <!--映射路径-->
</servlet-mapping>
<taglib>
<taglib-uri>utilities</taglib-uri>
<taglib-location>/web-inf/tlds/utilities.tld</taglib-location>
<!--自定义标签的实际位置-->
</taglib>
utilities.tld关键部分
<taglib>
<tag>
<name>requestparameter</name>
<tagclass>tags.getrequestparametertag</tagclass>
<!--类的位置,如果有包写上-->
<info>simplest example: inserts one line of output</info>
<bodycontent>empty</bodycontent>
<attribute>
<name>property</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
自定义标签类getrequestparametertag.java
package tags;
import javax.servlet.servletrequest;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.tagext.tagsupport;
public class getrequestparametertag extends tagsupport {
private string property;
public void setproperty(string property){
this.property = property;
}
public int dostarttag() throws jspexception {
servletrequest reg = pagecontext.getrequest();
string value = reg.getparameter(property);
try{
pagecontext.getout().print(value == null ? "":va
闽公网安备 35060202000074号