Create servlet gives me 404 error

Hi.

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

 <servlet>
    <servlet-name>MyclassReader</servlet-name>
    <servlet-class>com.path.way.MyclassReader</servlet-class>
    <load-on-startup>9</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyclassReader</servlet-name>
    <url-pattern>/myclass/*</url-pattern>
  </servlet-mapping>

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("<html><body>");  
        pw.println("Welcome to servlet");  
        pw.println("</body></html>");  
        
        pw.close();//closing the stream  
    }

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

http:/localhost:8080/cms/myclass

Any idea what I missing???

Thanks

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:

  <filter>
    <filter-name>NavAppRedirectFilter</filter-name>
    <filter-class>org.hippoecm.frontend.filter.NavAppRedirectFilter</filter-class>
    <init-param>
      <param-name>customAcceptedPathPrefixes</param-name>
      <param-value>actuator,myclass</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>NavAppRedirectFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

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.

Great Jasper. Working fine.

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