zoukankan      html  css  js  c++  java
  • Servlet课程0424(三) 通过继承HttpServlet来开发Servlet

    //这是第三种开发servlet的方法,通过继承httpservlet
    package com.tsinghua;
    
    import javax.servlet.http.*;
    import java.io.*;
    
    public class HelloHttp extends HttpServlet{
        
        //处理get请求
         //req用于获得客户端(浏览器)的信息
         //res用于向 客户端(浏览器)返回信息
        public void doGet(HttpServletRequest req, HttpServletResponse res)
        {
            //业务逻辑
            try{
                PrintWriter pw = res.getWriter();
                pw.println("Hello,http!");
                
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
            
        }
        
         //处理get请求
         //req用于获得客户端(浏览器)的信息
         //res用于向 客户端(浏览器)返回信息
        public void doPost(HttpServletRequest req, HttpServletResponse res)
        {
            
            this.doGet(req,res);        
            
        }    
        
    }

    web.xml Servlet配置文件

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--
     Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
    
    <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_2_5.xsd"
       version="2.5">
    
      <display-name>Welcome to Tomcat</display-name>
      <description>
         Welcome to Tomcat
      </description>
      
      <!-- JSPC servlet mapping start -->
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.tsinghua.Hello</servlet-class>
    </servlet>
    
      <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/sp</url-pattern>
      </servlet-mapping>
      
      <servlet>
        <servlet-name>hellogen</servlet-name>
        <servlet-class>com.tsinghua.HelloGen</servlet-class>
    </servlet>
    
      <servlet-mapping>
        <servlet-name>hellogen</servlet-name>
        <url-pattern>/hellogen</url-pattern>
      </servlet-mapping>
      
        <servlet>
        <servlet-name>hellohttp</servlet-name>
        <servlet-class>com.tsinghua.HelloHttp</servlet-class>
    </servlet>
    
      <servlet-mapping>
        <servlet-name>hellohttp</servlet-name>
        <url-pattern>/hellohttp</url-pattern>
      </servlet-mapping>
    <!-- JSPC servlet mapping end -->
    </web-app>
  • 相关阅读:
    36_Cache Aside Pattern缓存+数据库读写模式的分析
    35_亿级流量商品详情页的多级缓存架构以及架构中每一层的意义
    34_redis阶段性总结:1T以上海量数据+10万以上QPS高并发+99.99%高可用
    33_redis在实践中的一些常见问题以及优化思路(包含linux内核参数优化)
    正则表达式全部符号解释
    如何正确学习JavaScript
    2015阿里校招前端笔试题
    前端面试总结2
    前端面试总结
    通俗易懂的来讲讲DOM
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/5428089.html
Copyright © 2011-2022 走看看