码迷,mamicode.com
首页 > 其他好文 > 详细

Servle--转发实例学习笔记

时间:2018-04-17 20:52:01      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:Servle   转发   实例   学习笔记   

页面显示:
技术分享图片

失败:
技术分享图片

成功
技术分享图片

视图层(view)

login.html
<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登陆</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
   <form action=‘/day04/LoginServlet‘ method=‘post‘>
       <table border="1" align="center">
           <caption>用户登陆【mvc】</caption>
           <tr>
               <th>用户</th>
               <td><input type="text" name="username"></td>
           </tr>
           <tr>
           <td colspan="2" align="center">
              <input type="submit" value="提交">

           </td>
           </tr>
       </table>

   </form>  
  </body>
</html>

fail.hmtl
<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登陆</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
   登陆失败
  </body>
</html>

cuccess.html
<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登陆</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
   登陆成功  
  </body>
</html>

控制层(controller)

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 取得提交的参数
        String username = request.getParameter("username");

        // 调用模型层对象
        LoginBean lgin = new LoginBean();
        boolean flag = lgin.validate(username);

        // 根据还回值转发到不同页面;
        if (flag) {
            // 也可以使用这个
            // this.getServletContext().getRequestDispatcher("/success.html").forward(request, response);
            //获取提交值
            ServletContext context = this.getServletContext();
            // 定位需要转发的路径
            RequestDispatcher rd = context
                    .getRequestDispatcher("/success.html");
            // 真正转向页面
            rd.forward(request, response);
        } else {

            //this.getServletContext().getRequestDispatcher("/fail.html").forward(request, response);
            //获取提交值
            ServletContext context = this.getServletContext();
            // 定位需要转发的路径
            RequestDispatcher rd = context
                    .getRequestDispatcher("/fail.html");
            // 真正转向页面
            rd.forward(request, response);
        }

    }

}

模型(model)

public class LoginBean {

    public boolean validate(String username)
    {
        boolean flag = false;
        //去掉空格
        if(username.trim() != null && "liwen".equals(username.trim()))
        {
            flag = true;
        }

        return flag;
    }

}

技术分享图片

Servle--转发实例学习笔记

标签:Servle   转发   实例   学习笔记   

原文地址:http://blog.51cto.com/357712148/2104553

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!