Code Monkey home page Code Monkey logo

servlet-guide's Introduction

Java Servlet

定义

Servlet 是一种技术,被用来创建一个JAVA WEB Application.

  • 接收请求
    • 从请求中获得数据
    • 处理请求
  • 响应请求
    • 写入响应内容

Java Servlet

实现 servlet-api 提供的 Servlet接口,并且重写如下方法,但更好的方式是:继承并重写 HttpServlet 用于实现http方式的 请求处理

  1. implements Servlet Interface
  2. extends GenericServlet
  3. extends HttpServlet 接口
public interface Servlet {

    public void init(ServletConfig config);

    public void service(ServletRequest request,ServletResponse response);

    public void destroy();
    
    // ...
}

ServletContext

Servlet上下文,每一个Java Web Application 中有且仅有一个,在war包部署到tomcat等容器之后,读取web.xml. 将所有信息释放到上下文中。

https://www.javatpoint.com/servletcontext

Servlet 生命周期

https://www.javatpoint.com/life-cycle-of-a-servlet

  1. 类加载器加载Servlet class
  2. 创建Servlet class instance
  3. 调用init() 方法初始化
  4. 收到请求后进行处理,调用service(-,-)方法
  5. 销毁destroy()

Filter

过滤器能干吗

https://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html

  • 指定请求和响应的编码
  • 压缩图片
  • 认证
  • 指定那些资源可以被访问 那些不能
  • token 过滤器

定义一个过滤器

public class HelloServlet extends HttpServlet {
  void init(FilterConfig filterConfig);
  void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;
  public void destroy();
}

过滤器匹配请求

Servlet API

getParameter

Servlet API的 getParameter 接口,只支持获得GET请求的query参数 或者是 POST请求的from-data参数。 但是对于PUT/DELETE/PATCH 类型,则无法处理。

Web.xml

load-on-startup

If you pass the negative value, servlet will be loaded at request time, at first request.

指定servlet 加载顺序,如果不指定,则默认在首次请求到达的时候才进行初始化Servlet 如果指定:从0开始按照顺序加载,在服务器启动后。

REFERENCES

servlet-guide's People

Contributors

marchocode avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.