转: java web demo的示例

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的内容:

Html代码  收藏代码
  1. <span style=""><%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  3.   
  4. <sql:query var="rs" dataSource="jdbc/TestDB">  
  5. select host, user, password from user  
  6. </sql:query>  
  7.   
  8. <html>  
  9.   <head>  
  10.     <title>DB Test</title>  
  11.   </head>  
  12.   <body>  
  13.   
  14.   <h2>Results</h2>  
  15.     
  16. <c:forEach var="row" items="${rs.rows}">  
  17.     Foo ${row.host}<br/>  
  18.     Bar ${row.user}<br/>  
  19. </c:forEach>  
  20.   
  21.   </body>  
  22. </html>  
  23.   
  24.    
  25. </span>  

(5)web.xml内容:

Xml代码  收藏代码
  1. <span style=""><?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!--  
  3.   Licensed to the Apache Software Foundation (ASF) under one or more  
  4.   contributor license agreements.  See the NOTICE file distributed with  
  5.   this work for additional information regarding copyright ownership.  
  6.   The ASF licenses this file to You under the Apache License, Version 2.0  
  7.   (the "License"); you may not use this file except in compliance with  
  8.   the License.  You may obtain a copy of the License at  
  9.   
  10.       http://www.apache.org/licenses/LICENSE-2.0  
  11.   
  12.   Unless required by applicable law or agreed to in writing, software  
  13.   distributed under the License is distributed on an "AS IS" BASIS,  
  14.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15.   See the License for the specific language governing permissions and  
  16.   limitations under the License.  
  17. -->  
  18. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  19.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  20.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  21.    version="2.5">   
  22.   
  23.   <display-name>Jdbc Test</display-name>  
  24.   <description>  
  25.      Tomcat Jdbc Test.  
  26.   </description>  
  27. </web-app>  
  28. </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:

Xml代码  收藏代码
  1. <span style=""><?xml version='1.0' encoding='utf-8'?>  
  2. <tomcat-users>  
  3.   <role rolename="manager"/>  
  4.   <role rolename="admin"/>  
  5.   <user username="admin" password="admin" roles="admin,manager"/>  
  6. </tomcat-users>  
  7. </span>  

 在管理页面的用户名密码框中填入admin/admin即可进入管理页面,看到部署过的所有app。

posted @ 2016-04-21 00:17  跬步者  阅读(2589)  评论(0编辑  收藏  举报