Tomcat MVC example - FindCat
来源:互联网 发布:淘宝网睡衣家居服 编辑:程序博客网 时间:2024/06/07 11:45
This program is developed in Tomcat7 Eclipse EE
program purpose: Find the cat by its name
index.html - has a input box that requires the name of the cat
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><form action="findcat.do" method="post">Cat name: <input type="text" name="name" /><br /><input type="submit" ></form></body></html>
web.xml - the Deployment Descriptor for this program, maps findcat.do to com.example.web.FindCat
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>UseBean</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>CatFinder</servlet-name> <servlet-class>com.example.web.FindCat</servlet-class> </servlet> <servlet-mapping> <servlet-name>CatFinder</servlet-name> <url-pattern>/findcat.do</url-pattern> </servlet-mapping> </web-app>
FindCat.java - The Controler, it is a class that extends HttpServletoverrides the doPost method. if the cat's name is John, return John and its age 2 if the name is Kitty, return Kitty and its age 3.
The Cat is send to result.jsp
package com.example.web;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 com.example.model.Cat;public class FindCat extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String catName = req.getParameter( "name");Cat foundCat = new Cat();if( catName.equals("John" ) ) {foundCat.setName("John" );foundCat.setAge( 2 );}else if( catName.equals("Kitty") ) {foundCat.setName("Kitty" );foundCat.setAge( 3 );}RequestDispatcher rd = getServletContext().getRequestDispatcher("/result.jsp");System.out.println( "foundCat name: " + foundCat.getName() );System.out.println( "foundcat age: " + foundCat.getAge() );req.setAttribute("foundCat", foundCat);rd.forward( req, resp);}}
Cat.java - the Model
package com.example.model;import java.io.Serializable;public class Cat implements Serializable {String name;int age;public Cat(){}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
result.jsp - The View
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><jsp:useBean id="foundCat" class="com.example.model.Cat" scope="request" /><!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=UTF-8"><title>Cats found</title></head><body><% if( foundCat.getName() == null ){%> <h1>sorry, can not find this cat</h1> <% }else{%>Name: <jsp:getProperty name="foundCat" property="name" />Age: <jsp:getProperty name="foundCat" property="age" /><%}%></body></html>
Structure of program
- Tomcat MVC example - FindCat
- Spring MVC - DispatcherServlet Example
- Spring MVC Model Example
- one mvc example
- Spring MVC MultiActionController example
- Spring MVC PropertiesMethodNameResolver example
- Spring MVC ParameterMethodNameResolver example
- Spring MVC ParameterMethodNameResolver example
- Spring MVC ParameterizableViewController example
- Spring MVC InternalResourceViewResolver example
- Spring MVC XmlViewResolver example
- Spring MVC ResourceBundleViewResolver example
- Spring MVC BeanNameUrlHandlerMapping example
- Spring MVC ControllerClassNameHandlerMapping example
- Spring MVC SimpleUrlHandlerMapping example
- Spring MVC RedirectView example
- Spring MVC textbox example
- Spring MVC BeanNameUrlHandlerMapping example
- java File读取文件
- 最大流模版
- JavaScript函数积累
- 正则表达式(字符串处理大爱)
- 好用的IOS平台SSH应用
- Tomcat MVC example - FindCat
- STL实现的底层数据结构简介
- arm7 力天电子 lpc2148 GPIO之花样流水灯
- heru 5081 Turn the corner(三分)
- C# 多线程同步
- XML语法—XML中5个预定义的实体引用及对应的英语解释
- 关于2812的bootloader与中断向量表之间的关系——DSP2812启动过程
- 【算法】输入一颗二元查找树,将该树转换为它的镜像
- datastage调度之java调度 欢迎加QQ群:106389516讨论