I would like to add an existing servlet to a context and it works, when I use (Main.java):
Tomcat.addServlet(ctx, "MyServlet", new MyServlet());
ctx.addServletMappingDecoded("/url_pattern", "MyServlet")
However, I have annotations inside servlet to map url_pattern(MyServlet.java):
@WebServlet(name = "MyServlet", urlPatterns = { "/url_pattern" })
@MultipartConfig(
fileSizeThreshold = 1024 * 1024 * 1, // 1 MB
maxFileSize = 1024 * 1024 * 10, // 10 MB
maxRequestSize = 1024 * 1024 * 100 // 100 MB
)
Unfortunately, these annotations do not work. I would like to delete mapping from Main.java and use mapping from my Servlet annotation.
I use Tomcat 10.0.0.
Main.java
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
public class Main {
public static void main(String[] args) throws LifecycleException,
InterruptedException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8082);
Context ctx = tomcat.addContext("", new File(".").getAbsolutePath());
Tomcat.addServlet(ctx, "MyServlet", new MyServlet());
ctx.setAllowCasualMultipartParsing(true);
ctx.addServletMappingDecoded("/url_pattern", "MyServlet");
tomcat.start();
tomcat.getConnector();
}
}