• Servlet开发


    Servlet基础

    Servlet是运行在Web服务器端的Java应用程序,它是用Java语言编写的,具有Java语言的特点。并且Servlet对象主要封装了对Http请求的处理,并且他的运行需要Servlet容器的支持。

    Servlet结构体系

    在这里插入图片描述

    Servlet技术特点

    因为Servlet是由Java语言编写的,不仅继承了Java语言的优点还对Web的相关应用进行了封装,同时对Servlet容器还提供了对应用的相对扩展。
    主要技术优点:功能强大,可移植,性能高效,安全性能高,可扩展.

    Servlet与JSP的区别

    在这里插入图片描述
    在这里插入图片描述

    Servlet API编程常用接口和类

    Servlet接口

    方法 说明
    public void init(SerletConfig config) Servlet实例化后,Servlet容器调用该方法来完成初始化工作
    public void service(ServletRequest request,ServletResponse response) 用于处理客户端的请求
    public void destory() 当Servlet 对象从Servlet 容器中移除时,容器调用该方法,以便释放资源
    public ServletConfig getServletConfig() 用于获取Servlet 对象的配置信息,返回ServletConfig对象
    public String getServletInfo() 返回有关Servlet 的信息,它是纯文本格式的字符串,如作者,版本

    ServletConfig接口

    在这里插入图片描述

    HttpServletRequest接口

    在这里插入图片描述

    HttpServletResponse接口

    在这里插入图片描述

    Servlet开发

    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class MyServlet extends javax.servlet.http.HttpServlet {
        protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    
        }
    
        protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("GBK");
            PrintWriter out=response.getWriter();
            out.print("<HTML>");
            out.print("<HEAD><TITLE>Servlet实例</TITLE></HEAD>");
            out.print("<BODY>");
            out.print("Servlet实例<BR/>");
            out.print(this.getClass());
            out.print("</BODY");
            out.print("</HTML>");
            out.flush();
            out.close();
    
    
        }
    }
    
    

    在这里插入图片描述
    在这里插入图片描述

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>MyServlet</servlet-name>
            <servlet-class>MyServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>MyServlet</servlet-name>
            <url-pattern>/servlet/MyServlet</url-pattern>
        </servlet-mapping>
    </web-app>
    

    在这里插入图片描述

  • 相关阅读:
    今天整理了一下博客文章
    让我们猜猜明天凌晨一点NASA会有什么重大消息公布?
    微软2010 PDC Party郑州社区行
    记一次Shiro反序列化到远程桌面
    从公有云到渗透进内网漫游
    华为云CTF cloud非预期解之k8s渗透实战
    记一次任意文件下载到getshell
    记一次失败的实战渗透
    Subversion for Windows 安装配置
    使用Fiddler2录制HTTP操作脚本
  • 原文地址:https://www.cnblogs.com/gaochunhui/p/11700604.html
Copyright © 2020-2023  润新知