注册 | 登录 | 设为首页 | 加入收藏
您当前的位置:飞翔学院-IT中国 → 编程开发JSP → 文章内容

JSP用MVC架构注册登录的例子

作者:佚名 来源:不详 发布时间:2007-11-30 0:08:22

  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>

上一页  [1] [2] [3] [4]  下一页


  • 打印文档
  • 推荐好友
  • 返回顶部
  • 增大字体
  • 减少字体
关于本站 | 工作机会 | 合作网站 | 广告服务 | 市场合作| 联系我们 | 抽奖活动
版权所有: 武汉威俊科技有限公司 Copyright 2005-2007 www.ITCNW.COM All rights reserved