3
: -,
:
1. .
2. ,
, .
3. , .
NetBeans Java EE, JSP .
Apache Tomcat. Windows, . -, ; - . / .
: apache-tomcat-7.0.12.zip apache-tomcat-7.0.12 . Apache Tomcat Java - (JDK), JAVA_HOME. :
c:\apache-tomcat-7.0.12\bin>echo %JAVA_HOME%
c:\Java\jdk1.6.0_20
c:\Java\jdk1.6.0_20 bin.
: startup.bat c:\apache-tomcat-7.0.12\bin, :
Using CATALINA_BASE: "C:\apache-tomcat-7.0.12"
Using CATALINA_HOME: "C:\apache-tomcat-7.0.12"
Using CATALINA_TMPDIR: "C:\apache-tomcat-7.0.12\temp"
Using JRE_HOME: "c:\Java\jdk1.6.0_20"
Using CLASSPATH: "C:\apache-tomcat-7.0.12\bin\bootstrap.jar;C:\apache-tomc
at-7.0.12\bin\tomcat-juli.jar"
1 ; 2 http://127.0.0.1:8080; 3 ; 4 Manager App; 5 JSP; 6 tomcat, , http://127.0.0.1:8080/test1/; 7 ; 8 , search Organization; 9 - http://127.0.0.1:8080/test1/org_search.jsp; 10 ; 11 .
. :
1 -
, 8080. conf\server.xml:
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at:
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
|
|
connectionTimeout="20000"
redirectPort="8443" />
:
2 http://127.0.0.1:8080
, .
Apache Tomcat , .. manager application. Manager App. :
3
:
apache-tomcat-7.0.12\webapps\manager\WEB-INF, web.xml, manager.
<security-constraint>
<web-resource-collection>
<web-resource-name>HTML Manager interface (for humans)</web-resource-name>
<url-pattern>/html/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>manager-gui</role-name>
</auth-constraint>
</security-constraint>
, manager-gui.
apache-tomcat-7.0.12\conf tomcat-users.xml, . xml -
<role rolename=manager-gui/> <user username=manager password=manager roles=manager-gui/>
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="manager" password="manager" roles="manager-gui"/>
</tomcat-users>
manager/manager Manager App:
4 Manager App
Manager Server Status .
Applications -, , :
Start , ;
Stop . ;
Reload ;
Undeploy ;
Expire sessions with idle >= minutes - - .
Deploy war -.
Diagnostics JVM.
Host Manager, , : http://127.0.0.1:8080/manager/html. , , .
( JSP ) tomcat IDE :
|
|
1. tomcat apache-tomcat-7.0.12\webapps , test1.
2. test1 index.jsp :
<html>
<head><title>Test 1</title></head>
<body>
<table border=1>
<% for(int i=0;i<5;i++)
{
%>
<tr><td>record number <%=i%></td></tr>
<%
}
%>
<table>
</body>
</html>
3. , http://127.0.0.1:8080/test1 / :
5 JSP
JSP, , .
main ( DriverManager.getConnection()), , . - :
1. . http - -, .
2. http -, - , .
, . , . http - . http - ( ). , , , , , . ( ).
tomcat, http://127.0.0.1:8080/docs/jndi-datasource-examples-howto.html
Host Manager (http://127.0.0.1:8080/host-manager/html). . .
1. JDBC - ( classes12.jar Oracle) apache-tomcat-7.0.12\lib. zip, jar.
2. server.xml <GlobalNamingResources> :
<Resource name="jdbc/oracle_itc2011" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:FMS"
username="itc2011" password="itc2011" maxActive="20" maxIdle="10"
maxWait="-1"/>
3. webapps\test1 WEB-INF, web.xml :
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "
|
|
version="3.0"
metadata-complete="true">
<display-name>A test application</display-name>
<description>
A scriptable management web application for the Tomcat Web Server;
Manager lets you view, load/unload/etc particular web applications.
</description>
<resource-ref>
<description>Oracle itc2011 Datasource</description>
<res-ref-name>jdbc/oracle_itc2011</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
apache-tomcat-7.0.12\webapps\manager\WEB-INF\web.xml .
4. WEB-INF test1\META-INF, context.xml :
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/test1" docBase="test1" debug="1" reloadable="true">
<ResourceLink global="jdbc/oracle_itc2011" name="jdbc/oracle_itc2011" type="javax.sql.DataSource"/>
</Context>
, .
5. index.jsp , :
<%@page import="javax.naming.*, javax.sql.*, java.sql.*" contentType="text/html; charset=Cp1251"%>
<html>
<head><title>Test 1</title></head>
<body>
<table border=1>
<%
// JNDI -
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
//
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle_itc2011");
// -
Connection conn = ds.getConnection();
//
Statement stmt=conn.createStatement();
//
ResultSet rs=stmt.executeQuery("select organization_id,name from organization order by organization_id");
//
while(rs.next())
{
%>
<tr>
<td><%=rs.getInt("ORGANIZATION_ID")%></td>
<td><%=rs.getString("NAME")%></td>
</tr>
<%
}
// -
stmt.close();
rs.close();
// ,
conn.close();
%>
</table>
</body>
</html>
6. tomcat, , http://127.0.0.1:8080/test1/, :
6 tomcat, , http://127.0.0.1:8080/test1/
JSP -
, JSP -, , Tomcat.
1. WEB-INF classes.
2. . ( org_po), , :
7
3. org_search.jsp - test1. , Main.main(). org_search.jsp:
<%@page import="javax.naming.*, javax.sql.*, java.sql.*, org_po.*" contentType="text/html; charset=Cp1251"%>
|
|
<jsp:useBean class="org_po.Organization" id="org" scope="session" />
<html>
<head><title>Test 1</title></head>
<body>
<table border=1>
<%
// JNDI -
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
//
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle_itc2011");
// -
Connection conn = ds.getConnection();
// ( null )
Organization[] orgs=org.search(conn, null, null);
out.println(orgs.length+" . ");
// , .
for(int i=0;i<orgs.length;i++)
{
System.out.println(orgs[i].name+" "+orgs[i].registration_num);
%>
<tr>
<td><%=orgs[i].name%></td>
<td><%=orgs[i].registration_num%></td>
</tr>
<%
}
// ,
conn.close();
%>
</table>
</body>
</html
4. , http://127.0.0.1:8080/test1/org_search. jsp, :
8 , search Organization.
JSP -
org_search.jsp . :
<%@page import="javax.naming.*, javax.sql.*, java.sql.*, org_po.*" contentType="text/html; charset=Cp1251"%>
<jsp:useBean class="org_po.Organization" id="org" scope="session" />
<html>
<head><title> </title>
<link rel=stylesheet href="sample.css"/>
</head>
<body>
<%--
, 2 input text,
: org_name org_reg_num. .
form action, .
--%>
<form method="get">
<input type=text name="org_name" value="<%=request.getParameter("org_name")%>"/>
<input type=text name="org_reg_num" value="<%=request.getParameter("org_reg_num")%>"/>
<input type=submit value=" ">
</form>
<table>
<%
// JNDI -
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
//
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle_itc2011");
// -
Connection conn = ds.getConnection();
// ,
// http- request getParameter.
// .
Organization [] orgs=org.search(conn, request.getParameter("org_name"), request.getParameter("org_reg_num"));
%>
<tr>
<th colspan="3"><%=" : "+orgs.length%></th>
</tr>
<tr class="yellow">
<td> </td>
<td> </td>
<td> </td>
</tr>
<%
// , .
for(int i=0;i<orgs.length;i++)
{
System.out.println(orgs[i].name+" "+orgs[i].registration_num);
%>
<tr>
<td><%=orgs[i].name%></td>
<td><%=orgs[i].registration_num%></td>
<td><%=orgs[i].vat_num%></td>
</tr>
<%
}
// ,
conn.close();
%>
</table>
</body>
</html>
, apache-tomcat-7.0.12\conf\server.xml, Connector URIEncoding Cp1251:
|
|
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="Cp1251"
/>
http://127.0.0.1:8080/test1/org_search.jsp:
9 - http://127.0.0.1:8080/test1/org_search.jsp
:
10
():
11
: -, ; ; , , ; , .
.. | ||
.. |