Servlet

本文阅读 1 分钟
首页 代码,Java 正文

img 案例:

//servlet依赖坐标
<dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>


package com.study.web;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/demo1")
public class ServletDemo1 implements Servlet { 
    @Override
    public void init(ServletConfig servletConfig) throws ServletException { 
        System.out.println("Hello!");
    }

    @Override
    public ServletConfig getServletConfig() { 
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { 

    }

    @Override
    public String getServletInfo() { 
        return null;
    }

    @Override
    public void destroy() { 

    }
}

在控制台会打印Hello!。

img 案例:

package com.study.web;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/demo2")
public class ServletDemo2 implements Servlet { 
    //第一次访问servlet的时候调用,该方法只调用一次。
    @Override
    public void init(ServletConfig servletConfig) throws ServletException { 
        System.out.println("init.......");

    }
    //每次访问Servlet时都会被调用
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { 
        System.out.println("Hello Service");
    }
    //释放内存或容器关闭时调用,只调用一次
    @Override
    public void destroy() { 
        System.out.println("destroy....");

    }
    @Override
    public String getServletInfo() { 
        return null;
    }

    @Override
    public ServletConfig getServletConfig() { 
        return null;
    }
}

img

img 案例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/demo3" method="post">
    <input name="username" type="text"><input type="submit" value="提交">
</form>
</body>
</html>


package com.study.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        System.out.println("doGet...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        System.out.println("doPost...");
    }
}

img img img

img

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125235961
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复