服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - servlet转发、包含详解(七)

servlet转发、包含详解(七)

2021-01-14 15:41柳暗花明睡一觉 Java教程

这篇文章主要为大家详细介绍了servlet转发、包含的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家介绍了servlet转发、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 类用于处理用户请求参数。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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  用于输出处理结果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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  用于包含其他组件,并返回响应结果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

?
1
2
3
4
5
6
7
8
9
10
<!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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

?
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>footer</title>
</head>
<body>
<span>Thanks for stopping</span>
</body>
</html>

web.xm配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/wwyx-xi/p/7599237.html

延伸 · 阅读

精彩推荐