Lesson 1

开发servlet有三种方法:

  1. 实现servlet接口
  2. 继承GenericServlet
  3. 继承HttpServlet

1.实现Servlet接口来开发servlet

package com.jyq;
import javax.servlet.*;
import java.io.*;
//使用实现servlet接口的方式来开发
public class HelloWorld implements Servlet{

    //销毁servlet实例,释放内存
    //1.reload该servlet(webapp)
    //2.关闭tomcat

    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("destory");
        
    }

    
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    //该函数用来初始化该servlet
    //该函数只会被调用一次
    //当用户第一次访问servlet
    public void init(ServletConfig arg0) throws ServletException {
        System.out.println("init it");
    }

    //用于处理业务逻辑,
    //程序员应当把业务逻辑代码写在这里,当用户每访问该servlet的时候,都会调用
    //req用于获得浏览器的信息
    //res用于向客户端返回信息
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("service it");
        //从res中得到PrintWriter
        PrintWriter pw = res.getWriter();
        pw.print("Hello,world");
    }

}

web.xml配置方法

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LearnServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
  <servlet>
    <!-- 给你的servlet去个名字。任意的 -->
    <servlet-name>hello</servlet-name>
    <!-- 指明servlet的路径,包名+类名 -->
    <servlet-class>com.jyq.HelloWorld</servlet-class>
  </servlet>
  
  
  <servlet-mapping>
     <!-- 给你的servlet去个名字。任意的 -->
    <servlet-name>hello</servlet-name>
    <!-- 这是在浏览器中输入的访问该servlet的url -->
    <url-pattern>/jyq</url-pattern>
  </servlet-mapping>

</web-app>

2.通过继承GenericServlet来实现servlet

屏幕快照 2016-03-20 21.11.47.png

中途出现了一些小插曲:

  • 当我开着小纸飞机的时候,tomcat就会访问不了,所以要将小纸飞机关闭才能从浏览器中访问。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,803评论 11 349
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 34,706评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,568评论 19 139
  • 这部分主要是与Java Web和Web Service相关的面试题。 96、阐述Servlet和CGI的区别? 答...
    杂货铺老板阅读 1,502评论 0 10
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,273评论 6 342

友情链接更多精彩内容