java 培训日记

一、servlet的service()方法
回顾Servlet的生命周期,通过查看源代码了解init()及service()方法的设计思想;
问:
(1)我们在编写Servlet类时,为什么不覆盖它的service()方法,通常覆盖doGet()之类的方法?
如果用覆盖servlet的service()方法,那么servlet在处理表单提交时,就会去调用父类的service方法,父类的service方法会根据提交的方式做出判断,然后调用对应的doXX()方法。如果是覆盖doGet()之类的方法,servlet就会根据提交的方式直接调用相应的doXX()方法。
(2)如果要覆盖service()方法,通常会覆盖下列方法中的哪一个方法?
service(HttpServletRequest req, HttpServletResponse resp)
service(ServletRequest req, ServletResponse res)

二、浏览器缓存内幕及解决方案
在下面三种不同的情况下,浏览器的缓存情况不同:
1).Servlet没有覆盖getLastModified方法,响应消息中无LastModified头字段,在浏览器缓存的文档无“上次修改时间”.
2).有getLastModified方法,响应消息中有LastModified头字段,但返回时间晚于缓存文档“上次修改时间”
3).有getLastModified方法,响应消息中有LastModified头字段,但返回时间早于等于缓存文档“上次修改时间”
后退、前进、转到(手工输入后按回车) 通过超链接访问 刷新
1)不发请求,直接使用缓存 发出请求 发出请求
2)不发请求,直接使用缓存 不发请求 发出请求,返回200
3)不发请求,直接使用缓存 不发请求 发出请求,返回304
2、如何禁止Servlet的缓存?(张老师JavaWeb书第236页)
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
3、如何禁止JSP页面的缓存?
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

4、如何禁止静态页面的缓存?(张老师JavaWeb书第238页)
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
静态页面被禁止后,刷新浏览器返回304
JSP页面被禁止后,刷新浏览器返回200
三、初始化参数
1、相对于单个Servlet:P178
在web.xml文件中配置
<servlet>
<servlet-name>ReadInitParam</servlet-name>
<servlet-class>servlet.ReadInitParam</servlet-class>
<init-param>
<param-name>dbname</param-name>
<param-value>mysql</param-value>
</init-param>
</servlet>
访问方法为:
getServletConfig().getInitParameter("keyname")
2、相对于整个Web应用程序:P201
在web.xml文件中配置
<context-param>
<param-name>company</param-name>
<param-value>itcast.cn</param-value>
</context-param>
访问方法为:
getServletContext().getInitParameter("keyname")

四、资源文件(.properties)的访问
1、使用Properties类来访问:
Properties prop = new Properties();

//方法一
//InputStream is = new FileInputStream("db.properties");
//db.properties要放在启动tomcat的bin目录中

//方法二
//InputStream is = new FileInputStream(this.getServletContext().getRealPath("/") + "WEB-INF/classes/config/db.properties");
//db.properties可以放在web工程的任意目录

//方法三
//InputStream is = this.getServletContext().getResourceAsStream("WEB-INF/classes/config/db.properties");
//db.properties可以放在web工程的任意目录,相对于工程的上下文路径

//方法四
//InputStream is = this.getClass().getClassLoader().getResourceAsStream("/config/db.properties");
InputStream is = ReadProp.class.getClassLoader().getResourceAsStream("/config/db.properties");
//db.properties只能放在类路径即classes目录中
prop.load(is);
is.close();
//读取key对应的value
prop.getProperty("keyname")

2、使用ResourceBundle类来访问:
//也只能放在类路径下
ResourceBundle rb = ResourceBundle.getBundle("config/db");
//读取key对应的value
rb.getString("keyname")

[url]http://www.itcast.cn[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值