今天是三月20号,离月底还有11天,感觉是有点赶不上进度了,加上这两天的毕业设计也耽误了一些时间,所以时间很仓促。
day05的内容主要是servlet开发,servlet是sun发布的一款用于开发动态web资源的技术,本质是一个接口。一般的servlet继承的是HttpServlet类,覆盖其service方法,在实现get请求时,使用doGet()方法,在实现post请求时,使用doPost()请求。所以一般是直接覆盖doPost()或者doGet()。
servlet有几个细节:
(1)servlet在创建时,WEB-INF中会自动创建其映射例如:
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>cn.iatcast.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
一般约定,<servlet-name>标签与类名相同,<url-pattern>标签中为/servlet/类名
(2)servlet映射过程中有两种url通配方式,一种是*.扩展名,一种是/*,在访问时,一般长得像为优先以及/为优先。
(3)servlet实例对象一旦创立,它就会一直在内存中,直到web容器退出,他才被销毁。
(4)如果一个servlet的映射路径仅为一个/,那么这个servlet就成为当前web应用程序的缺省servlet
接下来衍生讲了线程安全问题,synchronized是Java语言的关键字,可用来给对象和方法或者代码块加锁,当它锁定一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码。 synchronized(this)代表锁定当前对象。
ServletConfig与ServletContext
ServletConfig内容较少,主要讲了使用<init-param>配置初始化参数,然后通过ServletConfig的方法来获取参数:在web.xml中配置如下:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletConfigDemo1</servlet-name>
<servlet-class>cn.itcast.servletconfig.ServletConfigDemo1</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>crush</param-value>
</init-param>
</servlet>
Servlet中获取如下:
String username = this.getServletConfig().getInitParameter("username");
ServletContext为今天内容的重中之重。web容器在启动时,会为每个WEB应用程序创建一个对应的ServletContext对象,代表当前的web应用。通过ServletConfig.getServletContext()来获取ServletContext对象,一个web中的所有的servlet共享一个ServletContext对象。
ServletContext应用如下:
(1)获得整个web站点的初始参数
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
String url = (String) this.getServletConfig().getServletContext().getInitParameter("url")
(2)多个servlet通过servletContext实现数据共享
public class ServletContextDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = "crush";
ServletContext context = this.getServletConfig().getServletContext();
context.setAttribute("data", data); //map
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public class ServletContextDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
String data = (String) context.getAttribute("username");
System.out.println(username);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
(3)转发功能(请求重定向)
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = this.getServletConfig().getServletContext().getRequestDispatcher("/servlet/ServletContextDemo5");
rd.forward(request, response);
}
(4)利用ServletContext获得资源文件
private void test2() throws IOException{
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty("driver");
System.out.println(driver);
//读取web工程中资源文件的模板代码
}
private void test3() throws IOException {
//获取web资源的绝对路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream in = new FileInputStream(path);
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty("driver");
System.out.println(driver);
}
(5)利用类装载器读取资源
private void test1() throws IOException {
//获取到装载当前类的类装载器
ClassLoader loader = ServletContextDemo7.class.getClassLoader();
//用类装载器装载db.properties文件
InputStream in = loader.getResourceAsStream("db.properties");
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(driver);
}//通过类装载器读取资源文件的注意事项:不适合装载大文件,否则会导致jvm内存溢出
(6)读取大文件的方法,此例中使用一个100+m的视频做示例
//文件太大,只能用servletContext
public void test4() throws IOException{
//读取pokerface.mp4,并拷贝到e:\根目录下
//path=c:\asdf\adsd\add\PranavMistry_2009I_480.mp4
//path=PranavMistry_2009I_480.mp4
获取文件名
String path = this.getServletContext().getRealPath("/WEB-INF/classes/pokerface.mp4");
String filename = path.substring(path.lastIndexOf("\\")+1);//分割出文件名,天才
//读文件模板代码
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/pokerface.mp4");
byte buffer[] = new byte[1024];
int len = 0;
FileOutputStream out = new FileOutputStream("e:\\" + filename);
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
out.close();
in.close();
}
所以day05内容就是这些,累了,洗洗睡。