Servlet( ServletConfig & ServletContext)

开发第一个Servlet入门应用

打开myeclipse,新建一个web工程,名为day04,在src目录下新建一个包为it.cast,如下:

 



 

同时在web.xml文件中会自动产生代码:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <servlet>

    <servlet-name>ServletDemo1</servlet-name>

    <servlet-class>cn.itcast.ServletDemo1</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>ServletDemo1</servlet-name>

    <url-pattern>/servlet/ServletDemo1</url-pattern>

  </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

启动tomcat,在IE中输入http://localhost:8080/day05/servlet/ServletDemo1,得到Hello Httpservlet!!!

 

从上述的入门案例来看servlet的UML图是如何访问



 

由图中可以看出servlet的生命周期:

 

Servlet的整个生命周期内,Servletinit方法只被调用一次。而对一个Servlet的每次访问请求都导致Servlet引擎调用一次servletservice方法。对于每次访问请求,Servlet引擎都会创建一个新的HttpServletRequest请求对象和一个新的HttpServletResponse响应对象,然后将这两个对象作为参数传递给它调用的Servletservice()方法,service方法再根据请求方式分别调用doXXX方法。

2 针对客户端的多次Servlet请求,通常情况下,服务器只会创建一个Servlet实例对象,也就是说Servlet实例对象一旦创建,它就会驻留在内存中,为后续的其它请求服务,直至web容器退出,servlet实例对象才会销毁。

 

ServletConfig

Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。

servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servletinit方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。

 

案例: 获取WEB应用的初始化参数。

假如:在web中配置文件,如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <servlet>

    <servlet-name>ServletDemo3</servlet-name>

    <servlet-class>cn.itcast.ServletDemo3</servlet-class>

    <init-param>

    <param-name>data</param-name>

    <param-value>xxxx</param-value>

    </init-param>

  </servlet>

  <servlet-mapping>

    <servlet-name>ServletDemo3</servlet-name>

    <url-pattern>/servlet/ServletDemo3</url-pattern>

  </servlet-mapping>

</web-app>

 

第一种:

 

package cn.itcast;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.jsp.jstl.core.Config;

public class ServletDemo3 extends HttpServlet {

         private ServletConfig config;

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

       String value=config.getInitParameter("data");

      System.out.println(value);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

      doGet(request, response);

}

@Override(右击àSourceàOverride/Implement MethodsàGenericServletàinit(String,Throwable)àok形成)

public void init(ServletConfig config) throws ServletException {

// TODO Auto-generated method stub

      this.config=config;

}

}Console中就拿到了xxx       

 

第二种:

 

package cn.itcast;

import java.io.IOException;……………….等

public class ServletDemo3 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

     String value=this.getServletConfig().getInitParameter("data");

    System.out.println(value);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

     doGet(request, response);

}

}Console中业可以拿到了xxx

 

当有多个参数的时候:下面有得到指定的和得到所有的。

 

//得到指定的

String value=this.getServletConfig().getInitParameter("data1");

System.out.println(value);

//得到所有的

Enumeration e=this.getServletConfig().getInitParameterNames();

while(e.hasMoreElements()){

String name=(String)e.nextElement();

String value1=this.getServletConfig().getInitParameter(name);

System.out.println(name+"="+value1);

}

 

阅读ServletConfig API,一般web.xml文件用来:

• 获得字符集编码

• 获得数据库连接信息

• 获得配置文件,查看struts案例的web.xml文件

 

 

 

ServletContext

WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。

2 ServletContext对象被包含在ServletConfig对象中,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得对ServletContext对象的引用。

两种得到ServletContext的方式:

 

//得到ServletContext方式1

ServletContext context=this.getServletConfig().getServletContext();

//得到ServletContext方式2

context=this.getServletContext();

 

3 由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。(requestsessionpage

4 查看ServletContext API文档,了解ServletContext对象的功能。

 

应用: 1、多个Servlet通过ServletContext对象实现数据共享。

先吧数据存在servletContext域里面

 

          String date="aaa";

          this.getServletContext().setAttribute("date", date);

   然后在另一个servlet类去共享,并且输出来

 

          String value=(String) this.getServletContext().getAttribute("date");

          System.out.println(value);

 


 

2、利用ServletContext对象读取资源文件。

• 得到文件路径

• 读取资源文件的三种方式

• .properties文件(属性文件)

cn.itcast包里,新建一个文件db.properties,注意这个文件会建在与servlet程序同目录下

有两种资源文件(properties(没有关系的)xml(配置文件)

url=jdbc:mysql://localhost:3306/test

username=root

password=root

 

 

package cn.itcast;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.util.Properties;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ServletDemo9 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/db.properties");

Properties props=new Properties();

props.load(in);

String url=props.getProperty("url");

String username=props.getProperty("username");

String password=props.getProperty("password");

System.out.println(url);

System.out.println(username);

System.out.println(password);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

IE中输入http://localhost:8080/day05/servlet/ServletDemo9获取,在Console下打印出

jdbc:mysql://localhost:3306/test

root

root


假如资源文件在src下另一个包里,它映射的地址是

InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes /db.properties");

假如资源文件在webRoot目录下

InputStream in=this.getServletContext().getResourceAsStream("/db.properties");

 


 

package cn.itcast;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Properties;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

//读取资源文件

public class ServletDemo10 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//通过servletContextgetRealPath得到资源的绝对路径后,再通过传统流读取资源文件

String path=this.getServletContext().getRealPath("/WEB-INF/classes/cn/itcast/db.properties");

String filename=path.substring(path.lastIndexOf("\\")+1);

System.out.println("当前读取到的资源名称是:"+filename);

FileInputStream in =new FileInputStream(path);

Properties props=new Properties();

props.load(in);

String url=props.getProperty("url");

String username=props.getProperty("username");

String password=props.getProperty("password");

System.out.println(url);

System.out.println(username);

System.out.println(password);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}得到:

当前读取到的资源名称是:db.properties

jdbc:mysql://localhost:3306/test

root

root


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值