[关闭]
@duyao 2015-05-01T01:08:38.000000Z 字数 2790 阅读 3872

开发serverlet的3种方式

servlet


开发serverlet的3种方式

实现Serverlet接口

  1. 建立web应用web1
  2. web1目录下建立WEB-INF/web.xml(web.xml可以从root中拷贝)
  3. web1目录下classeslib文件夹
  4. web1/classes下建立MyFirstServerlet.java文件实现Serverlet接口
  1. //MyFirstServerlet.java
  2. package com.web1;
  3. import javax.serverlet.*;
  4. import javax.serverlet.http.*;
  5. import java.io.*;
  6. class MyFisrstServerlet implements Serverlet{
  7. //该函数用于初始化serverlet,就是把该serverlet装载到内存中,且只被调用一次
  8. public void init(ServerletConfig config) throws ServerletException{
  9. }
  10. //得到serverletconfig对象
  11. public ServerletConfig getServerletConfig(){
  12. return null;
  13. }
  14. //该函数是服务函数,我们的业务逻辑代码写在这里
  15. //该函数每次都会被调用
  16. public void service(ServerletRequest req,ServerletResponse res)
  17. throws ServerletException,java.io.IOException{
  18. }
  19. //该函数得到serverlet配置信息
  20. public java.lang.String getServerletInfo(){
  21. return null;
  22. }
  23. //销毁该serverlet,从内存中清楚,且只被调用一次
  24. public void destroy(){
  25. }
  26. }

5 . 用javac编译,对打包文件应使用命令javac -d . 文件名
6. 根据serverlet规范部署到web.xml文件

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  17. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  19. version="2.5">
  20. <servlet>
  21. <!--serverlet-name
  22. 该名字可以自己定义,也使用该serverlet的名字
  23. -->
  24. <servlet-name>MyFirstServerlet</servlet-name>
  25. <!--serverlet-class
  26. 要指明该serverlet放在哪个包下面的,形式是包.包.类,类名不加java,eg->com.web1.MyFirstServerlet
  27. -->
  28. <servlet-class><com.web1.MyFirstServerlet</servlet-class>
  29. </servlet>
  30. <!--selverlet-mapping是用来做映射的-->
  31. <servlet-mapping>
  32. <!--serverlet-name
  33. 这个serverlet名字要和上面的serverlet名字相同
  34. -->
  35. <servlet-name>MyFirstServerlet</servlet-name>
  36. <!--url-pattern
  37. 是将来访问serverlet的资源名,默认命名为该serverlet的名字
  38. -->
  39. <url-pattern>/web1</url-pattern>
  40. </servlet-mapping>
  41. </web-app>

一堆bug

其他问题

如何不重启tomcat,但可以reload一个应用?
进入tomcat的manager,localhost:8080,点击reload

继承GenericServerlet

了解即可,只有service函数需要重写

继承HttpServerlet

通过继承HttpServerlet方法需要重写doGet()doPost()方法
区别是:
安全性:get<post,即get提交的数据会在浏览器地址栏显示
内容大小:get<post,get不能大于2k,post理论不设限
响应速度:get>post,get要求立即处理,post可能会形成请求队列

serverlet的生命周期

  1. 当serverlet第一被调用时会触发init函数,该函数会把serverlet实例加载到内存中,且init函数只会被调用一次
  2. 然后去调用serverlet 的service函数
  3. 当第二次访问就直接调用service函数
  4. 当web应用relaod或者关闭,都会去调用relaod函数,该函数会销毁
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注