zoukankan      html  css  js  c++  java
  • servlet Servlet例子

    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.0webappshelloServletWEB-INFclasses下新建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.0libservlet-api.jar)set classpath了,然后javac -d . FirstServlet了。

    在F:Tomcat 7.0webappshelloServletWEB-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即可。

  • 相关阅读:
    “上海名媛群”事件,我来说几句
    急于脱手商业地产的酒店式公寓,让我在无锡买了房
    40年产权的商业地产,个人投资者决不能碰
    产权分割商铺,太坑人!
    我的第二故乡 – 武汉
    我的第二故乡
    Consul踢除失效服务和移除Node节点
    合并2个数组为1个无重复元素的有序数组--Go对比Python
    当Prometheus遇到混沌工程
    测试流程规范--测试准入、准出、停止标准、bug优先级定义
  • 原文地址:https://www.cnblogs.com/flying607/p/3449283.html
Copyright © 2011-2022 走看看