ISSUE
Running servlets on Coldfusion 9
SOLUTION
ColdFusion 9 is basically a J2EE Application running on an Application server known as JRUN
since Application Server have a web Container by default they are capable of processing servlets
so in short you can run Servlets on ColdFusion 9.
Official Documentation :
Interoperating with JSP pages and servlets
About ColdFusion, Java, and J2EE
TO TEST
-Create a cfm page and call the Servlet [in our case the servlet name is "testServlet"] with content.
note we are using “GetPageContext().forward(“testServlet”);”
to forward the request to the servlet.
<html>
<head>
<title>This is a test CFM page calling a Servlet page</title>
</head>
<body>
<h1>This is a test CFM page calling a Servlet page named “testServlet”</h1>
<cfscript>
GetPageContext().forward(“testServlet”);
</cfscript>
</body>
</html>
-Create a Servlet name it “testServlet.java” with content testServlet[please refer the code mentioned at the end]
once you have compiled the Servlet.
-Copy the compiled Servlet “.Class” file to location
For ColdFusion Standalone : C:\ColdFusion9\wwwroot\WEB-INF\classes
For ColdFusion Multiserver : C:\JRun4\servers\<coldfusion instance name>\cfusion-ear\cfusion-war\WEB-INF\classes
-Add Servlet specific mapping entries to ColdFusion “web.xml”
The “web.xml” can be found at location
For ColdFusion Standalone : C:\ColdFusion9\wwwroot\WEB-INF\
For ColdFusion Multiserver : C:\JRun4\servers\<coldfusion instance name>\cfusion-ear\cfusion-war\WEB-INF\
The entries should look like :
<servlet>
<description></description>
<display-name>testServlet</display-name>
<servlet-name>testServlet</servlet-name>
<servlet-class>testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
[ These entries to "web.xml" would not be required if you are using annotations
i.e. @WebServlet(name = "testServlet", urlPatterns = {"/testServlet"}
please note that these annotations only work on JDK versions which support the same so please be mindful regarding the same.
for the sake of this test i have commented out the annotations]
-Restart the ColdFusion server once all files and entries are in place request the CFM page.
you would see output as[instead of the HTML you specified in the CFM page]
testServlet CODE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author chandran
* @WebServlet(name = “testServlet”, urlPatterns = {“/testServlet”})
*/
public class testServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8″);
PrintWriter out = response.getWriter();
try {
out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Servlet testServlet</title>”);
out.println(“</head>”);
out.println(“<body>”);
out.println(“<h1>Servlet testServlet at ” + request.getContextPath () + “</h1>”);
out.println(“This is just a test servlet”);
out.println(“</body>”);
out.println(“</html>”);
} finally {
out.close();
}
}
// <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”>
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return “Short description”;
}// </editor-fold>
}

