Servlet是sun公司提供的一门用于开发动态web资源的技术。
 
Sun公司在其API中提供了一个servlet接口(参看J2EE API文档),用户若想使用Java程序开发一个动态web资源,只需编写一个servlet接口的实现类,并把这个类部署到web服务器中,就算开发好了一个动态web资源。
 
按照一种约定俗成的称呼习惯,通常我们也把实现了servlet接口的java程序,称之为Servlet。
 
API:
 
public interface Servlet

Defines methods that all servlets must implement.

A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

  1. The servlet is constructed, then initialized with the init method.
  2. Any calls from clients to the service method are handled.
  3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.

下面手写一个servlet实例:

在F:\Tomcat 7.0\webapps\helloServlet\WEB-INF\classes下新建FirstServlet.java内容如下:

package flying607;

import java.io.*;
import javax.servlet.*;

public class FirstServlet extends GenericServlet{
 public void service(ServletRequest req, ServletResponse res) throws ServletException,IOException{
  OutputStream out = res.getOutputStream();
  out.write("Hello,Servlet!Good-Bye!".getBytes());
 }
}

将需要的包(F:\Tomcat 7.0\lib\servlet-api.jar)set classpath了,然后javac -d . FirstServlet了。

在F:\Tomcat 7.0\webapps\helloServlet\WEB-INF下新建web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<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_3_0.xsd"
  version="3.0">

<servlet-mapping>
        <servlet-name>flying607</servlet-name>
        <url-pattern>/flying</url-pattern>
    </servlet-mapping>

<servlet>
        <servlet-name>flying607</servlet-name>
        <servlet-class>
          flying607.FirstServlet
        </servlet-class>
 </servlet>

</web-app>

访问helloServlet/flying即可。

servlet Servlet例子的更多相关文章

  1. Servlet+JSP例子

    前面两节已经学习了什么是Servlet,Servlet接口函数是哪些.怎么运行.Servlet生命周期是什么?  以及Servlet中的模式匹配URL,web.xml配置和HttpServlet.怎么 ...

  2. servlet简单例子1

    servlet简单例子1 分类: servlet jsp xml2012-04-18 21:54 3646人阅读 评论(3) 收藏 举报 servletloginjspaction浏览器 LoginS ...

  3. org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.servlet.Servlet

    java.lang.ClassCastException: org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.se ...

  4. sae Servlet class XXXX is not a javax.servlet.Servlet

    以前都是使用myeclipse开发web工程上传sae后没有问题,但是使用javaee导出war包上传sae 无法访问 Servlet class  XXXX is not a javax.servl ...

  5. cannot be cast to javax.servlet.Servlet

    在第一次开发Maven项目时,maven环境和仓库以及eclipse都和讲师讲解的一样,可是却遇到下面这个问题: java.lang.ClassCastException: servlet.UserS ...

  6. java.lang.ClassCastException: cn.itcase.serviceImpl.servicestudentImpl cannot be cast to javax.servlet.Servlet

    java.lang.ClassCastException: cn.itcase.serviceImpl.servicestudentImpl cannot be cast to javax.servl ...

  7. 异常:Servlet class X is not a javax.servlet.Servlet

    使用Maven命令 mvn archetype:create 创建了一个简单的web项目.导入Eclipse运行时,报这样的异常信息: Servlet class X is not a javax.s ...

  8. Servlet Servlet是Java平台上的CGI技术

    Servlet Servlet是Java平台上的CGI技术.Servlet在服务器端运行,动态地生成Web页面.与传统的CGI和许多其它类似CGI的技术相比,Java Servlet具有更高的效率并更 ...

  9. Servlet - Servlet相关

    1. 概念 Servlet是指任何实现了Servlet接口的类, Servlet运行于支持Java的应用服务器中, Servlet可以响应任何类型的请求, 但大多数情况下, Servlet只用来扩展基 ...

  10. "xxx cannot be cast to jakarta.servlet.Servlet "报错解决方式

    在做jsp的上机时候同学出现了一个500错误:com.kailong.servlet.ComputeBill cannot be cast to jaka.servlet.Servlet 然后因为我用 ...

随机推荐

  1. CLR基础,CLR运行过程,使用dos命令创建、编译、运行C#文件,查看IL代码

    CLR是Common Language Runtime的缩写,是.NET程序集或可执行程序运行的一个虚拟环境.CLR用于管理托管代码,但是它本身是由非托管代码编写的,并不是一个包含了托管代码的程序集, ...

  2. dos命令行实践

    本篇体验使用dos命令行窗口实现各种操作. □ 打开dos命令行窗口 →点击电脑左下角"开始"按钮→点击"运行"→输入"cmd",按回车,来 ...

  3. NDK开发环境搭建_r8

    本文主内容: 1.  Android NDK 安装 2.  安装Cygwin与使用NDK编译 3.  在Eclipse中集成C/C++开发环境CDT 4.  安装Sequoyah插件 5.  JNI编 ...

  4. MySQL面试题集锦

    1. 如何设计一个高并发的系统 ① 数据库的优化,包括合理的事务隔离级别.SQL语句优化.索引的优化 ② 使用缓存,尽量减少数据库 IO ③ 分布式数据库.分布式缓存 ④ 服务器的负载均衡 2. 锁的 ...

  5. 追MM和Java的23种设计模式

    我在Java论坛看到这篇文章,作者以轻松的语言比喻了java的32种模式,有很好的启发作用,但可惜没有给出具体的意思,我就在后边加上了.这些都是最简单的介绍,要学习的话建议你看一下阎宏博士的<J ...

  6. 设置IE浏览器指定的功能

    if ($.browser.msie) { // Internet Explorer is a sadist. }

  7. ASP.NET MVC中使用jQuery时的浏览器缓存问题

    介绍 尽管jQuery在浏览器ajax调用的时候对缓存提供了很好的支持,还是有必要了解一下如何高效地使用http协议. 首先要做的事情是在服务器端支持HTTP GET,定义不同的URL输出不同的数据( ...

  8. WebViewClient 简介 API 案例

    代码位置:https://github.com/baiqiantao/WebViewTest.git 设计思想理解 在WebView的设计中,不是什么事都要WebView类干的,有相当多的杂事是分给其 ...

  9. du 命令秘籍

    导读 du命令是检查硬盘使用情况,统计文件或目录及子目录使用硬盘的空间大小.参数的不同组合,可以更快的提高工作效率,以下仅列出了经常使用到的参数,如需更详细的信息,请用man du命令来获得. 1.命 ...

  10. mac利用Synergy操作多台电脑

    话说,我现在桌子上有3台电脑,但是我只有一个鼠标和键盘,我该怎么玩呢,就像win一样,可以外接一个显示器,鼠标到达了显示器边缘自动翻越到另一个显示器上,这个没问题,win已经实现了. 我今天推荐一款牛 ...