MVC Design Patterns - Study Notes

1) If you strictly follow the MVC idea, the browser home page accesses the controller, and then the controller forwards it to the view
2) The resources located in the WEB-INF/ directory cannot be directly accessed by the client, but can only be forwarded by the server Enter
3) The request object can also be used for resource forwarding
request
.getRequestDispatcher("/WEB-INF/success.html")
.forward(request,response);
4) Verify Chinese by regular expression: [\u4E00-\uFA29] +
5) For redirection, the two Request domain objects before and after are different. If the value cannot be obtained, "null" will be returned, and no error will be reported.

java code:

ModelBean

public class ModelBean {
    public boolean validate(String username){
        boolean flag = false;
        //username.matches("[a-zA-Z0-9]+")) 用户名是数字与字母
        if(username!=null && username.matches("[\u4E00-\uFA29]+")){
            flag = true;
        }
        return flag;
    }
}

Demo8


import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.web.domain.ModelBean;

public class Demo8 extends HttpServlet {
    //NO1
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //转发到view.html页面
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/view.html");
        rd.forward(request,response);
    }
    //NO2
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        ModelBean mb = new ModelBean();
        boolean flag = mb.validate(username);
        if(flag){
            //将用户名绑定到Request域对象中
            request.setAttribute("USERNAME",username);

            /*转发到ListServlet.java
            request
                .getRequestDispatcher("/ListServlet")
                .forward(request,response);
            */  

            //重定向到ListServlet.java
            response.sendRedirect("/day07/ListServlet");

        }else{
            request
            .getRequestDispatcher("/WEB-INF/fail.html")
            .forward(request,response);
        }
    }
}

ListServlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("欢迎"+username+"光临");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("欢迎"+username+"光临");     
    }
}

html

view.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="/day07/Demo8" method="post">
        <table border="1" align="center">
            <caption>用户登录</caption>
            <tr>
                <th>用户名</th>
                <td><input type="text" name="username"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="提交"/>
                            
                    <input type="reset" value="重填"/>
                </td>
            </tr>   
        </table>
    </form>
  </body>
</html>

sucess.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>

fail.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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982499&siteId=291194637