基础原理

    技术2022-05-11  11

    1 jsp生命周期:

       1 jspinit jsp容器创建一个对象的时候,执行jspinit方法,该方法在jsp的生命周期中只执行一次。

       2 jspService  jsp容器处理客户请求的时候,调用这个方法,对于每一个客户的请求,jsp容器新建一个线程来处理。

       3 jspDestory方法 由于servlet常驻内存,所以jsp响应速度快。当系统资源不足的时候,需要将Servlet移出内存,此时执行jspDestory方法

     

    2 连接池: 连接池“借出”连接时,该连接仅供请求它的线程使用

       1 缩短了连接创建时间   2 简化的编程模型

      3 受控的资源使用

     

      创建 连接池:

      Server.xml 

      <Host>  <Context path="/Test" doBase="Test" debug="10" reloadable="true">         <Resource  name="jndi/ds/mysql" type="javax.sql.DataSource"

        auth="Container" maxVactive="10"   maxIdle="2" maxWait="5000"

        driverClassName="com.mysql.jdbc.Driver"

        url="jdbc:mysql://localhost:3306/mytest?characterEncoding=gbk"    username="root" password="sa"/>  </Context>  </Host>

      

      //context会被每个工程都加载

      context.xml

       <Context>   <WatchedResource>WEB-INF/web.xml</WatchedResource>    <Manager pathname="" />    <ResourceLink  name="jndi/ds/mysql" global="jndi/ds/mysql"

           type="javax.sql.DataSourcer"/>   </Context>

       //web.xml

      <resource-ref>       <res-ref-name>jndi/ds/mysql</res-ref-name>       <res-type>javax.sql.DataSource</res-type>       <res-auth>Container</res-auth>    </resource-ref>

      

      //使用连接池:

     

      直接使用:

      DataSource ds = null;       InitialContext ctx=new InitialContext();      ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mssql");      Connection conn = ds.getConnection();      Statement stmt = conn.createStatement();      String strSql = " select * from ttt";      ResultSet rs = stmt.executeQuery(strSql);   

      hibernate中使用:

         <hibernate-configuration>        <session-factory>          <property name="dialect"> net.sf.hibernate.dialect.MySQLDialect</property>          <property name="connection.datasource">java:comp/env/jdbc/mssql</property>          <mapping resource="cn/edu/ynu/db/User.hbm.xml" />  </session-factory>  </hibernate-configuration>

       Spring中使用:

       <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">     <property name="jndiName" value="java:comp/env/jdbc/mssql"/>  </bean>

     

     


    最新回复(0)