Results 1 to 2 of 2
  1. #1
    patr1c1a is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default A servlet that used to work and now it magically doesn't

    I haven't touched this project in a long time, but I could swear it worked the last time I tried it. Now, of course, it doesn't.

    It's a java servlet that takes some java code, compiles it and returns some html with the results (either "compile successful" or "compile error at line xxx"). It's built with only 2 classes: "Compiler" that extends from HttpServlet, and "JavaObjectFromString" that extends from SimpleJavaFileObject.

    Class JavaObjectFromString looks like this:
    Java Code:
    package servlet;
    public class JavaObjectDesdeString extends SimpleJavaFileObject{
        private String contents;
        private String name;
        public JavaObjectFromString(String className, String cont){
            super(URI.create(className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            return contents;

    Class Compiler looks like this:

    Java Code:
    package servlet;
    import java.util.Arrays;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class Compiler extends HttpServlet {
        private static final long serialVersionUID = 1L;
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter out = response.getWriter();
            StringBuffer sb = new StringBuffer();
            String code = request.getParameter("usercodefromhtml").toString();
            String results = this.compileCode(code);
            sb.append("<html><body bgcolor=pink text=black>");
            sb.append("<h1 align=center>" + results + "</h1>");
        private String compileCode(String codeToCompile){
            String results = null;
            /*Creating dynamic java source code file object*/
            SimpleJavaFileObject fileObject = new JavaObjectFromString ("userclass", codeToCompile);
            JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject} ;
            /*Instantiating the java compiler*/
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            /*Create a diagnostic controller, which holds the compilation problems*/
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
            /*get a standard file manager from compiler, this file manager helps us to customize how a compiler reads and writes to files*/
            StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(diagnostics, null, null);
            /* Prepare a list of compilation units (java source code file objects) to input to compilation task*/
            Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(javaFileObjects);
            /*Prepare any compilation options to be used during compilation
            //In this example, we are asking the compiler to place the output files under bin folder.*/
            String[] compileOptions = new String[]{"-d", "c:"} ;
            Iterable<String> compilationOptions = Arrays.asList(compileOptions);
            /*Create a compilation task from compiler by passing in the required input objects prepared above*/
            CompilationTask compilerTask = compiler.getTask(null, stdFileManager, diagnostics, compilationOptions, null, compilationUnits) ;
            /*Perform the compilation by calling the call method on compilerTask object.*/
            boolean status =;
            /*On compilation failure, we can use the diagnostic collector to read the error messages and log them in specific format.*/
            if (!status){//If compilation error occurs
                /*Iterate through each compilation problem and print it*/
                for (@SuppressWarnings("rawtypes") Diagnostic diagnostic : diagnostics.getDiagnostics()){
                    results = "Error en la línea "+diagnostic.getLineNumber();
                results = "Compilación exitosa";
            /*Finally close the fileManager instance to flush out anything that is there in the buffer.*/
            try {
                stdFileManager.close() ;//Close the file manager
            } catch (IOException e) {
            return results;

    I compiled those two into .class files in an older Eclipse version (I haven't re-compiled since then).

    I also have this form in an html file, that I suppose calls the servlet and passes it the "usercodefromhtml" parameter with the code the user writes in that textarea:

    Java Code:
    <form action="compiler" method="post">
        <textarea rows="18" cols="70" name="usercodefromhtml"></textarea>
        <input type="submit">

    (I also tried "/compiler" and "Compiler" as the action value, but no luck).

    This is how I installed and configured tomcat:

    1. I downloaded de 64 bit windows version from Apache Tomcat - Apache Tomcat 7 Downloads
    2. I added two environment variables: JAVA_HOME -> path to JDK (in my case it's C:\Program Files\Java\jdk1.7.0_40) CATALINA_HOME -> path to tomcat (in my case it's C:\apache-tomcat-7.0.53)
    3. I edited environment Path and added %JAVA_HOME%\jre\bin. I then ran configtest.bat from tomcat and it threw no errors.
    4. In the webapps folder in tomcat, I created a folder for my project, where I placed my html containing the form code (as shown above).
    5. In the project folder inside "webapps", I created a WEB-INF folder where I placed the web.xml and a "classes" folder with a subfolder named "servlet" containing the 2 compiled classes (.class files) I showed above.

    My web.xml is as follows:
    Java Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    Then, from cmd I ran startup.bat from tomcat folder. I opened the html file in my browser, entered some random code in the textarea and pressed the submit button. Nothing. No class file (as a result of compilation task) is created in c: either.

    I believe the two classes were working correctly the last time I tested them, so I'm more inclined to think this is a configuration problem... But where?

    I created the servlet a long while ago (older version of Eclipse and older version of java JDK). I remember changing the eclipse compiler from JRE to JDK when I created the project. Could it be that since I have a newer java version now something is not matching?

    Last edited by patr1c1a; 04-28-2014 at 02:51 AM.

  2. #2
    patr1c1a is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Re: A servlet that used to work and now it magically doesn't

    Nevermind... My html with the form was missing a > in the body tag. Added that and voilà!

Similar Threads

  1. Replies: 13
    Last Post: 07-25-2013, 06:30 PM
  2. I wonder why this doesn't work...
    By louboulos in forum New To Java
    Replies: 3
    Last Post: 05-13-2012, 05:59 PM
  3. Why doesn't this work?
    By mailman in forum Java Applets
    Replies: 5
    Last Post: 01-10-2012, 02:01 PM
  4. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 09:07 AM
  5. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 11:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts