首页 > 其他分享 >ServletContext

ServletContext

时间:2022-09-28 11:08:22浏览次数:33  
标签:ServletContext ServletException resp req IOException context

web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;

作用:

  1)共享数据,我在这个servlet中保存的数据,可以在另外一个servlet中拿到

ServletContext_ide

 

 HelloServlet

public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = "老尹"; // 数据
context.setAttribute("username",username); // 将一个数据保存在ServletContext中
}
}

getc

public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
// 获得ServletContext中放置的uaername数据,强转为String
String username = (String)context.getAttribute("username");
resp.setContentType("text/html"); // 设置文本格式
resp.setCharacterEncoding("utf-8"); // 设置字符格式
resp.getWriter().print("name" + username); // 输出信息到网页

}

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

 

2) 获取初始化参数

<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);

}

 

3)请求转发

ServletContext_java_02

 

 gp的代码(gp是ServletDemo03的缩写)

package com.kuang.servlet;

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

public class ServletDemo03 extends HelloServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);

}

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

 

图解重定向和转发:

重定向

ServletContext_ide_03

 

 

转发

ServletContext_ide_04

 

 

4:读取资源文件

Properties类

 

标签:ServletContext,ServletException,resp,req,IOException,context
From: https://blog.51cto.com/u_15810109/5718727

相关文章

  • ServletContext
     ServletContext类中有这么四个方法:getRealPath(Stringpath)getResource(Stringpath)getResourceAsStream(Stringpath)getResourcePaths(Stringpath)......
  • ServletContextListener使用
    ServletContextListener使用步骤:1,定义一个类,实现servletcontextListener接口2。复写方法3。配置1.web.xml<klistener><listener......
  • ServletContext对象概述、ServletContext获取
    ServletContext对象概述1,概念:代表整web应用,可以和程序容器(服务器)来通信2,功能:获取MIME类型域对象:共享数据获取文件的真实(服务器)路径......