1. //这是第三种开发servlet的方法,通过继承httpservlet
  2. package com.tsinghua;
  3.  
  4. import javax.servlet.http.*;
  5. import java.io.*;
  6.  
  7. public class HelloHttp extends HttpServlet{
  8.  
  9. //处理get请求
  10. //req用于获得客户端(浏览器)的信息
  11. //res用于向 客户端(浏览器)返回信息
  12. public void doGet(HttpServletRequest req, HttpServletResponse res)
  13. {
  14. //业务逻辑
  15. try{
  16. PrintWriter pw = res.getWriter();
  17. pw.println("Hello,http!");
  18.  
  19. }
  20. catch(Exception ex)
  21. {
  22. ex.printStackTrace();
  23. }
  24.  
  25. }
  26.  
  27. //处理get请求
  28. //req用于获得客户端(浏览器)的信息
  29. //res用于向 客户端(浏览器)返回信息
  30. public void doPost(HttpServletRequest req, HttpServletResponse res)
  31. {
  32.  
  33. this.doGet(req,res);
  34.  
  35. }
  36.  
  37. }

web.xml Servlet配置文件

  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.  
  10. http://www.apache.org/licenses/LICENSE-2.0
  11.  
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. -->
  18.  
  19. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  20. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  22. version="2.5">
  23.  
  24. <display-name>Welcome to Tomcat</display-name>
  25. <description>
  26. Welcome to Tomcat
  27. </description>
  28.  
  29. <!-- JSPC servlet mapping start -->
  30. <servlet>
  31. <servlet-name>hello</servlet-name>
  32. <servlet-class>com.tsinghua.Hello</servlet-class>
  33. </servlet>
  34.  
  35. <servlet-mapping>
  36. <servlet-name>hello</servlet-name>
  37. <url-pattern>/sp</url-pattern>
  38. </servlet-mapping>
  39.  
  40. <servlet>
  41. <servlet-name>hellogen</servlet-name>
  42. <servlet-class>com.tsinghua.HelloGen</servlet-class>
  43. </servlet>
  44.  
  45. <servlet-mapping>
  46. <servlet-name>hellogen</servlet-name>
  47. <url-pattern>/hellogen</url-pattern>
  48. </servlet-mapping>
  49.  
  50. <servlet>
  51. <servlet-name>hellohttp</servlet-name>
  52. <servlet-class>com.tsinghua.HelloHttp</servlet-class>
  53. </servlet>
  54.  
  55. <servlet-mapping>
  56. <servlet-name>hellohttp</servlet-name>
  57. <url-pattern>/hellohttp</url-pattern>
  58. </servlet-mapping>
  59. <!-- JSPC servlet mapping end -->
  60. </web-app>

Servlet课程0424(三) 通过继承HttpServlet来开发Servlet的更多相关文章

  1. java一个类 继承HttpServlet 和实现Servlet区别

    java一个类 继承HttpServlet 和实现Servlet区别 servlet 是一个接口,如果实现这个接口,那么就必须实现接口里面定义的所有方法 而HttpServlet实现了servlet接 ...

  2. Servlet-通过继承HttpServlet类实现Servlet程序

    通过继承HttpServlet类实现Servlet程序(开发一般用) 一般在实际项目开发中,都是使用继承 HttpServlet类的方式实现Servlet程序 1,编写一个类去继承 HttpServl ...

  3. Servlet课程0424(二) 通过继承GenericServlet来开发Servlet

    //这是第二种开发servlet的方法(继承GernericServlet) package com.tsinghua; import javax.servlet.GenericServlet; im ...

  4. JavaWeb学习之Servlet(二)----Servlet的生命周期、继承结构、修改Servlet模板

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140466.html 一.http协议回顾: 在上一篇文章中:JavaW ...

  5. (转)JavaWeb学习之Servlet(二)----Servlet的生命周期、继承结构、修改Servlet模板

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140466.html 一.http协议回顾: 在上一篇文章中:JavaW ...

  6. Servlet课程0424(一) 通过实现Servlet接口来开发Servlet

    //这是我的第一个Servlet,使用实现Servlet接口的方式来开发 package com.tsinghua; import javax.servlet.*; import java.io.*; ...

  7. servlet实现的三种方式对比(servlet 和GenericServlet和HttpServlet)

    第一种: 实现Servlet 接口 第二种: 继承GenericServlet 第三种 继承HttpServlet (开发中使用) 通过查看api文档发现他们三个(servlet 和GenericSe ...

  8. 开发servlet三种方式

    第一种:实现Servlet接口 ServletDemo类 实现Servlet接口 public class ServletDemo implements Servlet { //初始化该servlet ...

  9. Servlet实现的三种方法

    (1)方法一: //这是第一个实现servlet的方法.使用时限servlet接口的方法来实现,使用的时候须要引用servlet-api.jar package com.lc; import java ...

随机推荐

  1. C#中Predicate<T>与Func<T, bool>泛型委托的用法实例

    本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...

  2. (转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例

    1. 源程序 package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAdd ...

  3. UVaLive 7361(矩阵快速幂)

    题意:矩阵快速幂求斐波那契数列. 分析:

  4. lex&yacc4

    yacc: we cannt use the $$ value dirictly. we need get it irrotly;

  5. DWZ 刷新 dialog

    DWZ刷新dialog: 1,在删除按钮上添加callback属性;如:(callback="dialogAjax") <a class="delImg" ...

  6. PHP、Java、C#实现URI参数签名算法,确保应用与REST服务器之间的安全通信,防止Secret Key盗用、数据篡改等恶意攻击行为

    简介 应用基于HTTP POST或HTTP GET请求发送Open API调用请求时,为了确保应用与REST服务器之间的安全通信,防止Secret Key盗用.数据篡改等恶意攻击行为,REST服务器使 ...

  7. 使用node.js抓取有路网图书信息(原创)

    之前写过使用python抓取有路网图书信息,见http://www.cnblogs.com/dyf6372/p/3529703.html. 最近想学习一下Node.js,所以想试试手,比较一下http ...

  8. [ Database ] [ Sybase ] [ SQLServer ] sybase 與SQL Server的界接方式

    目前我們有個專案Server A安裝了 SQL Server 2012,有個需求需要連線到另外一台Server B上的 Sybase 12.5的view, 先前試過了很多方法都無法連通.主要的原因是因 ...

  9. [Testing] 測試電子原文書

    測試電子原文書 http://files.cnblogs.com/vincentmylee/SoftwareTesting2ndEdition.7z

  10. WPF 多线程处理(2)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) WPF UI 设计需要自动适应窗体大小,那么 ...