JavaWeb学习笔记 —— ServletContext常用方法(setAttribute、getAttribute、getInitParameter等)
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
·
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
主要方法:
ServletContext context = this.getServletContext();
1、共享数据
用到的主要方法:
//设置
context.setAttribute("参数名",参数值(可以是变量名));
//获取
context.getAttribute("参数名");
在HelloServlet 里设置上下文数据
package com.ylw.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 context = this.getServletContext();
String username = "影龙武"; //数据
context.setAttribute("username",username); //将一个数据保存在了ServletContext,数据的名字为username,值为变量username的值
System.out.println("Hello,进入doGet");
}
}
在GetServlet 里获取上下文
package com.ylw.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 context = this.getServletContext(); //这里获取的上下文就是HellServlet里设置的
String username = (String) context.getAttribute("username"); //获取上下文中名为username的值
resp.setContentType("text/html"); //设置文本类型
resp.setCharacterEncoding("utf-8"); //设置编码
resp.getWriter().print("用户名:"+username); //写在网页上
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp); //注意这里改成doGet(req, resp),形成一个规范
}
}
web.xml里的注册
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.ylw.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getContext</servlet-name>
<servlet-class>com.ylw.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getContext</servlet-name>
<url-pattern>/getContext</url-pattern>
</servlet-mapping>
2、获取初始化参数
用到的主要方法:
context.getInitParameter("参数名")
web中配置初始化信息
<!--配置一些web应用的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
3、请求转发
用到的主要方法:
context.getRequestDispatcher("转发路径").forward(req,resp);
转发代码
package com.ylw.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //这里获取的上下文就是HellServlet里设置的
System.out.println("进入了Demo04");
// RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp"); //设置转发路径
// requestDispatcher.forward(req,resp); //调用forward实现请求转发
//合并上面两句
context.getRequestDispatcher("/gp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//注意这里改成doGet(req, resp),形成一个规范
}
}
4、读取资源文件 getResourceAsStream()
用到的主要方法:
//获取文件,变成流
this.getServletContext().getResourceAsStream("文件路径")
//加载流
Properties prop = new Properties();
prop.load(inputStream); //加载上面文件转化成的流
prop.getProperty("属性名"); //获取文件中的一个属性
如何插入一段漂亮的代码片
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
var foo = 'bar';
Properties
比较分析:
在java目录下新建properties
在resources目录下新建properties
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath:
在java目录下的properties,发现导出的target中没有该文件,就需要在porm.xml中添加配置,详见博客https://blog.csdn.net/qq_43594119/article/details/106199248中的解决资源导出失败问题
读取资源文件:
1在resources目录下新建一个properties文件作为资源文件
2.写一个类用来读取这个资源文件,下面是截图和代码
package com.ylw.servlet;
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.InputStream;
import java.util.Properties;
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得资源,变成一个流
InputStream inputStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(inputStream); //加载上面文件转化成的流
String user = prop.getProperty("username"); //获取文件中的一个属性
String pwd = prop.getProperty("password"); //获取文件中的一个属性
//在网页上显示出来
resp.getWriter().print("user: " + user +'\n' + "pwd: " + pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp); //注意这里改成doGet(req, resp),形成一个规范
}
}
3.在web.xml中注册该类
<servlet>
<servlet-name>sd5</servlet-name>
<servlet-class>com.ylw.servlet.ServletDemo05</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sd5</servlet-name>
<url-pattern>/sd5</url-pattern>
</servlet-mapping>
更多推荐
已为社区贡献1条内容
所有评论(0)