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