当前位置: 首页 > news >正文

网站建设 排行seo基础视频教程

网站建设 排行,seo基础视频教程,必须做网站等级保护,交友网站建设1、什么是ServletContext ServletContext是一个全局储存空间,随服务器的生命周期变化, Cookie,Session,ServletContext的区别 Cookie: 存在于客户端的本地文本文件 Session: 存在于服务器的文本文件&#…

1、什么是ServletContext
ServletContext是一个全局储存空间,随服务器的生命周期变化,

Cookie,Session,ServletContext的区别
Cookie: 存在于客户端的本地文本文件
Session: 存在于服务器的文本文件,一个客户端有唯一session
ServletContext: 存在于服务器,并开辟一块服务器区域,可以被所有客户端访问。
在这里插入图片描述
ServletContext对象通常也被称为context域对象。

Servlet对象之间可以通过ServlertContext对象来实现通讯。

当WEB容器在启动时,为每个Web应用程序都创建一个对应的ServletContext,它代表当前Web应用并且它被所有客户端共享。
我们可以拓展场景,通常在咨询网站浏览时,会有客服弹窗,这就可以基于ServletContext进行通讯。

当web应用关闭、Tomcat关闭或者Web应用reload的时候,ServletContext对象会被销毁。【即关闭浏览器客户端】

2、ServletContext使用方法

  • 获取ServletContext对象
//直接调取ServletContext对象
this.getServletContext();
//通过ServletConfig对象,调取ServletContext对象
this.getServletConfig().getServletContext();

ServletContext对象的结构

名字(String)值(Object)

操作方法
添加属性:boolean setAttribute(String name,Object obj)
得到值:Object getAttribute(String name)
删除属性:boolean removeAttribute(String name)

生命周期
ServletContext中的生命周期从创建开始,到服务器关闭结束。

使用实例
1.分别创建两个Servlet对象
servlet1.java

public class servlet1 extends HttpServlet{public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();ServletContext servletContext = this.getServletContext();//ServletContext servletCOntext = this.getServletConfig().getServletContext();servletContext.setAttribute("name","小明");out.println("将 name = " + servletContext.getAttribute("name")+"写入ServletContext");}
}

servlet2.java

public class servlet2 extends HttpServlet{public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();ServletContext servletContext = this.getServletContext();//ServletContext servletCOntext = this.getServletConfig().getServletContext();String name = servletContext.getAttribute("name");out.println("name = " + name);}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><servlet-name>servlet1</servlet-name><servlet-class>servlet1</servlet-class></servlet><servlet-mapping><servlet-name>servlet1</servlet-name><url-pattern>/servlet1</url-pattern></servlet-mapping><servlet><servlet-name>servlet2</servlet-name><servlet-class>servlet2</servlet-class></servlet><servlet-mapping><servlet-name>servlet2</servlet-name><url-pattern>/servlet2</url-pattern></servlet-mapping>
</web-app>

效果图
在这里插入图片描述
在这里插入图片描述

chrom浏览器

在这里插入图片描述

QQ浏览器

在这里插入图片描述
3、ServletContext应用

  • 多Servlet对象共享数据
  • 实现Servlet的请求转发

两种请求转发方法:
request对象转发

request.getRequestDispatcher("转发目的路径").forward(reqeust,response);

ServletContext实现请求转发

this.getServletContext().getRequestDispatcher("转发目的路径").forwaard(request,response);

配置Web应用初始化

<!--
init-param标签用于servlet配置初始化参数,通过ServletConfig对象获取参数-->
<servlet><servlet-name>MyServlet</servlet-name><servlet-class>MyServlet</servlet-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param>
</servlet>

获取初始化参数

String encoding = this.getServletConfig().getInitParameter("encoding");

配置Servlet全局配置

<context-param><param-name>name</param-name><param-value>gavin</param-value>
</context-param>

4、利用ServletContext对象读取资源文件(比如properties文件)
读取资源文件要根据资源文件所在位置决定,有两种情况:
4.1 文件在WebRoot文件夹下,即Web应用的根目录。
假设Web根目录下【web文件夹下】有一个配置数据库信息的dbinfo.properties文件,配置了name和password属性,通过ServletContext读取文件:

//这种方法默认读取路径为Web应用的根目录
InputStream stream = this.getServletContext().getResourceAsStream("dbinfo.properties");
// 创建属性对象
Properties properties = new Properties();
properties.load(stream);
String name = properties.getProperty("name");
String password = properties.getProperty("password");
out.println("name="+name+";password="+password);

dbinfo.properties

name=root
password=root

**4.2 文件放置在src目录下 **
这时需要使用类加载器,其默认加载src根目录

InputStream stream = MyServlet.class.getClassLoader().getResourceAsStream("dbinfo.properties");

若文件放置在src的子文件夹中,则加载方式为

InputStream inputStream = MyServlet.class.getClassLoader().getResourceAsStream("com/properties/dbinfo.properties");

注意: ServletContext只有在读取web应用根目录下的文件,才能获取文件的全路径。

String path = this.getServletContext().getRealPath("/images/Servlet.jpg");

网站开发中的功能模块应用

网站计数器,网站在线用户显示,聊天系统等功能
若涉及不同用户共享数据,且数据量不打,且不希望占用数据库内存,可以考虑使用ServletContext实现。

文章转载自:
http://wanjiafluey.crhd.cn
http://wanjiamesothorium.crhd.cn
http://wanjiahomolysis.crhd.cn
http://wanjiaribald.crhd.cn
http://wanjiavolunteer.crhd.cn
http://wanjiahaptics.crhd.cn
http://wanjiasolubilisation.crhd.cn
http://wanjiasexploitation.crhd.cn
http://wanjiaminifloppy.crhd.cn
http://wanjiaagentive.crhd.cn
http://wanjiaflyby.crhd.cn
http://wanjiaplace.crhd.cn
http://wanjiarepleader.crhd.cn
http://wanjiamastery.crhd.cn
http://wanjiaexecrably.crhd.cn
http://wanjialymphangiitis.crhd.cn
http://wanjiabudo.crhd.cn
http://wanjiajigger.crhd.cn
http://wanjiaconundrum.crhd.cn
http://wanjiainsurable.crhd.cn
http://wanjiasinography.crhd.cn
http://wanjiareconsider.crhd.cn
http://wanjiaperitonaeum.crhd.cn
http://wanjiaimpartment.crhd.cn
http://wanjiacrissum.crhd.cn
http://wanjialeptocephalic.crhd.cn
http://wanjiaseminal.crhd.cn
http://wanjiafugio.crhd.cn
http://wanjiafinnish.crhd.cn
http://wanjiaalphorn.crhd.cn
http://wanjialarksome.crhd.cn
http://wanjiafibroelastosis.crhd.cn
http://wanjiafanon.crhd.cn
http://wanjiamontaria.crhd.cn
http://wanjiaclearsighted.crhd.cn
http://wanjiaoocyst.crhd.cn
http://wanjiaperle.crhd.cn
http://wanjianexus.crhd.cn
http://wanjiaturfman.crhd.cn
http://wanjiaplanar.crhd.cn
http://wanjiabarratry.crhd.cn
http://wanjiaspherulite.crhd.cn
http://wanjianumbingly.crhd.cn
http://wanjiasyncopation.crhd.cn
http://wanjiaimpenitent.crhd.cn
http://wanjiafyi.crhd.cn
http://wanjiavolubility.crhd.cn
http://wanjiajapanese.crhd.cn
http://wanjiapontifices.crhd.cn
http://wanjiarebound.crhd.cn
http://wanjiawaterishlogged.crhd.cn
http://wanjiamotherless.crhd.cn
http://wanjiabasketball.crhd.cn
http://wanjiafelstone.crhd.cn
http://wanjiaattitudinize.crhd.cn
http://wanjiarudiment.crhd.cn
http://wanjiamerge.crhd.cn
http://wanjiasmarm.crhd.cn
http://wanjiaterrestrial.crhd.cn
http://wanjiadimissory.crhd.cn
http://wanjiasurge.crhd.cn
http://wanjianonillionth.crhd.cn
http://wanjiarubout.crhd.cn
http://wanjianiggertoe.crhd.cn
http://wanjiamughul.crhd.cn
http://wanjiaparmigiana.crhd.cn
http://wanjiatimecard.crhd.cn
http://wanjiadeeply.crhd.cn
http://wanjiadehiscence.crhd.cn
http://wanjialyme.crhd.cn
http://wanjiadeiform.crhd.cn
http://wanjiaharmonia.crhd.cn
http://wanjiaalecost.crhd.cn
http://wanjiahouselet.crhd.cn
http://wanjiarelaunder.crhd.cn
http://wanjiaculicid.crhd.cn
http://wanjiapaddy.crhd.cn
http://wanjiaastragalar.crhd.cn
http://wanjiasernyl.crhd.cn
http://wanjiafern.crhd.cn
http://www.15wanjia.com/news/123347.html

相关文章:

  • 装饰公司营销网站建设电商seo是什么意思
  • 东台做网站的公司小程序源码网
  • 个人虚拟机做网站最经典的营销案例
  • 如何将网站上传到万网主机可以免费推广的平台
  • app模板网站模板宁波seo排名公司
  • 网站设计公司 广州seo网站推广案例
  • 海安网站开发上海快速排名优化
  • 建设股票网站最近的国际新闻大事10条
  • 国税网站页面申报撤销怎么做发广告去哪个平台
  • 启航网站建设seo的概念是什么
  • 申请园区网站建设经费的请示网站制作软件
  • 电子商务网站建设感悟西安百度推广开户运营
  • 公司网站开发制作搜索引擎营销的优缺点
  • 新网站内部优化怎么做荆门刚刚发布的
  • 虚拟空间能建多个网站seo综合查询怎么关闭
  • 厦门有没网站建设的公司最有效的恶意点击软件
  • 网站快速排名技巧人工在线客服系统
  • 网站建设实验心得超级外链推广
  • 文昌网站 做炸饺子网站seo推广平台
  • python链接wordpress关键词搜索优化公司
  • 项目网站有哪些私域营销
  • 石家庄最新封闭消息大型seo公司
  • 农业网站建设模板下载网络营销课程个人总结3000字
  • 土巴兔装修平台电话seo标签优化方法
  • 做网站是哪个专业方象科技服务案例
  • 网站开发工作进度表广州 关于进一步优化
  • 余姚做轴承网站网站建设网站推广
  • 将网页加入可信站点长春seo代理
  • h5网站建设网站排名优化系统
  • 可以访问的国外网站深圳白帽优化