How to Enable CGI Mode in Apache Tomcat
Having trouble enabling the CGI mode in your Apache Tomcat? Check out this tutorial to learn more!
Hello Coders, I hope you are all doing well.
Today, I had to enable the CGI mode in Tomcat, while facing lots of issue in the same. So, I thought that, after successful deployment, I would write an article in the same simplified steps. Here it is!
- First, download the Apache Tomcat (choose the version that is compatible with your application; in my case, I am using the latest one).
- Go to the
CATALINA_HOME/confand modify the following files:- web.xml (Here, we will enable the CGI support in Tomcat)
- To enable CGI support in Tomcat, we have to uncomment servlet and servlet-mapping of CGI servlet in web.xml.
- After uncommenting the servlet and servlet-mapping of CGI, we have to add one param value in that servlet mapping to execute our CGI.
- context.xml
- We have to set the attribute "privileged" to true for Context.
- web.xml (Here, we will enable the CGI support in Tomcat)
<!-- web.xml -->
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>executable</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
<!-- context.xml -->
<Context privileged="true">
</Context>
- Go to the
CATALINA_HOME/webapps/ROOT/directory.- Check weather
WEB-INFdirectory is exist or not, if not create the directoryWEB-INF. - Create another directory named as cgi under
WEB-INF.
- Check weather
- Now, your Tomcat is CGI enabled. You can copy any CGI file under this directory, and it will run successfully.
- Your base URL will be <<request protocol>>>://<<server-ip>>:<<server-port>>/cgi-bin/<<name of cgi>>, in my case i.e. http://localhost:8080/cgi-bin/test.cgi
- For more understanding, you can look for CGI-howto section in Tomcat
Let me know if you face any issue after this deployment. Happy Coding!
Apache Tomcat
Opinions expressed by DZone contributors are their own.