http://quqtalk.iteye.com/blog/360699
从事Java开发已经两年了,但是由于工作的关系,对Java Web还是个freshman。今天做了一个Java Web的简单Demo,对这个Demo的总结如下。
环境:
JDK:1.5.0_12-b04
Tomcat:apache-tomcat-6.0.18
MySQL:mysql-5.1.32-win32
这些软件可以从各自的官方网站上下载得到。
Demo制作过程:
(1)在Tomcat中配置MySQL数据源。
修改$CATALINA_HOME/conf目录中的context.xml,增加以下配置:
<Resource name="jdbc/mysqlDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysql?autoReconnect=true"/>
这里有个学习点,就是Resource 元素的各个属性分别代表什么含义?
(2)把包含MySQL的JDBC驱动的jar包放入到$CATALINA_HOME/lib目录。
(3)在$CATALINA_HOME/webapps目录,新建testjdbc目录,testjbdc的目录结构
+testjdbc/
|
|——+WEB-INF/
| |
| |——+lib/
| |
| |——+web.xml
|
|——+index.jsp
(4)index.jsp的内容:
- <span style=""><%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <sql:query var="rs" dataSource="jdbc/TestDB">
- select host, user, password from user
- </sql:query>
- <html>
- <head>
- <title>DB Test</title>
- </head>
- <body>
- <h2>Results</h2>
- <c:forEach var="row" items="${rs.rows}">
- Foo ${row.host}<br/>
- Bar ${row.user}<br/>
- </c:forEach>
- </body>
- </html>
- </span>
(5)web.xml内容:
- <span style=""><?xml version="1.0" encoding="ISO-8859-1"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <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_2_5.xsd"
- version="2.5">
- <display-name>Jdbc Test</display-name>
- <description>
- Tomcat Jdbc Test.
- </description>
- </web-app>
- </span>
(6)在testjdbc/WEB-INF/lib目录中放入 JSTL的jstl.jar和standard.jar,在Tomcat的document中,建议务必使用1.1.x release,可以从http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi得到。
(7)mysqld --console启动MySQL。
(8)$CATALINA_HOME/bin目录,startup.bat启动Tomcat。
(9)在浏览器地址栏敲入http://127.0.0.1:8080/testjdbc/可以看到从mysql库,user表取出的数据。
(10)在Tomcat主页可以进入管理页面,http://127.0.0.1:8080/manager/html,第一次进入时要求输入用户名和密码,Tomcat安装时,是没有用户名和密码的,修改$CATALINA_HOME/conf/tomcat-users.xml:
- <span style=""><?xml version='1.0' encoding='utf-8'?>
- <tomcat-users>
- <role rolename="manager"/>
- <role rolename="admin"/>
- <user username="admin" password="admin" roles="admin,manager"/>
- </tomcat-users>
- </span>
在管理页面的用户名密码框中填入admin/admin即可进入管理页面,看到部署过的所有app。