博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet中使用RequestDispatcher调派请求--forware
阅读量:5833 次
发布时间:2019-06-18

本文共 3170 字,大约阅读时间需要 10 分钟。

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

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

 

 

HelloModel.java:

package cc.openhome;import java.util.*;public class HelloModel {    private Map
messages = new HashMap
(); 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 =             ""            + "    "            + "        
" + " %s" + " " + " " + "

%s

" + " " + ""; /** * @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); }}

转载地址:http://khedx.baihongyu.com/

你可能感兴趣的文章
在CentOS上搭建git仓库服务器以及mac端进行克隆和提交到远程git仓库
查看>>
測試文章
查看>>
Flex很难?一文就足够了
查看>>
【BATJ面试必会】JAVA面试到底需要掌握什么?【上】
查看>>
CollabNet_Subversion小结
查看>>
mysql定时备份自动上传
查看>>
Linux 高可用集群解决方案
查看>>
17岁时少年决定把海洋洗干净,现在21岁的他做到了
查看>>
linux 启动oracle
查看>>
《写给大忙人看的java se 8》笔记
查看>>
倒计时:计算时间差
查看>>
Linux/windows P2V VMWare ESXi
查看>>
Windows XP倒计时到底意味着什么?
查看>>
tomcat一步步实现反向代理、负载均衡、内存复制
查看>>
运维工程师在干什么学些什么?【致菜鸟】
查看>>
Linux中iptables详解
查看>>
java中回调函数以及关于包装类的Demo
查看>>
maven异常:missing artifact jdk.tools:jar:1.6
查看>>
终端安全求生指南(五)-——日志管理
查看>>
Nginx 使用 openssl 的自签名证书
查看>>