| Tomcat+JSP经典配置实例 |
| |
发布者: 发布时间:2007-01-08 |
|
|
|
经常看到jsp的初学者问tomcat下如何配置jsp、servlet和bean的问题,于是总结了一下如何tomcat下配置jsp、servlet和ben,希望对那些初学者有所帮助。 一、开发环境配置 第一步:下载j2sdk和tomcat:到sun官方站(http://java.sun.com/j2se/1.5.0/download.jsp)下载j2sdk,注意下载版本为Windows Offline Installation的SDK,同时最好下载J2SE 1.5.0 Documentation,然后到tomcat官方站点(http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi)下载tomcat(下载最新5.5.9版本的tomcat); 第二步:安装和配置你的j2sdk和tomcat:执行j2sdk和tomcat的安装程序,然后按默认设置进行安装即可。 1.安装j2sdk以后,需要配置一下环境变量,在我的电脑->属性->高级->环境变量->系统变量中添加以下环境变量(假定你的j2sdk安装在c:\j2sdk1.5.0): JAVA_HOME=c:\j2sdk1.5.0 classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;(.;一定不能少,因为它代表当前路径) path=%JAVA_HOME%\bin 接着可以写一个简单的java程序来测试J2SDK是否已安装成功: public class Test{ public static void main(String args[]){ System.out.println("This is a test program."); } } 将上面的这段程序保存为文件名为Test.java的文件。 然后打开命令提示符窗口,cd到你的Test.java所在目录,然后键入下面的命令 javac Test.java java Test 此时如果看到打印出来This is a test program.的话说明安装成功了,如果没有打印出这句话,你需要仔细检查一下你的配置情况。 2.安装Tomcat后,在我的电脑->属性->高级->环境变量->系统变量中添加以下环境变量(假定你的tomcat安装在c:\tomcat): CATALINA_HOME=c:\tomcat CATALINA_BASE=c:\tomcat 然后修改环境变量中的classpath,把tomat安装目录下的common\lib下的(可以根据实际追加)servlet.jar追加到classpath中去,修改后的classpath如下: classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME%\common\lib\servlet.jar; 接着可以启动tomcat,在IE中访问http://localhost:8080,如果看到tomcat的欢迎页面的话说明安装成功了。 第三步:建立自己的jsp app目录 1.到Tomcat的安装目录的webapps目录,可以看到ROOT,examples, tomcat-docs之类Tomcat自带的的目录; 2.在webapps目录下新建一个目录,起名叫myapp; 3.myapp下新建一个目录WEB-INF,注意,目录名称是区分大小写的; 4.WEB-INF下新建一个文件web.xml,内容如下: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>My Web Application</display-name> <description> A application for test. </description> </web-app> 5.在myapp下新建一个测试的jsp页面,文件名为index.jsp,文件内容如下: <html><body><center> Now time is: <%=new java.util.Date()%> </center></body></html> 6.重启Tomcat 7.打开浏览器,输入http://localhost:8080/myapp/index.jsp 看到当前时间的话说明就成功了。 第四步:建立自己的Servlet: 1.用你最熟悉的编辑器(建议使用有语法检查的java ide)新建一个servlet程序,文件名为Test.java,文件内容如下: package test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Test extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); out.println("<html><body><h1>This is a servlet test.</h1></body></html>"); out.flush(); } } 2 .编译 将Test.java放在c:\test下,使用如下命令编译: C:\Test>javac Test.java 然后在c:\Test下会产生一个编译后的servlet文件:Test.class 3 .将结构test\Test.class剪切到%CATALINA_HOME%\webapps\myapp\WEB-INF\classes下,也就是剪切那个test目录到classes目录下,如果classes目录不存在,就新建一个。 现在webapps\myapp\WEB-INF\classes下有test\Test.class的文件目录结构 4 .修改webapps\myapp\WEB-INF\web.xml,添加servlet和servlet-mapping 编辑后的web.xml如下所示,红色为添加的内容: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>My Web Application</display-name> <description> A application for test. </description> <servlet> <servlet-name>Test</servlet-name> <display-name>Test</display-name> <description>A test Servlet</description> <servlet-class>test.Test</servlet-class> </servlet> <servlet-mapping> <servlet-name>Test</servlet-name> <url-pattern>/Test</url-pattern> </servlet-mapping> </web-app> 这段话中的servlet这一段声明了你要调用的Servlet,而servlet-mapping则是将声明的servlet"映射"到地址/Test上 5 .好了,重启动Tomcat,启动浏览器,输入http://localhost:8080/myapp/Test 如果看到输出This is a servlet test.就说明编写的servlet成功了。 注意:修改了web.xml以及新加了class,都要重启Tomcat 第四步:建立自己的Bean: 1.用你最熟悉的编辑器(建议使用有语法检查的java ide)新建一个java程序,文件名为TestBean.java,文件内容如下: package test; public class TestBean{ private String name = null; public TestBean(String strName_p){ this.name=strName_p; } public void setName(String strName_p){ this.name=strName_p; } public String getName(){ return this.name; } } 2 .编译 将TestBean.java放在c:\test下,使用如下命令编译: C:\Test>javac TestBean.java 然后在c:\Test下会产生一个编译后的bean文件:TestBean.class 3 .将TestBean.class文件剪切到 %CATALINA_HOME%\webapps\myapp\WEB-INF\classes\test下, 4 .新建一个TestBean.jsp文件,文件内容为: <%@ page import="test.TestBean" %> <html><body><center> <% TestBean testBean=new TestBean("This is a test java bean."); %> Java bean name is: <%=testBean.getName()%> </center></body></html> 5 .好了,重启Tomcat,启动浏览器,输入http://localhost:8080/myapp/TestBean.jsp 如果看到输出Java bean name is: This is a test java bean.就说明编写的Bean成功了。 这样就完成了整个Tomcat下的jsp、servlet和javabean的配置。接下来需要做的事情就是多看书、多读别人的好代码,自己多动手写代码以增强自己在这方面开发的能力了。 jvm应填写到 c:\j2sdk\bin 给你一个简单的配置:::: JSP环境配置心得 首先要说的是,使用jdk+tomcat完全可以配置我们的jsp服务器,不再需要其实任何东东,有很多文章介绍了Apache,其实根本用不着,一般的学习调试tomcat完全可以胜任了。 安装jdk后,tomcat在安装之前会自动找到jdk的安装路径,一路点击"下一步",经过一段时间的文件复制,最后"close",完成comcat的安装。 您最好去下载一个版本较高的tomcat,比如4.1以上的,因为它不需要设置太多的系统变量,右击"我的电脑",选择"属性"->"高级"->"环境变量"->"系统变量",新建一个TOMCAT_HOME,值设置成你的tomcat所在的路径,比如:D:\Program Files\Apache Group\Tomcat 5.5,配置完成。 从开始菜单中找到tomcat选项,一般打开顺序是:开始->程序->Apache Tomcat 5.5,选择"Start Tomcat",让jsp服务器开始运行,此时会打开一个类似Dos的窗口,会显示一些相关的信息。
|
| (转载文章请保留出处:北天JAVA技术网(www.java114.com)) |
| |
| 更多精彩文章: |
| Tomcat 4入门指南 |
| 用 JFC/Swing 将可访问性构建到您的 Java 应用程序中 |
| 用Java生成XML |
| 为什么要始终使用PreparedStatement代替Statement? |
| Jsp + JavaBean循序渐进教程(一) |
| Jrun 快速定位编译时期错误位置 |
| |
| 最近评论: |
|
|
| 你曾悄悄的来过! |
| wow gold,wow gold,wow gold,ffxi gil max(6980) |
|
|
| 冰封的往事! |
| wow power leveling,wow gold,wow power leveling,wow gold
max(34) |
|
|
| 冰封的往事! |
| wow power leveling,wow gold,wow power leveling,wow gold
max(3765) |
|
|
| 冰封的往事! |
| wow power leveling,wow gold,WoW Gold,wow gold
max(2686) |
|
|
| 飞舞的传奇! |
| 传世私服,传世私服.传奇世界私服传奇世界私服,传世私服传世私服, 传奇世界私服传奇世界私服.传奇私服传奇私服. max(7716) |
|
|
| moulddni |
| QWQAesV cool site!!! [url=http://moulddni1.com]cool site!!![/url] http://moulddni2.com VoUrtyrQ |
|
|
| acetaminophen |
|
fioricet fioricet fioricet online buy fioricet hydrocodone hydrocodone buy hydrocodone hydrocodone online hydrocodone order acetaminophen e hydrocodone
[url=http://freewebtown.com/morelife/fioricet/]fioricet[/url] [url=http://freewebtown.com/morelife/fioricet/map-fioricet.html]fioricet[/url] [url=http://freewebtown.com/morelife/fioricet/fioricet-online.html]fioricet online[/url] [url=http://freewebtown.com/morelife/fioricet/buy-fioricet.html]buy fioricet[/url] [url=http://freewebtown.com/morelife/hydrocodone/]hydrocodone[/url] [url=http://freewebtown.com/morelife/hydrocodone/map-hydrocodone.html]hydrocodone[/url] [url=http://freewebtown.com/morelife/hydrocodone/buy-hydrocodone.html]buy hydrocodone[/url] [url=http://freewebtown.com/morelife/hydrocodone/hydrocodone-online.html]hydrocodone online[/url] [url=http://freewebtown.com/morelife/hydrocodone/hydrocodone-order.html]hydrocodone order[/url] [url=http://freewebtown.com/morelife/hydrocodone/acetaminophen-e-hydrocodone.html]acetaminophen e hydrocodone[/url]
http://freewebtown.com/morelife/fioricet/ fioricet http://freewebtown.com/morelife/fioricet/map-fioricet.html fioricet http://freewebtown.com/morelife/fioricet/fioricet-online.html fioricet online http://freewebtown.com/morelife/fioricet/buy-fioricet.html buy fioricet http://freewebtown.com/morelife/hydrocodone/ hydrocodone http://freewebtown.com/morelife/hydrocodone/map-hydrocodone.html hydrocodone http://freewebtown.com/morelife/hydrocodone/buy-hydrocodone.html buy hydrocodone http://freewebtown.com/morelife/hydrocodone/hydrocodone-online.html hydrocodone online http://freewebtown.com/morelife/hydrocodone/hydrocodone-order.html hydrocodone order http://freewebtown.com/morelife/hydrocodone/acetaminophen-e-hydrocodone.html acetaminophen e hydrocodone |
|
|
| buy |
|
generic zocor nexium nexium nexium side effects buy nexium effexor effexor effexor xr effexor side effects effexor withdrawal
[url=http://freewebtown.com/morelife/zocor/generic-zocor.html]generic zocor[/url] [url=http://freewebtown.com/morelife/nexium/]nexium[/url] [url=http://freewebtown.com/morelife/nexium/map-nexium.html]nexium[/url] [url=http://freewebtown.com/morelife/nexium/nexium-side-effects.html]nexium side effects[/url] [url=http://freewebtown.com/morelife/nexium/buy-nexium.html]buy nexium[/url] [url=http://freewebtown.com/morelife/effexor/]effexor[/url] [url=http://freewebtown.com/morelife/effexor/map-effexor.html]effexor[/url] [url=http://freewebtown.com/morelife/effexor/effexor-xr.html]effexor xr[/url] [url=http://freewebtown.com/morelife/effexor/effexor-side-effects.html]effexor side effects[/url] [url=http://freewebtown.com/morelife/effexor/effexor-withdrawal.html]effexor withdrawal[/url]
http://freewebtown.com/morelife/zocor/generic-zocor.html generic zocor http://freewebtown.com/morelife/nexium/ nexium http://freewebtown.com/morelife/nexium/map-nexium.html nexium http://freewebtown.com/morelife/nexium/nexium-side-effects.html nexium side effects http://freewebtown.com/morelife/nexium/buy-nexium.html buy nexium http://freewebtown.com/morelife/effexor/ effexor http://freewebtown.com/morelife/effexor/map-effexor.html effexor http://freewebtown.com/morelife/effexor/effexor-xr.html effexor xr http://freewebtown.com/morelife/effexor/effexor-side-effects.html effexor side effects http://freewebtown.com/morelife/effexor/effexor-withdrawal.html effexor withdrawal |
|
|
| zocor |
|
celexa drug celexa anxiety zyrtec zyrtec zyrtec d zyrtec medication generic zyrtec zocor zocor zocor side effects
[url=http://freewebtown.com/morelife/celexa/celexa-drug.html]celexa drug[/url] [url=http://freewebtown.com/morelife/celexa/celexa-anxiety.html]celexa anxiety[/url] [url=http://freewebtown.com/morelife/zyrtec/]zyrtec[/url] [url=http://freewebtown.com/morelife/zyrtec/map-zyrtec.html]zyrtec[/url] [url=http://freewebtown.com/morelife/zyrtec/zyrtec-d.html]zyrtec d[/url] [url=http://freewebtown.com/morelife/zyrtec/zyrtec-medication.html]zyrtec medication[/url] [url=http://freewebtown.com/morelife/zyrtec/generic-zyrtec.html]generic zyrtec[/url] [url=http://freewebtown.com/morelife/zocor/]zocor[/url] [url=http://freewebtown.com/morelife/zocor/map-zocor.html]zocor[/url] [url=http://freewebtown.com/morelife/zocor/zocor-side-effects.html]zocor side effects[/url]
http://freewebtown.com/morelife/celexa/celexa-drug.html celexa drug http://freewebtown.com/morelife/celexa/celexa-anxiety.html celexa anxiety http://freewebtown.com/morelife/zyrtec/ zyrtec http://freewebtown.com/morelife/zyrtec/map-zyrtec.html zyrtec http://freewebtown.com/morelife/zyrtec/zyrtec-d.html zyrtec d http://freewebtown.com/morelife/zyrtec/zyrtec-medication.html zyrtec medication http://freewebtown.com/morelife/zyrtec/generic-zyrtec.html generic zyrtec http://freewebtown.com/morelife/zocor/ zocor http://freewebtown.com/morelife/zocor/map-zocor.html zocor http://freewebtown.com/morelife/zocor/zocor-side-effects.html zocor side effects |
|
|
| online |
|
online xenical discount xenical propecia propecia buy propecia propecia prescription loss propecia celexa celexa buy celexa
[url=http://freewebtown.com/morelife/xenical/online-xenical.html]online xenical[/url] [url=http://freewebtown.com/morelife/xenical/discount-xenical.html]discount xenical[/url] [url=http://freewebtown.com/morelife/propecia/]propecia[/url] [url=http://freewebtown.com/morelife/propecia/map-propecia.html]propecia[/url] [url=http://freewebtown.com/morelife/propecia/buy-propecia.html]buy propecia[/url] [url=http://freewebtown.com/morelife/propecia/propecia-prescription.html]propecia prescription[/url] [url=http://freewebtown.com/morelife/propecia/loss-propecia.html]loss propecia[/url] [url=http://freewebtown.com/morelife/celexa/]celexa[/url] [url=http://freewebtown.com/morelife/celexa/map-celexa.html]celexa[/url] [url=http://freewebtown.com/morelife/celexa/buy-celexa.html]buy celexa[/url]
http://freewebtown.com/morelife/xenical/online-xenical.html online xenical http://freewebtown.com/morelife/xenical/discount-xenical.html discount xenical http://freewebtown.com/morelife/propecia/ propecia http://freewebtown.com/morelife/propecia/map-propecia.html propecia http://freewebtown.com/morelife/propecia/buy-propecia.html buy propecia http://freewebtown.com/morelife/propecia/propecia-prescription.html propecia prescription http://freewebtown.com/morelife/propecia/loss-propecia.html loss propecia http://freewebtown.com/morelife/celexa/ celexa http://freewebtown.com/morelife/celexa/map-celexa.html celexa http://freewebtown.com/morelife/celexa/buy-celexa.html buy celexa |
|
|
| |
| 免责声明:该文章由网友发表,如果对您造成侵权,请联系站长。 |