用ServletContext简单实现共享数据
发布日期:2022-03-09 00:49:18
浏览次数:43
分类:技术文章
本文共 1198 字,大约阅读时间需要 3 分钟。
public class test01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String username = "小明"; context.setAttribute("username", username); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}
用getServletContext方法得到ServletContext对象,并且用setAttribute() 将数据进行存储
public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String username = (String) context.getAttribute("username"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); resp.getWriter().println( "姓名:" + username); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}
用getAttribute()方法拿到存储的数据,然后在配置Servlet和Servlet的映射
启动服务就可以在浏览器中进行展示
转载地址:https://blog.csdn.net/m0_57831732/article/details/122260666 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年03月27日 18时44分27秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
svn提交的一个坑
2019-04-27
eclipse识别不了模拟器解决办法
2019-04-27
unity mesh合并
2019-04-27
谈谈类之间的关联关系与依赖关系
2019-04-27
unity5.x assetbundle打包和加载
2019-04-27
C#用正则表达式去匹配被双引号包起来的中文
2019-04-27
lua table排序
2019-04-27
Unity发布的ios包在iphone上声音是从听筒里出来的问题
2019-04-27
UIScrollView复用节点示例
2019-04-27
Unity 5 AudioMixer
2019-04-27
Unity 代码混淆: CodeGuard的使用
2021-06-30
UGUI 列表循环使用
2021-06-30
使用命令行运行unity并执行某个静态函数(运用于命令行打包和批量打包)
2021-06-30
web.py框架
2021-06-30
web.py学习笔记
2021-06-30
python的代码缩进
2021-06-30
A* Pathfinding Project (Unity A*寻路插件) 使用教程
2021-06-30
bash学习笔记
2019-04-27
sqlite学习
2019-04-27