[Java EE] Tomcat internal connection pool

Source: Internet
Author: User

under the Meta-inf directory, create a new context.xml

Get the data source in the program, through jndi, this jndi must be available in the Servlet , and you need to configure Web. xml get the servlet to the data source as soon as it starts

Context.xml

<?XML version= "1.0" encoding= "Utf-8"?><Context>    <Resourcename= "MySource"type= "Javax.sql.DataSource"username= "root"Password= "root"Driverclassname= "Com.mysql.jdbc.Driver"URL= "Jdbc:mysql:///java"maxactive= "8"Maxidle= "4"    /></Context>
 PackageCom.tsh.web;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.Statement;ImportJavax.naming.Context;ImportJavax.naming.InitialContext;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJavax.sql.DataSource;/** */ Public classDatasourcetestextendsHttpServlet {/**     */     Publicdatasourcetest () {Super(); //TODO auto-generated Constructor stub    }    /**     */    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stub    }    /**     */    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {}/*** It will start when created*/@Override Public voidInit ()throwsservletexception {Try {            //Note the Guide pack javax.naming.Context;Context context=NewInitialContext (); Context Jndi= (Context) context.lookup ("Java:comp/env")); DataSource Source= (DataSource) jndi.lookup ("MySource"); //Note the Guide pack java.sql.Connection;Connection conn=source.getconnection (); //get The transmitter objectStatement statement=conn.createstatement (); //get result Set objectResultSet resultset=statement.executequery ("SELECT * from User"); //Traverse             while(Resultset.next ()) {String username=resultset.getstring ("username");            SYSTEM.OUT.PRINTLN (username); }            //Close ResourceResultset.close ();            Statement.close ();        Conn.close (); } Catch(Exception e) {e.printstacktrace (); }    }}

Xml

    <servlet>        <Servlet-name>Datasourcetest</Servlet-name>        <Servlet-class>Com.tsh.web.DataSourceTest</Servlet-class>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Datasourcetest</Servlet-name>        <Url-pattern>/servlet/datasourcetest</Url-pattern>    </servlet-mapping>

[Java EE] Tomcat internal connection pool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.