今天复习了下方立勋老师的Servlet开发,结果搞了好几次都没有成功,真是丢人,不过期间也收获很多,现记录如下:
首先需要我们对web应用的结构有一个简单的了解,一个web应用有如下目录:
- projectName
-static resource (*.html,*.js,*.img)
-WEB-INF 这里注意不是`WEB_INF`、`WEB-INFO` 需要注意。虽然我没有在这里犯错 -_-#
-classes 这里我犯了一个非常低级的错误,将classes拼写错误,导致tomcat怎么都找不到我编写的servlet,愁死了.
-lib 这里存放这个项目需要的支持jar包
-web.xml这里配置项目需要的资源
- 新建项目
将新建的项目放在tomcat 的webapps目录下,然后我们新建上面描述的目录结构. - 编写Servlet:
我们手动编写一个Servlet类,继承HttpServlet
,覆写service
方法导入对应的类就可以了[严格说,这里继承HttpServlet
覆写service
方法是错误的,我们继承HttpServlet只需要关注doGet
和doPost
方法就可以了]。
我编写的Servlet源码如下,以供参考:
我们编写Servlet类的时候可以查询
javaEE api
或servlet api我比较喜欢选择后者,这样不容易搞混,servlet api
里面只有servlet相关的方法.这里我继承HttpServlet
所以导包需要注意,如果使用*
导入一系列包则需要分级导入两次如下:
import javax.servlet.*
andjavax.servlet.http.*
package cn.palm.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class OneServlet extends HttpServlet {
public void service(ServletRequest req,ServletResponse res) throws ServletException,java.io.IOException {
//取得输出流
OutputStream out = res.getOutputStream() ;
out.write("Hello Word.".getBytes()) ;
}
}
然后手动编译,我在这有比较曲折的经历,这里编译直接编译是不能通过的,因为在javase中没有servlet
相关jar包,需要将来servlet-api.jar
导入到编译使用 classpath
,我们可以临时变更classpath
的内容,在win下如下:
set classpath=%classpath%;D:\apache-tomcat-6.0.44\lib\servlet-api.jar
这样就可以使用javac命令进行编译了,我在这里折腾了好几次,怎么都设置不好classpath,因为我使用了""
set classpath ="xxxx" 命令如下:
javac -d . OneServlet .java
以上意思是打包编译,并且将编译之后的class存放到当前目录下,所以我们不能在别的目录中进行编译,需要进入刚才新建好的classes
目录中,或者你可以编译完成后手动拷贝过去.现在我们就算完成了servlet
的准备。接下来我们开始编写web.xml
文件内容。
3.配置Servlet
我们按照tomcat 自己的web.xml模板配置自己的web.xml需要配置如下:
-配置servlet class 路径和名字, 还有servlet mpping内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>OneServlet</servlet-name>
<servlet-class>cn.palm.servlet.OneServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OneServlet</servlet-name>
<url-pattern>/OneServlet</url-pattern>
</servlet-mapping>
</web-app>
这样我们启动服务器就可以访问了。上面的<servlet></servlet>配置是为了启动服务器(tomcat)实例化servlet,<servlet-mapping>是为了映射请求路径,这里只要是/OneServlet
都会触发到这个servlet请求。name可以不用和servlet同名,只要上下配置一致就可以了.
结果如下: