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. Spring Framework 4.0.0发布,首次支持Java 8

    Spring项目组今天发布了Spring 框架4.0.0版本.Spring是一个开源的轻量级Java SE和Java EE开发应用框架,其目的是用于简化企业级应用程序开发. Spring框架第一个版本 ...

  2. MyBatis使用Collection查询多对多或一对多结果集bug

    情况描述:当使用JOIN查询,如果SQL查询出来的记录不是按id列排序的,则生成的List结果会有问题 案例: 1) 数据库模型 简而言之一个Goods包含多个Goods_Img 2) Java Be ...

  3. C语言:用字符读取流和输出流来读写入数据。(文本文件)

    /* 文件的几种操作模式: r:只读   w:只写   rw:可读可写 文件的分类: t:文本文件(字符文件)   b:二进制文件(字节文件) 注意: 采用只读方式打开文件时,如果源文件不存在,打开文 ...

  4. Maximal Rectangle leetcode java

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  5. a标签默认颜色

    <a href="www.baidu.com">test</a> <span style="color:#428bca;"> ...

  6. layer和3D仿射变换

    1.视图的显示基于图层,通过控制图层同样能控制显示效果,获取当前的视图的layer,并为其增加圆角边框. //设置layer边框的宽度为2 view.layer.borderWidth=; //如果需 ...

  7. Ubuntu14.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 说在前面的话  首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu 14.04.4 LTS ...

  8. Oracle服务启动顺序导致ORA-12514

    在window 上装了oracle11g,按照常规步骤安装完成后一切OK,如下图所示 C:\Users\Administrator>sqlplus /nolog SQL*Plus: Releas ...

  9. Android-Bundle认知、和Intent的差别

    不时的回过头来看看自己的Andriod学习.实践之路,总发现有些曾经不明确的,如今清楚缘由.也会发现一些之前没怎么关注的.如今看到了 ,很想去深刻了解的. 比方:Bundle. 在一个Activity ...

  10. DockPanel 类

    DockPanel 类 .NET Framework 4.5   其他版本   此主题尚未评级 - 评价此主题   定义您可水平或垂直排列子元素的区域,互相. 继承层次结构   System.Obje ...