顺便演示了MVC的作法,以后hello.view可以移交到jsp中处理。

而MODEL和CONTROL,VIEW就实现了分享。

HelloModel.java:

package cc.openhome;

import java.util.*;

public class HelloModel {
    private Map<String, String> messages =
            new HashMap<String, String>();
    public HelloModel() {
        messages.put("caterpillar", "Hello");
        messages.put("Justin", "Welcome");
        messages.put("momor", "Hi");
    }

    public String doHello(String user) {
        String message = messages.get(user);
        System.out.println(message + "@@@@@@@@@@@@@@@@@");
        return message + ", " + user + "!";
    }

}

HelloController.java:

package cc.openhome;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloController
 */
@WebServlet("/hello.do")
public class HelloController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private HelloModel model = new HelloModel();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String name = request.getParameter("user");
        String message = model.doHello(name);
        System.out.println(message + "$$$$$$$$$$");
        request.setAttribute("message", message);
        request.getRequestDispatcher("hello.view").forward(request, response);
    }

}

HelloView.java:

package cc.openhome;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloView
 */
@WebServlet("/hello.view")
public class HelloView extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String htmlTemplate =
            "<html>"
            + "    <head>"
            + "        <meta http-equiv='Content-Type'"
            + "            content='text/html; charset=UTF-8'>"
            + "        <title>%s</title>"
            + "    </head>"
            + "    <body>"
            + "        <h1>%s</h1>"
            + "    </body>"
            + "</html>";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloView() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String user = request.getParameter("user");
        String message = (String) request.getAttribute("message");
        System.out.println(user + message + "##################");
        String html = String.format(htmlTemplate, user, message);
        response.getWriter().print(html);
    }

}

Servlet中使用RequestDispatcher调派请求--forware的更多相关文章

  1. Servlet中使用RequestDispatcher调派请求--include

    一共有两种调派方式,一个是include用于包含进来,一个是forward,是转发出去. 这时先测试包含的include方式. Some.java: package cc.openhome; impo ...

  2. Java EE javax.servlet中的RequestDispatcher接口

    RequestDispatcher接口 public interface RequestDispatcher 一.介绍 定义一个对象,从客户端接收请求并将其发送到服务器上的任何资源(例如servlet ...

  3. 【Servlet】—在servlet中常混的请求路径

    在页面请求,后台获取相关请求路径是,自己长搞混的几个路径,再次做次标记,不要每次使用想不起来是,都去写一个小的demo来测试. request.getContextPath(); request.ge ...

  4. servlet中如何发送ajax请求并动态拼接数据到html中

    废话不多说,直接上代码 1.servlet 2.js 3.jsp 有不懂得欢迎来扣我哦^_^ 详细介绍请查看全文:https://cnblogs.com/qianzf/ 原文博客的链接地址:https ...

  5. Servlet中的一些注意事项

    servlet中的一些注意事项 1 什么是servlet? 1)Servlet是Sun公司制定的一套技术标准,包含与Web应用相关的一系列接口,是Web应用实现方式的宏观解决方案.而具体的Servle ...

  6. Servlet中的请求转发RequestDispatcher接口的forword与Include的区别

    RequestDispatcher接口中具有两个方法: forward() 与 include() 均 可完成请求 的转发.区别如下: forword(): 使用该方法,则当前 的 Servlet 中 ...

  7. servlet中请求转发(forword)与重定向(sendredirect)的区别

    摘自:http://www.cnblogs.com/CodeGuy/archive/2012/02/13/2349970.html 通俗易懂 servlet请求转发与重定向的区别: request.s ...

  8. Servlet中的请求包含

    public class SrcIncludeServlet extends HttpServlet { public void doGet(HttpServletRequest request, H ...

  9. AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式

    转载:http://blog.csdn.net/mhmyqn/article/details/25561535 HTTP请求中,如果是get请求,那么表单参数以name=value&name1 ...

随机推荐

  1. Word中公式和文字混排对齐的问题

    全选-字体-字符间距-位置-标准-确定 段落-中文版式-文本对齐方式-居中-确定

  2. thinkphp结合云之讯做短信验证码

    thinkphp结合云之讯做短信验证码先去云之讯注册账号 网址http://www.ucpaas.com/ 注册云之讯平台账号,即可免费获得10元测试费用测试够用啦 解压附件到 ThinkPHP\Li ...

  3. [Swift通天遁地]二、表格表单-(7)电子邮件Mail:实现单元格左右滑动调出功能按钮

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. MySQL数据库笔记总结

    MySQL数据库总结 一.数据库简介 1. 数据 所谓数据(Data)是指对客观事物进行描述并可以鉴别的符号,这些符号是可识别的.抽象的.它不仅仅指狭义上的数字,而是有多种表现形式:字母.文字.文本. ...

  5. 【BZOJ2565】最长双回文串 (Manacher算法)

    题目: BZOJ2565 分析: 首先看到回文串,肯定能想到Manacher算法.下文中字符串\(s\)是输入的字符串\(str\)在Manacher算法中添加了字符'#'后的字符串 (构造方式如下) ...

  6. js常用操作~~~~将持续更新

    1.替换多个模板变量 var s="my javascript is very poor,who can help me?" var reg=/(\w*)my(.*)is(.*)c ...

  7. [hihocoder][Offer收割]编程练习赛57

    1-偏差排列 斐波那契数列 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> ...

  8. 依存分析 Dependency Parsing

    依存分析 Dependency Parsing 句子成分依存分析主要分为两种:句法级别的和语义级别的 依存句法分析 syntactic dependency parsing 语义依存分词 semant ...

  9. 调用.NET Serviced Component引发的性能问题及其解决

    在企业用户环境里,.NET Serviced Component使用广泛.它比较好的把传统COM+封装和.NET应用逻辑衔接了起来,在服务器端应用起到重要作用..NET Serviced Compon ...

  10. 更改计算机名后DB2不能启动的解决方法

    1.找到以下位置目录下相应的文件db2nodes.cfg C:\Documents and Settings\All Users\Application Data\IBM\DB2\DB2COPY1\D ...