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

  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. <sql:query var="rs" dataSource="jdbc/TestDB">
  4. select host, user, password from user
  5. </sql:query>
  6. <html>
  7. <head>
  8. <title>DB Test</title>
  9. </head>
  10. <body>
  11. <h2>Results</h2>
  12. <c:forEach var="row" items="${rs.rows}">
  13. Foo ${row.host}<br/>
  14. Bar ${row.user}<br/>
  15. </c:forEach>
  16. </body>
  17. </html>
  18. </span>

(5)web.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. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  17. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  19. version="2.5">
  20. <display-name>Jdbc Test</display-name>
  21. <description>
  22. Tomcat Jdbc Test.
  23. </description>
  24. </web-app>
  25. </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:

  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。

最新文章

  1. .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  2. Apache 配置参考
  3. HDU-4533 威威猫系列故事——晒被子(区间更新)
  4. ChesFrame框架介绍
  5. xdu_RainAndBow 鞍山打铁记
  6. java读写文件
  7. Client与Server
  8. Bootstrap记录
  9. JavaScript第三课 (循环)
  10. TCP/IP入门(4) --应用层
  11. Diango 框架起步
  12. iptables(1)
  13. pythonのsqlalchemy简单查询
  14. fcn+caffe+voc2012实验记录
  15. 实例,C# 导出.dbf格式文件
  16. android check box 自定义图片
  17. [转]将字体嵌入程序资源中 C# Winform
  18. Pwn Heap With Tcache
  19. [2017BUAA软工]第2次个人作业
  20. eclipse中maven项目部署到tomcat [转]

热门文章

  1. reduce实现数组求和
  2. Java并发容器--ConcurrentHashMap
  3. 一种机制,与js类似
  4. 用MysQL语句怎么进行远程连接数据库
  5. gdb 打印内存 x
  6. 解析Linux内核的基本的模块管理与时间管理操作---超时处理【转】
  7. python--线程池(concurrent.futures)
  8. APscheduler总结
  9. poj 3026(BFS+最小生成树)
  10. [BZOJ2084][Poi2010]Antisymmetry 二分+hash