On the internet to check a lot, and finally realized. Let's write the procedure below:
First, create a file context.xml under Webroot/meta-inf, with the following:
<?XML version= "1.0" encoding= "UTF-8"?> <Context> <Resourcename= "Jdbc/connectionpool"Auth= "Application"type= "Javax.sql.DataSource"username= "root"Password= "root"Driverclassname= "Com.mysql.jdbc.Driver"URL= "Jdbc:mysql://localhost:3306/sow"maxactive= "8"Maxidle= "4"/> </Context>
Where the Name property is the names of the database connection pool, the Auth property is the scope of the database connection pool, application for this web project, and the following sow for the URL attribute is the database name;
Next, add the configuration in Web. xml:
<Resource-ref> <Description>News DataSource</Description> <Res-ref-name>Jdbc/connectionpool</Res-ref-name> <Res-type>Javax.sql.DataSource</Res-type> <Res-auth>Application</Res-auth></Resource-ref>
Note that the attributes inside are consistent.
Next, the new myjsp.jsp is used as a test
<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><%@ pageImport= "java.sql.*"%><%@ pageImport= "javax.naming.*"%><%@ pageImport= "Javax.sql.DataSource"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Context Initctx=NewInitialContext (); Context CTX= (Context) initctx.lookup ("Java:comp/env")); //Get Connection Pool objectDataSource ds = (DataSource) ctx.lookup ("Jdbc/connectionpool"); //Create a connectionConnection conn =ds.getconnection (); System.out.println ("Conn=" +conn); Statement stmt=conn.createstatement (); String MySQL= "SELECT * FROM NOTIFICATION_TB"; ResultSet myrs=stmt.executequery (MySQL);//Execute Query%> <body>This is my JSP page.<br> <table> <% while(Myrs.next ()) {%> <%String Notitheme=myrs.getstring ("Notitheme"); String noticontent=myrs.getstring ("Noticontent"); %> <tr><td><%=notitheme%></td><td><%=noticontent%></td></tr > <%System.out.println (Notitheme); System.out.println (noticontent); %> <% } %> </table> </body>Then reboot Tomcat and publish your website to access http://localhost:8080/PW_Application/MyJsp.jsp to see the results.
MyEclipse Web project JSP using a JNDI database connection pool to connect to the MySQL database