servlet 转发包含

Posted 柳暗花明睡一觉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了servlet 转发包含相关的知识,希望对你有一定的参考价值。

1.什么是servlet转发?

  servlet将用户的请求通过一组servlet来处理,每个servlet都有自己特定的职责,当该servlet完成自己的工作后将请求转发到下一个servlet来处理,这种通过不同servelet相互转发请求来完成对某个请求的处理过程就叫做servlet转发。

2.什么是servlet包含?

  servlet包含就是源servlet将其他web组件(servlet,html,jsp),生成的结果包含到自己的结果中。

3.为什么需要servlet转发和包含?

  servlet对象是由Servlet容器创建,Servlet对象的Service()方法是由servlet容器调用,servlet之间是不允许相互调用(servlet API 2.1以前版本可以)。但是我们又需要通过多个servlet组件协调完成任务,同时方便代码维护管理,提高开发效率。这样就可以需要servlet转发和包含的技术支持。

4、转发和包含的相同点

  (1)都是对同一个请求对象处理,源组件和目标组件共享同一个ServletRequest对象和ServletResponse对象。

  (2)目标组件都可以是servlet,jsp,html。

  (3)都依赖javax,servlet.RequestDispatcher 接口。

5.RequestDIspatcher 对象

  该接口表示请求分发器

  (1)获取RequestDispatcher对象方法

    调用ServletContext的getRequestDispather(String path)方法获得。

    调用ServletRequest的getRequestDispather(String path)方法获得。

  (2)两种方法的区别

    ServletContext调用方法的参数必须为绝对路径,而ServletRequest调用方法参数可以是绝对路径也可以是相对路径。

6.请求转发案例

CheckServlet 类用于处理用户请求参数。
package com.learn;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/09/26.
 */
public class CheckServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userName = req.getParameter("name");
        String message = "";

        if(userName == null){
            message = "please input your name";
        } else {
            message = userName;
        }
        req.setAttribute("mess",message);
        RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/out");

        PrintWriter out = resp.getWriter();
        out.print("before forward ");
        System.out.println("before forward");

        requestDispatcher.forward(req,resp);

        out.print("after forward");
        System.out.println("after forward");

        out.close();
    }


}
OutServlet  用于输出处理结果
package com.learn;

import org.json.HTTP;

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

/**
 * Created by Administrator on 2017/09/26.
 */
public class OutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String message = (String) req.getAttribute("mess");

        PrintWriter printWriter = resp.getWriter();
        printWriter.print(message);
        printWriter.close();

    }
}

注意:   1.在转发之前不要提交响应结果。

    2.转发过程中,源组件的响应结果不会返回,只会返回目标组件的响应结果。

 

web.xml配置

  <servlet>
        <servlet-name>check</servlet-name>
        <servlet-class>com.learn.CheckServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>check</servlet-name>
        <url-pattern>/check</url-pattern>
    </servlet-mapping>
    
    <servlet>
        <servlet-name>out</servlet-name>
        <servlet-class>com.learn.OutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>out</servlet-name>
        <url-pattern>/out</url-pattern>
    </servlet-mapping>

 

7.包含(include)案例 

MainServlet  用于包含其他组件,并返回响应结果
package com.learn;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/09/26.
 */
public class MainServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");

        PrintWriter out = resp.getWriter();
        out.println("<html><head><title>mainServlet</title></head>");
        out.println("<body>");

        ServletContext servletContext = getServletContext();

        RequestDispatcher headDisPatcher = servletContext.getRequestDispatcher("/header.html");
        RequestDispatcher greetDisPatcher = servletContext.getRequestDispatcher("/greet");
        RequestDispatcher footeDisPatcher = servletContext.getRequestDispatcher("/footer.html");

        headDisPatcher.include(req,resp);
        greetDisPatcher.include(req,resp);
        footeDisPatcher.include(req,resp);

        out.print("</body></html>");
        out.close();


    }
}

 

header.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>header</title>
</head>
<body>
<font size="6">Welcome to ab</font>
</body>
</html>
GreetServlet 
package com.learn;

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

/**
 * Created by Administrator on 2017/09/26.
 */
public class GreetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PrintWriter out = resp.getWriter();

        out.print("hi! "+req.getParameter("name")+", <p>");
        out.close();
    }
}

footer.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>footer</title>
</head>
<body>
<span>Thanks for stopping</span>
</body>
</html>

web.xm配置

 <servlet>
        <servlet-name>main</servlet-name>
        <servlet-class>com.learn.MainServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>/main</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>greet</servlet-name>
        <servlet-class>com.learn.GreetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>greet</servlet-name>
        <url-pattern>/greet</url-pattern>
    </servlet-mapping>

 

以上是关于servlet 转发包含的主要内容,如果未能解决你的问题,请参考以下文章

Java Web中请求转发和请求包含

Servlet 及 请求转发与重定向

servlet请求转发与重定向的区别

JavaLearn#(23)JSP相关语法HTTP协议Servlet介绍Servlet生命周期请求和响应相对路径转发和重定向

JavaLearn#(23)JSP相关语法HTTP协议Servlet介绍Servlet生命周期请求和响应相对路径转发和重定向

Servlet的forward与include方法