Servlet 01 ----【javaweb-04】

SERVLET

什么是Servlet?

  Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容。    

                                 ---- 源自百度百科https://baike.baidu.com/item/Servlet/477555

 

初步使用Servlet

  doGet和doPost只需要写一个就够了,另一个继承即可。

package com.charles.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost执行");
        PrintWriter writer = resp.getWriter(); // 响应流
        writer.print("Hello,Servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

 

  运行结果(这里已经提前配置好了Tomcat):

posted @ 2020-07-24 22:12  Charles_H  阅读(69)  评论(0编辑  收藏  举报