MVC Design Patterns-Learning notes

Source: Internet
Author: User

1) If you strictly follow the MVC idea, the browser homepage is accessed by the controller and then forwarded by the Controller to the view
2) resources located in the web-inf/directory, the client cannot directly access, only through the server forwarding access
3) Request object can also be used for forwarding of resources
Request
. Getrequestdispatcher ("/web-inf/success.html")
. Forward (Request,response);
4) Verify Chinese with regular expression: [\u4e00-\ufa29]+
5) for redirection, the front and back of the two request domain objects are different, if the value is not, return "null", will not error

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, ioexcept        Ion {//forwards to view.html page RequestDispatcher rd = request.getrequestdispatcher ("/web-inf/view.html");    Rd.forward (Request,response); }//no2 public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcep        tion {request.setcharacterencoding ("UTF-8");        String username = request.getparameter ("username");        Modelbean MB = new Modelbean ();        Boolean flag = Mb.validate (username); if (flag) {//binds the user name to the request domain object Request.setattribute ("USERNAME", UsernaME); /* forward to Listservlet.java request. Getrequestdispatcher ("/listservlet"). Forward (requ            Est,response);        *//Redirect to 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+"光临");         }}
Htmlview.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Sucess.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Fail.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

MVC Design Patterns-Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.