一个mvc小案例

 

一个mvc小案例_第1张图片

Add
	

CustomerServlet zzuli.mvcapp.servlet.CustomerServlet CustomerServlet /customerServlet protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getParameter("method"); switch(method) { case "add" : add(request,response);break; case "query" : query(request,response);break; case "delete" : delete(request,response);break; } }

 一个mvc小案例_第2张图片

一个mvc小案例_第3张图片

一个mvc小案例_第4张图片

利用反射实现多个请求访问一个Servlet

      Add
	

Delete

映射为*.do, CustomerServlet zzuli.mvcapp.servlet.CustomerServlet CustomerServlet *.do @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取servletPath:/add.do String servletPath = request.getServletPath(); String methodName = servletPath.substring(1); //得到方法名,去掉:/ .do methodName = methodName.substring(0, methodName.length() - 3); try { //利用反射获取method对应的方法 Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //利用反射调用对应的方法 method.invoke(this, request, response); } catch (Exception e) { e.printStackTrace(); } }

一个mvc小案例_第5张图片

拼SQL语句

     @Override
	public List getForListWithCriteriaCustomer(CriteriaCustomer cc) {
		String sql = "SELECT * FROM customer WHERE name LIKE ? AND address LIKE ? "
				+"AND phone LIKE ?";
           //在CriteriaCustomer里边封装了geter,seter方法,得到"%%","%name"
		return getForList(sql, cc.getName(), cc.getAddress(), cc.getPhone());
	}

    public String getAddress() {
		if(address == null)
			address = "%%";
		else
			address = "%" + address + "%";
		return address;
	}

一个mvc小案例_第6张图片

一个mvc小案例_第7张图片

一个mvc小案例_第8张图片

一个mvc小案例_第9张图片

一个mvc小案例_第10张图片

一个mvc小案例_第11张图片

 

 

 

 

 

 

 

你可能感兴趣的:(javaWEB)