Create servlet gives me 404 error


I am trying to implement a servlet inside cms project but it is not working at all.

My steps:

1.- Create in web.xml file



2.- Create a java class extending HttpServlet

public class MyclassReader extends HttpServlet{  
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException  
        res.setContentType("text/html");//setting the content type  
        PrintWriter pw=res.getWriter();//get the stream to write the data  
        //writing html in the stream  
        pw.println("Welcome to servlet");  
        pw.close();//closing the stream  

After doing mvn clean install and CMS starts but gives me 404 when request the page


Any idea what I missing???


Hello, first of all please be aware that there are multiple web.xml (for cms, essentials and site), so make sure that you are working in the correct webapp.

I would also like to ask, what are you trying to achieve here? A raw servlet is probably not the best choice for whatever you are trying to do :slight_smile:

This one will be hard to figure out, but basically there is a filter in your way which will redirect away from unapproved paths. The filter is the “NavAppRedirectFilter”. You can override this in the web.xml, but I’m not sure what the effects will be. Probably fine, but I cannot guarantee. Something like this:


Why do you want this?

Thanks Jasper for your answer.

I would like to have a servlet because I want to access some parts outside the CMS but I need to use some object created in the CMS. I am not sure if I am explaining. Let´s say that is a little experiment form myself.

I will try the filter, but I can´t find it in my project, so can I add it to my CMS´s web.xml???

Thanks in advance.

The filter is contributed by a web-fragment in an included library. It’s not in your project but in hippo-cms-engine. web.xml configuration will override the web-fragment. Use the config I have given above, ie the “actuator” is what is in the web-fragment so it needs to be included.

I would configure this as the last filter in your web.xml. The order matters.

1 Like

Great Jasper. Working fine.

Thank you very much. I would have never made it without you. :clap: :clap: :clap:

Hi @jasper.floor,

Upgrading a project where there are CMS servlets in use I’m happy to find this solution.
I do have a question: In my case the filter only seems to work if I set url-pattern to /cms/*. Could that give problems?

Kind regards,

Filter mappings should already be relative to the context root of the application. If anything I would expect the filter to not work with that mapping, but perhaps there is something else going on in those projects I am not aware of.

Hi @jasper.floor ,

Yes, I thought so too. I haven’t yet had opportunity to check this on OD2 platform, so will find out if this was only local necessary.
Thanks for your reaction!

Kind regards,