Tomcat6.0 配置
在成功安装JDK的前提下,安装tomcat6.0(假设安装路径为C:,一般选择FULL安装,4.0以上版本不用配置环境变量)系统变量中添加以下环境变量 ( 假定你的 j2sdk 安装在 c:"jdk1.6 ): JAVA_HOME= c:"jdk1.6。 接着可以启动 tomcat ,在 IE 中访问 http://localhost:8080 ,如果看到 tomcat 的欢迎页面的话说明安装成功了。然后对tomcat进行配置:
第一步:建立自己的 jsp app 目录:
1. 在 webapps 目录下新建一个目录,起名叫 myapp ;
2.myapp 下新建一个目录 WEB-INF ,注意,目录名称是区分大小写的;
3.WEB-INF 下新建一个文件 web.xml ,内容如下:
1
<?
xml version="1.0" encoding="ISO-8859-1"
?>
2
3 <! DOCTYPE web-app
4 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
5 "http://java.sun.com/dtd/web-app_2_3.dtd" >
6
7 < web-app >
8 < display-name > My Web Application </ display-name >
9 < description >
10 A application for test.
11 </ description >
12 </ web-app >
2
3 <! DOCTYPE web-app
4 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
5 "http://java.sun.com/dtd/web-app_2_3.dtd" >
6
7 < web-app >
8 < display-name > My Web Application </ display-name >
9 < description >
10 A application for test.
11 </ description >
12 </ web-app >
4 . 在 myapp 下新建一个测试的 jsp 页面,文件名为 index.jsp ,文件内容如下
1
<
html
>
2 < body >
3 < center >
4 Now time is: < %=new java.util.Date()% >
5 </ center >
6 </ body >
7 </ html >
5
.
重启
Tomcat
2 < body >
3 < center >
4 Now time is: < %=new java.util.Date()% >
5 </ center >
6 </ body >
7 </ html >
6. 打开浏览器,输入 http://localhost:8080/myapp/index.jsp 看到当前时间的话说明就成功了
第二步:建立自己的Servlet:
写入你的第一个Servlet:
在你新建的Application myapp/WEB-INF/classes/test目录下新建HelloWorld.java
1
package
test;
2
3 import java.io. * ;
4 import javax.servlet. * ;
5 import javax.servlet.http. * ;
6 public class HelloWorld extends HttpServlet
7 {
8 public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
9 {
10 response.setContentType( " text/html " );
11 PrintWriter out = response.getWriter();
12 out.println( " <html><head><title> " );
13 out.println( " This is my first Servlet " );
14 out.println( " </title></head><body> " );
15 out.println( " <h1>Hello,World!</h1> " );
16 out.println( " </body></html> " );
17
18 }
19 }
把Tomcat中lib里面的 servlet-api.jar 文件拷贝到C:"JDK"jre"lib"ext中,编译
HelloWorld.java2
3 import java.io. * ;
4 import javax.servlet. * ;
5 import javax.servlet.http. * ;
6 public class HelloWorld extends HttpServlet
7 {
8 public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
9 {
10 response.setContentType( " text/html " );
11 PrintWriter out = response.getWriter();
12 out.println( " <html><head><title> " );
13 out.println( " This is my first Servlet " );
14 out.println( " </title></head><body> " );
15 out.println( " <h1>Hello,World!</h1> " );
16 out.println( " </body></html> " );
17
18 }
19 }
Servlet必须使用C:"Tomcat"webapps"myapp"WEB-INF这个目录下面的web.xml文件进行注册,用EditPlus打开这个web.xml文件,在<web-app></web-app>添加下面这段程序:
1
package
test;
2 public class TestBean
3 {
4 private String name = null ;
5 public TestBean(String nameInit){
6 this .name = nameInit;
7 }
8 public void setName(String newName){
9 this .name = newName;
10 }
11 public String getName(){
12 return this .name;
13 }
14 }
然后照样用javac TestBean.java来编译这个文件。
2 public class TestBean
3 {
4 private String name = null ;
5 public TestBean(String nameInit){
6 this .name = nameInit;
7 }
8 public void setName(String newName){
9 this .name = newName;
10 }
11 public String getName(){
12 return this .name;
13 }
14 }
2.然后在你新建的应用程序目录myapp下新建一个新的jsp文件:testBean.jsp
1
<%
@ page import
=
"
test.TestBean
"
%>
2 < html >
3 < head >
4 < title > Test Bean </ title >
5 </ head >
6 < body >
7 < center >
8 <%
9 TestBean testBean = new TestBean( " Http://czl.cn " );
10 %>
11 Java Bean Test:
12 The author's blog address is <% = testBean.getName() %>
13 </ center >
14 </ body >
15 </ html >
确定各个文件的位置,如下
2 < html >
3 < head >
4 < title > Test Bean </ title >
5 </ head >
6 < body >
7 < center >
8 <%
9 TestBean testBean = new TestBean( " Http://czl.cn " );
10 %>
11 Java Bean Test:
12 The author's blog address is <% = testBean.getName() %>
13 </ center >
14 </ body >
15 </ html >
myapp"index.jsp
myapp"testBean.jsp
myapp"WEB-INF"web.xml
myapp"WEB-INF"classes"test"HelloWorld.class
myapp"WEB-INF"classes"test"TestBean.class
myapp"testBean.jsp
myapp"WEB-INF"web.xml
myapp"WEB-INF"classes"test"HelloWorld.class
myapp"WEB-INF"classes"test"TestBean.class
3.重启Tomcat如果需要的话,在浏览器输入:http://localhost:8080/myapp/testBean.jsp 幸运的话就会看到:
Java Bean Test: The author's blog address isHttp://czl.cn
这样就完成了整个Tomcat下的jsp、servlet和javabean的配置。第四步:配置虚拟目录
打开 Tomcat6.0"conf"server.xml 文件,在 <Host> 和 </Host> 之间加入
<
Context
path
="/myapp"
docBase
="D:"myapp"
debug
="0"
reloadable
="true"
crossContext
="true"
/>