Servlet应用程序(1)----基于Servlet Controller的MVC
来源:互联网 发布:凤凰金融 以大数据为 编辑:程序博客网 时间:2024/06/16 05:47
实现MVC模式的应用程序由三个模块组成:模型Model,试图View,和控制器Controller。
View负责应用程序的显示,Model负责封装应用程序的数据和业务逻辑,Controller负责接收用户的输入,并命令M,V做出相应的修改。
在MVC中,是用一个Servlet或者过滤器充当Controller。每一个HTTP请求都必须被定向到控制器中,请求的URI高粗控制器要使用哪个Action,
实例:产品信息:
Product类:
Product实例是一个封装产品的JavaBean。
public class Product implements Serializable{ //实现Serializable接口,以便其实例可以安全的保存在HttpSession对象中。 private static final long serialVersionUID = 987565323L; private String name; private String description; private float price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; }}
ProductForm类:
Form类被映射到一个HTML表单。
/* * 与Product类的区别: * Form对象不需要将ServletRequesst传给其他组件,比如验证器 * 保护用户的输入,验证失败时在它的原始表单中重新显示出来。 * 不需要实现Serializable接口,因为Form对象通常不会保存到HttpSession中。 */public class ProductForm { private String name; private String description; private String price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; }
Controller类:
public class ControllerServlet extends HttpServlet{ private static final long serialVersionUID = 125232L; public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { process(request,response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { process(request, response); } /* * process处理请求 */ private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //获取uri和action的名称 String uri = request.getRequestURI(); int lastIndex = uri.lastIndexOf("/"); String action = uri.substring(lastIndex+1); if(action.equals("product_input")){ //没有action类 不处理 }else if(action.equals("product_save")){ //创建表单 ProductForm productForm = new ProductForm(); productForm.setName(request.getParameter("name")); productForm.setDescription(request.getParameter("description")); productForm.setPrice(request.getParameter("price")); //创建模型 Product product = new Product(); product.setName(productForm.getName()); product.setDescription(productForm.getDescription()); product.setPrice(Float.parseFloat(productForm.getPrice())); SaveProductAction saveProductAction = new SaveProductAction(); saveProductAction.save(product); //保存模型到视图 request.setAttribute("product", product); } //映射到视图 String dispatchUrl = null; if(action.equals("product_input")){ dispatchUrl="jsp/ProductForm.jsp"; }else if(action.equals("product_save")){ dispatchUrl="jsp/ProductDetails.jsp"; } if(dispatchUrl != null){ RequestDispatcher rd = request.getRequestDispatcher(dispatchUrl); rd.forward(request, response); } }}
Action类:
这里没有实现Save方法,在下一个博客将提供一个方法实现
public class SaveProductAction { public void save(Product product) { //添加到数据库操作 }}
View:
ProductForm.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Add Product Form</title><style type="text/css"> @import url(css/main.css);</style></head><body><div id="global"> <h3>Add a product</h3> <form method="post" action = "product_save"> <table> <tr> <td>Product Name:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>Description:</td> <td><input type="text" name="description"/></td> </tr> <tr> <td>Price:</td> <td><input type="text" name="price"/></td> </tr> <tr> <td><input type="reset"/></td> <td><input type="submit" value="Add Product"> </tr> </table> </form></div></body></html>
ProductDetails.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Save Product</title></head><body><div> <h4>SAVED</h4> <p> <h5>Details:</h5> Product Name:${product.name }<br> Description:${product.description }<br> Price:${product.price } </p></div></body></html>
部署:
<servlet> <servlet-name>ControllerServlet</servlet-name> <servlet-class>model.ControllerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ControllerServlet</servlet-name> <url-pattern>/product_input</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ControllerServlet</servlet-name> <url-pattern>/product_save</url-pattern> </servlet-mapping>
我们先访问product_input
填写好信息之后就提交
然后可以看到:
使用Servlet Contrller时,允许使用Servlet作为首页。这是一个重要的特性。因为可以通过设置,就只需要在浏览器输入域名就可以调用这个Servlet Controller。然而过滤器就不能这样处理。
在运行成功后,也许再访问时会有以下错误:
严重: Error processing request
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
这是因为Jsp中存在不能使用中文Cookie名的问题,一旦使用了中文的名字程序就会报错,这时需要清除本地Cookie缓存。在查看代码中的编码问题。就能正常访问了。
- Servlet应用程序(1)----基于Servlet Controller的MVC
- Servlet应用程序(2)----基于Filter Dispatcher的MVC
- 【Java.Web】MVC —— 基于Servlet Controller的Model2 —— 示例
- Servlet教程(四) 基于Servlet的MVC设计模式
- 基于Servlet的MVC设计模式
- 7基于Servlet的MVC模式
- 基于Servlet的MVC模式Demo
- 基于MVC思想的JSP+Servlet+JavaBean的登录模块
- 教案:第5章 基于Servlet的MVC设计模式
- jsp第八课-基于servlet的MVC模式
- java web 基于servlet及MVC的学习小结
- 基于jsp和servlet的简单mvc登陆注册
- 基于JSP + servlet + javabean的MVC简单验证登录实例
- 基于jsp+servlet+javabean的MVC模式简单应用
- springMVC <mvc:default-servlet-handler /> 导致controller无效
- Servlet MVC
- 建立一个简单的servlet controller
- web应用程序的缺省servlet
- 如何得心应手地运用贪心?
- POJ
- 4.Myeclipse开发Java web程序
- Java:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
- 内省(Introspector) — JavaBean
- Servlet应用程序(1)----基于Servlet Controller的MVC
- 二阶指针和一阶指针实例区分
- 1.VMware安装3个ubuntu14.05
- Xamarin.Forms 用户界面——控件——ListView——List外观
- Rikka with Subset(hdu6092)
- springboot 热部署 idea
- linux下开启SSH,并且允许root用户远程登录,允许无密码登录
- java的动态代理机制详解
- 排序算法之快速排序