Configure and use JNDI in Tomcat

xiaoxiao2021-03-06  19

Tomcat is configured and JNDI JNDI is a very important standard in J2EE. Usually we are used in EJB programming. Tomcat4.0 provides a method of using JNDI directly in JSP and Servelt, talk about Tomcat4.0 Methods for configuring and using JNDI (Take the JNDI connection database as an example) assume that the database is mysql, the experiment example is in the Tomcat_Home / WebApps / dbtest directory A. MM.Mysql's JDBC connection library mm.mysql-2.0.9-bin .jar is put in Tomcat_Home / Common / Lib B. Configure tomcat_home / conf / serer.xml file

Add a context:

This is the root path of dbtest, which is prepared in dbtest. C. Added on it

Segment join

Factory

Org.apache.commons.dbcp.basicDataSourceFactory

Maxactive

100

MaxIdle

30

Maxwait

10,000

Username

Test

Password

Test

Driverclassname

Org.gjt.mm.mysql.driver

URL

JDBC: MySQL: // localhost: 3306 / test

There are English annotations here, which is provided by Tomcat, we can join in Sample, mainly modified DriverClassName, URL, and user account; need to emphasize "JDBC / TestDB" is the Name of JDNI to find. D Using JSPH or Servlet The following is the code in the JSP file for JSP files (files name userHandledb.jsp) Need to note that JNDI Name is to be added "Java: Comp / ENV /" < % @ page language = "java"%> <% @ page import = "java.util. *"%> <% @ page import = "java.sql. *"%> <% @ Page Import = "javax.sql * "%> <% @ page import =" javax.naming. * "%> <% string jndi_name =" java: comp / env / jdbc / testdb "; string select_user_sql =" SELECT Userid, Name, Birthday, Email from EMP "; string colnames [] [] = {{" User ID "," name "," birth day "," email "}, {" userid "," name "," birthday "," email "}}; Vector userSet = new vector (); vector columnset = new vector (); for (int i = 0; i

E. Quote UserHandLeDb.jsp (recorded as viewtable.jsp)

Test Database <% @ include file = "UserHandledb.jsp"%>

"); for (int J = 0; J

" COL " ");} out.print (" ");}%>

<% for (int i = 0; i

F. Join DB Connection in Web.xml

JDBC / TESTDB

Javax.sql.datasource

Container

The Name of the JDBC / TESTDB and the Name of the resource segment in C, the observation first determines that the database has been started, then start Tomcat, if Tomcat starts an exception, the possible reason is that the database's JDBC library does not load finally open the browser, access HTTP : // localhost: 8080 / dbtest / viewtable.jsp You can see the result

转载请注明原文地址:https://www.9cbs.com/read-43835.html

New Post(0)