简体   繁体   English

如何将web.xml转换为@WebServlet?

[英]How to convert web.xml to @WebServlet?

I have a web application with servlet 3.1 and have servlet filter without @WebFilter annotation and its working fine. 我有一个带有Servlet 3.1的Web应用程序,并且具有不带@WebFilter批注的Servlet过滤器,并且工作正常。

I want to replace it with @WebServlet annotation but using same old filter without creating new filter class and using @WebFilter in the old filter class. 我想用@WebServlet注释替换它,但使用相同的旧过滤器而不创建新的过滤器类并在旧过滤器类中使用@WebFilter。 Below is my web.xml file. 以下是我的web.xml文件。

<web-app version="3.1"
         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_3_1.xsd">
  <display-name>Archetype Created Web Application</display-name>
    <filter>
        <display-name>User Auth Filter</display-name>
        <filter-name>UserAuthFilter</filter-name>
        <filter-class>com.example.UserAuthFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>checkUser</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>doValidate</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
   <filter-mapping>
        <filter-name>UserAuthFilter</filter-name>
        <url-pattern>/usercount</url-pattern>
    </filter-mapping>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Services</web-resource-name>
            <url-pattern>/usercount</url-pattern>
            <http-method>GET</http-method>
        </web-resource-collection>
    </security-constraint>
    <deny-uncovered-http-methods/>
</web-app>

and I tried to replace it with below servlet. 我试图用下面的servlet替换它。

package com.example;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet
@WebFilter(displayName = "User Auth Filter",
        filterName = "UserAuthFilter",
        value = "com.example.UserAuthFilter",
        asyncSupported = true,
        initParams = {
                @WebInitParam(name = "checkUser",value = "true"),
                @WebInitParam(name = "doValidate",value = "true")
        })
public class NotificationWebsocketServlet {
    public NotificationWebsocketServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
}

I did not find any annotation for filter-mapping and security-constraint. 我没有为过滤器映射和安全约束找到任何注释。

Could you help me on this? 你能帮我吗?

For the @WebFilter you should be able to use the urlPatterns attribute for the filter mapping. 对于@WebFilter,您应该能够将urlPatterns属性用于过滤器映射。 In addition you should be able to use @ServletSecurity annotation for your security-constraint. 另外,您应该可以对安全性约束使用@ServletSecurity批注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM