Configuration of Jndi in Java Web Engineering

Source: Internet
Author: User

The first step: Configure Web. xml

<resource-ref> <description>db connection</description><res-ref-name>test_datasource< /res-ref-name><res-type>javax.sql.datasource</res-type><res-auth>container</res-auth ></resource-ref>


Step Two: Configure the Server.xml for Tomcat (the following Oracle, for example, other DB equivalent)

<context docbase= "E:\dev\apps\apache-tomcat-7.0.42\webapps\Test" path= "/test" reloadable= "true" source= " Org.eclipse.jst.jee.server:Test "><resource name=" Test_datasource "auth=" Container "type=" Javax.sql.Da Tasource "Driverclassname=" Oracle.jdbc.driver.OracleDriver "url=" Jdbc:oracle:thin: @IP: Port: Database name "Username=" with User name "password=" password "maxactive=" maxidle= "2" maxwait= "1"/></context>


Step three: Paste the sample code for my Project Jndi section

static datasource getdatasource (String dsName)  {     DataSource ds = null;    try {       if  (Serverdetector.istomcat ())  {         dsName =  "java:comp/env/"  + dsName;      }       ds =  (DataSource) Jndiutil.getinitialcontext (). Lookup (DSName);     } catch  (namingexception e)  {      throw  New dataaccessexception (e);    }    if  (null == ds )  {      throw new dataaccessexception ("data source["  +  dsName +  "] is null");    }    return  ds;  } 


Precautions:

1, the above data source name Test_datasource must be consistent!

2, Server.xml in the configuration of the content, will be myeclipse to perform clean operation of the time to enforce the operation of restoring server.xml content, be sure to remember to reconfigure!





This article is from the "You can choose Extraordinary" blog, please be sure to keep this source http://ylcodes01.blog.51cto.com/5607366/1916643

Configuration of Jndi in Java Web Engineering

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.