标签:MVC设计模式-学习笔记
1)如果严格按照MVC思想的话,浏览器首页访问的是控制器,然后再由控制器转发到视图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;
}
}
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);
}
}
}
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+"光临");
}
}
<!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>
<!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>
<!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>
标签:MVC设计模式-学习笔记
原文地址:http://blog.51cto.com/357712148/2104909