Refer to "Lightweight JavaEE Enterprise Application Combat"
First download and install tomcat,
Configure Tomcat's data source
Tomcat provides two ways to configure data sources. The difference is that the access scope of the configured data sources is different: one data source can be accessed by all web applications and is called a global data source; the other can only be accessed by a single web application. In-application access is called a local data source. No matter which data source is configured, you need to provide a JDBC ( Java Data Base Connectivity, java database connection ) driver for a specific database. The MySQL database is used here, so you need to copy the MySQL JDBC driver to the lib path of Tomcat.
<%-- author LostTown version 1.0 --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@ page import="javax.naming.*,java.sql.*,javax.sql.*"%> <html> <head> <title> </title> </head> <body> <% //Initialize Context, use InitialContext to initialize Context Context ctx=new InitialContext(); /* Find the data source through JNDI, the JNDI is java:comp/env/jdbc/dstest, divided into two parts java:comp/env is fixed by Tomcat, and all JNDI bindings provided by Tomcat must be prefixed with this prefix jdbc/dstest is the data source name when defining the data source */ DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/ds"); //get database connection Connection conn=ds.getConnection(); //Get Statement Statement stmt=conn.createStatement(); //Execute the query and return the ResulteSet object ResultSet rs=stmt.executeQuery("select * from 101airinfo"); while(rs.next()) { out.println(rs.getString(2) + "<br/>"); } %> </body> </html>
<% Java script%>
<%@command%>
There are currently three directives: page, include and taglib, each with their own attributes.<%-- JSP comments--%> will not be output to the client
The page instruction is the most complex JSP instruction, and its main function is to set the properties and related functions of the entire JSP web page.
The include instruction means: insert a file containing text or code when JSP is compiled, the process of including is static,
and the included file can be a JSP web page, HTML web page, text file, or a Java program.
The taglib directive enables users to define new tags.
<!-- HTML comments--> will be output to the client
<%! Statement %>
<%! //declare an integer variable public int count; //declare a method public String info() { return "hello"; } %>
<%=expression%>
<%=count++%>
JSP script to embed Java code in HTML
<% for(int i = 0; i < 10; i++){ %> <tr> <td><%=i%></td> </tr> <% } %>