码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet中使用RequestDispatcher调派请求--forware

时间:2016-05-07 19:43:20      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

顺便演示了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

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5468879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!