Tomcat6.0 配置
??? 在成功安裝JDK的前提下,安裝tomcat6.0(假設(shè)安裝路徑為C:,一般選擇FULL安裝,4.0以上版本不用配置環(huán)境變量)系統(tǒng)變量中添加以下環(huán)境變量(假定你的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,注意,目錄名稱是區(qū)分大小寫的;?
??????? 3.WEB-INF下新建一個文件web.xml,內(nèi)容如下:?
???
?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,文件內(nèi)容如下
?
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中l(wèi)ib里面的servlet-api.jar文件拷貝到C:"JDK"jre"lib"ext中,編譯HelloWorld.java?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????}
??? 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"?/>
posted on 2008-09-04 15:43 nonels 閱讀(670) 評論(0) 編輯 收藏 所屬分類: J2EE