@duyao
2015-05-01T01:08:38.000000Z
字数 2790
阅读 4059
servlet
web1web1目录下建立WEB-INF/web.xml(web.xml可以从root中拷贝)web1目录下classes和lib文件夹web1/classes下建立MyFirstServerlet.java文件实现Serverlet接口
//MyFirstServerlet.javapackage com.web1;import javax.serverlet.*;import javax.serverlet.http.*;import java.io.*;class MyFisrstServerlet implements Serverlet{//该函数用于初始化serverlet,就是把该serverlet装载到内存中,且只被调用一次public void init(ServerletConfig config) throws ServerletException{}//得到serverletconfig对象public ServerletConfig getServerletConfig(){return null;}//该函数是服务函数,我们的业务逻辑代码写在这里//该函数每次都会被调用public void service(ServerletRequest req,ServerletResponse res)throws ServerletException,java.io.IOException{}//该函数得到serverlet配置信息public java.lang.String getServerletInfo(){return null;}//销毁该serverlet,从内存中清楚,且只被调用一次public void destroy(){}}
5 . 用javac编译,对打包文件应使用命令javac -d . 文件名
6. 根据serverlet规范部署到web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?><!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis 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 withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations 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><!--serverlet-name该名字可以自己定义,也使用该serverlet的名字--><servlet-name>MyFirstServerlet</servlet-name><!--serverlet-class要指明该serverlet放在哪个包下面的,形式是包.包.类,类名不加java,eg->com.web1.MyFirstServerlet--><servlet-class><com.web1.MyFirstServerlet</servlet-class></servlet><!--selverlet-mapping是用来做映射的--><servlet-mapping><!--serverlet-name这个serverlet名字要和上面的serverlet名字相同--><servlet-name>MyFirstServerlet</servlet-name><!--url-pattern是将来访问serverlet的资源名,默认命名为该serverlet的名字--><url-pattern>/web1</url-pattern></servlet-mapping></web-app>
一堆bug
如何不重启tomcat,但可以reload一个应用?
进入tomcat的manager,localhost:8080,点击reload
了解即可,只有service函数需要重写
通过继承HttpServerlet方法需要重写doGet()和doPost()方法
区别是:
安全性:get<post,即get提交的数据会在浏览器地址栏显示
内容大小:get<post,get不能大于2k,post理论不设限
响应速度:get>post,get要求立即处理,post可能会形成请求队列
init函数,该函数会把serverlet实例加载到内存中,且init函数只会被调用一次service函数service函数