把jakarta-tomcat-5.5.x.zip 和jakarta-tomcat-5.5.x-compat.zip 和jakarta-tomcat-5.5.x-admin.zip (Tomcat 默认是没有内置admin模块了 Tomcat's administration web application is no longer installed by default. Download and install the "admin" package to use it. ) 都解压到同一个目录下面。比如:D:\jakarta-tomcat-5.5.x\ (如果使用jdk1.4,才需要compat.zip用jdk1.5就可以免了这个。)
/** * Example filter that sets the character encoding to be used in parsing the * incoming request */ public class SetCharacterEncodingFilter implements Filter {
/** * Take this filter out of service. */ public void destroy() { } /** * Select and set (if specified) the character encoding to be used to * interpret request parameters for this request. */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " My Web Application A application for test.
Test Test A test Servlet test.Test
Test /Test
这段话中的servlet这一段声明了你要调用的Servlet,而servlet-mapping则是将声明的servlet"映射"到地址/Test上 5 .好了,重启动Tomcat,启动浏览器,输入如果看到输出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" %>
<% TestBean testBean=new TestBean("This is a test java bean."); %> Java bean name is: <%=testBean.getName()%>
5 .好了,重启Tomcat,启动浏览器,输入如果看到输出Java bean name is: This is a test java bean.就说明编写的Bean成功了。 这样就完成了整个Tomcat下的jsp、servlet和javabean的配置。接下来需要做的事情就是多看书、多读别人的好代码,自己多动手写代码以增强自己在这方面开发的能力了。