顺便演示了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. IDEA Spark Streaming Flume数据源 --解决无法转化为实际输入数据,及中文乱码(Scala)

    需要三步: 1.shell:往 1234 端口写数据 nc localhost 1234 2.shell: 启动flume服务 cd /usr/local2/flume/bin ./flume-ng ...

  2. Redis学习和应用记录(1)--介绍和安装

    Redis是一个开源的分布式缓存框架,它也常被理解为数据结构服务器,因为它包含丰富的数据类型,如strings, hashes, lists, sets, sorted sets, bitmaps a ...

  3. 使用Keras做OCR时报错:ValueError: Tensor Tensor is not an element of this graph

    现象 项目使用 Flask + Keras + Tensorflow 同样的代码在机器A和B上都能正常运行,但在机器C上就会报如下异常.机器A和B的环境是先安装的,运行.调试成功后才尝试在C上跑. F ...

  4. VUE修改每个页面title

    //index.js routes: [ { name:'home', path: '/home/:openname', component: Home, meta: { title: '首页' } ...

  5. Spring实例化bean之后的处理, 关于BeanPostProcessor接口的使用

    业务需求:缓存页面,展示需要缓存的所有对象,每类对象在字典表中有编码对应,点击某个对象可以缓存某类对象,每类对象都有自己的缓存runner(弱弱的说一句,本人看到这里的第一反应就是if-else,捂脸 ...

  6. Nginx 配置https请求

    通过阿里云生成指定的https证书文件xxxx.key 和 xxxx.pem文件 在阿里云上申请的https证书的是pem格式,转成cer 先在终端cd到文件目录下 然后 openssl x509 - ...

  7. Elasticsearch之CURL命令的GET

    这是个查询命令. 前期博客 Elasticsearch之CURL命令的PUT和POST对比 1. 以上是根据员工id查询. 即在任意的查询字符串中添加pretty参数,es可以得到易于我们识别的jso ...

  8. Hadoop Hive概念学习系列之hive的脚本执行(二十)

    相当一部分人,容易忽略hive脚本,其实,这在生产环境里,是非常重要的! $ hive -e "show tables" $ hive -e "show tables & ...

  9. ResGen.exe”已退出,代码为2 问题处理

    转载自  http://blog.sina.com.cn/s/blog_5f82a1060101d8tm.html 在64位的Windows 7下,用VS2010编译4.0以前的.Net项目会有问题. ...

  10. postgresql用sql语句查询表结构

    用到的postgresql系统表 关于postgresql系统表,可以参考PostgreSQL 8.1 中文文档-系统表. pg_class 记录了数据库中的表,索引,序列,视图("关系&q ...