73.【JavaWeb-01】(六)

简介: 73.【JavaWeb-01】

(九)、ServletContext

29.详解ServletContext的

(运用多个servlet进行交互)
1.web容器在启动的时候,他会为每一个web程序都创建一个对应的ServietContext
对象,它代表了当前的web应用;
(1).共享数据:我在A中保存的数据,可以在另一个Servlet中拿到;
2.基本代码展示:
(1). 设置ServletContext
ServletContext servletContext = this.getServletContext();
(2).设置键值对的操作:
servletContext.setAttribute("username",userName);  //将一个数据保存在了ServletContext中,名字为: username,值为:userName
=================================================存储值
(3).获取存储在共享ServletContext
因为我们知道他返回的是什么,然后程序不知道,所以我们要进行强制转化。
String username = (String) servletContext.getAttribute("username");
(4).输出信息的操作
3.执行顺序:
(1).必须先进行ServletContext进行存储,然后才能进行读取的操作。
1.共享效果:

1.共享效果:

1.存储信息

package com.Jsxs.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;
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.getInitParameter()   获取初始化参数
//        this.getServletConfig()   Servlet配置
//        this.getServletContext()  servlet上下文
        ServletContext servletContext = this.getServletContext();
        String userName="吉士先生";
        servletContext.setAttribute("username",userName);  //将一个数据保存在了ServletContext中,名字为: username,值为:userName
    }
}

2.读取共享的信息

package com.Jsxs.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;
public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
//        因为我们知道他返回的是什么,然后程序不知道,所以我们要进行强制转化。
        String username = (String) servletContext.getAttribute("username");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.getWriter().print("名字是:"+username);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

1.执行顺序

2.获取配置信息:
2.获取配置信息:
(1).在web添加配置信息:
 <context-param>
   <param-name>名字A</param-name>
   <param-value>配置的内容</param-value>
 </context-param>
(2). 设置ServletContext
ServletContext servletContext = this.getServletContext();
(3).设置方法getInitParameter()
String url = servletContext.getInitParameter("配置的名字");
(4).配置web 映射以及servlet:
<servlet></servlet>
<servlet -mapping></servlet -mapping>

1.设置ServletContext

package com.Jsxs.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;
public class ServletDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String url = servletContext.getInitParameter("url");
        resp.getWriter().print(url);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

2.配置web

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="4.0" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
 <context-param>
   <param-name>url</param-name>
   <param-value>jdbc:mysql://127.0.0.1:3306/demo1</param-value>
 </context-param>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.Jsxs.Servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>getHello</servlet-name>
    <servlet-class>com.Jsxs.Servlet.GetServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>getHello</servlet-name>
    <url-pattern>/getHello</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>url</servlet-name>
    <servlet-class>com.Jsxs.Servlet.ServletDemo03</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>url</servlet-name>
    <url-pattern>/demo3</url-pattern>
  </servlet-mapping>
</web-app>

3.请求转发:
3.请求转发:
(1).设置请求转发:
RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("转发到哪个地址?");
(2).调用forward实现请求转发,
requestDispatcher.forward(req,resp);

请求转发:

重定义:

请求转发

package com.Jsxs.Servlet;
import javax.servlet.RequestDispatcher;
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;
public class ServletDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入到demo444444");
        ServletContext servletContext = this.getServletContext();
//        转发到demo03这个项目里面
        RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/demo3");
//        调用forward实现请求转发,
        requestDispatcher.forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

WEB

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="4.0" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
 <context-param>
   <param-name>url</param-name>
   <param-value>jdbc:mysql://127.0.0.1:3306/demo1</param-value>
 </context-param>
<!--第一个hello-->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.Jsxs.Servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
<!--得到名字-->
  <servlet>
    <servlet-name>getHello</servlet-name>
    <servlet-class>com.Jsxs.Servlet.GetServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>getHello</servlet-name>
    <url-pattern>/getHello</url-pattern>
  </servlet-mapping>
<!--  获取参数-->
  <servlet>
    <servlet-name>url</servlet-name>
    <servlet-class>com.Jsxs.Servlet.ServletDemo03</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>url</servlet-name>
    <url-pattern>/demo3</url-pattern>
  </servlet-mapping>
<!--  请求转发-->
  <servlet>
    <servlet-name>demo4</servlet-name>
    <servlet-class>com.Jsxs.Servlet.ServletDemo4</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo4</servlet-name>
    <url-pattern>/demo4</url-pattern>
  </servlet-mapping>
</web-app>

输入这个转发到demo3这个目录

相关文章
|
Java
74.【JavaWeb -02】(四)
74.【JavaWeb -02】
47 0
|
Java Apache C++
74.【JavaWeb -02】(七)
74.【JavaWeb -02】
62 0
|
JavaScript 前端开发 Java
JavaWeb02
JavaWeb02
161 0
|
前端开发 网络协议 Java
javaweb的理解
javaweb的理解
72 0
|
Java 应用服务中间件 Maven
73.【JavaWeb-01】(五)
73.【JavaWeb-01】
70 0
|
Java 应用服务中间件 Maven
73.【JavaWeb-01】(三)
73.【JavaWeb-01】
62 0
|
前端开发 Java 数据库
75.【JavaWeb-03】(一)
75.【JavaWeb-03】
47 0
|
Java Maven
73.【JavaWeb-01】(七)
73.【JavaWeb-01】
51 0
|
前端开发 数据安全/隐私保护
75.【JavaWeb-03】(四)
75.【JavaWeb-03】
49 0
|
Java
74.【JavaWeb -02】(三)
74.【JavaWeb -02】
48 0

相关实验场景

更多
下一篇
DataWorks