• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:




Class Compiler looks like this:




I had 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:



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

This is how I installed and configured tomcat:

1. I downloaded the 64 bit windows version from http://tomcat.apache.org/download-70.cgi
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:




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? Thanks!
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Miguel wrote:I believe the two classes were working correctly the last time I tested them...

I don't think JavaObjectDesdeString will compile - lines 13 and 14 have a closing bracket which doesn't seem to belong.
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ron
Actually, I haven't recompiled the classes. I just took the two compiled classes from that old project that I had created long ago and placed them in the classes/servlet folder.
Maybe I accidentally missed a bracket when copying the code (sorry!).

I'm thinking... since I compiled the project a long while ago (older version of Eclipse and older version of java JDK), and 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?
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia - I ran your code on JBoss6AS (didn't use the web.xml though since there was already an @WebServlet annotation) and it worked fine - got Compilación exitosa.

Make sure that JAVA_HOME is set to a JDK (not JRE), otherwise ToolProvider.getSystemJavaCompiler() will fail and return null.

The URL will be something like http://localhost:8080/JavaRanch/index.html, where JavaRanch is the name of the project, and index.html is the name of the HTML file.
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, Ron. Thanks! At least I know the compiled project is actually working
"Compilación exitosa" is Spanish for "Compilation successful" (sorry, forgot to translate that bit!).
I'm not sure what's wrong with my configuration then... I have JAVA_HOME set to C:\Program Files\Java\jdk1.7.0_40

Maybe I'm missing some other Tomcat configuration step...
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed that your web.xml file has a mapping to a URL using Compiler, but your HTML file specifies compiler. You will need to fix one or the other so that they agree.

<servlet-mapping>
<servlet-name>Compiler</servlet-name>
<url-pattern>/
Compiler</url-pattern>
</servlet-mapping>


<form action="compiler" method="post">
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again
Yes, I tried the different variants in the <form> action attribute ("compiler", "Compiler", "/Compiler") but none of them work.
Argh, it's driving me crazy!
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean when you say none of them work? Are you seeing any output? Any messages to the browser or Tomcat's console?

Are you using the embedded browser in Eclipse, or an external browser like Chrome or Firefox?
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, no output at all.
I'm opening the html file in Firefox (after I start tomcat).
The html only has the form:


I enter some basic code, something like class test {} and then click on the submit button. Nothing happens.
I wrote the code a long time ago, but from looking at it I think the output files (compiled code) are supposed to go to the c:\ folder. I checked and nothing gets created there either. I've checked the tomcat log catalina.2014-04-27.txt but it only shows logs about tomcat being started.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should also be an access log in Apache's logs directory. This is what I see in mine:

127.0.0.1 - - [27/Apr/2014:19:37:52 -0700] "GET /JavaRanch1/index.html HTTP/1.1" 200 326
127.0.0.1 - - [27/Apr/2014:19:38:11 -0700] "POST /JavaRanch1/compiler HTTP/1.1" 200 92


Also, most browsers have a type of debugging mode where you can monitor what is sent and received by the browser. Try that and see if you can get any clues. This is what I see with Chrome:

 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, actually, I'm not using apache here. My file with the form using the servlet is plain html, so I thought apache wasn't needed... Is it?

All I do is start tomcat, then in Firefox I open the .html that contains the form, enter some test java code and click the submit button. The browser console from firefox shows nothing when I click the button. I also tried the Net panel from firebug but nothing shows up there either.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat is actually Apache Tomcat - you don't need anything else.

When you say then in Firefox I open the .html that contains the form, how do you do that? Are you opening the html file is the file explorer, or using your browser perform an HTTP get - browsing to something like http://localhost:8080/JavaRanch1/index.html ? If you shutdown Tomcat, and try to access the html page, does your browser indicate an error?
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried in two different ways:
1: I drag the test.html file (the one containing the form that calls the servlet) to a firefox tab. If tomcat is down, this method still works and shows my file correctly (it's an html file stored in some folder after all, so I believe it's expected that it works).
2: I point my browser to http://localhost:8080/myproject/test.html (also tried http://localhost:8080//myproject/test.html). This way the page doesn't show up when tomcat is down.

None of the two methods work. I mean, the page is displayed but clicking on the "submit" button seems to completely ignore my action.


Just to be clear, this is how my project is stored within the tomcat webapp folder:

$tomcat/webapps/myproject
------------------------|
------------------------+===> test.html
------------------------+===> /WEB-INF
--------------------------------------|
--------------------------------------+===> web.xml
--------------------------------------+===> /classes
-----------------------------------------------------|
-----------------------------------------------------+===> /servlet
--------------------------------------------------------------------|
--------------------------------------------------------------------+===> Compiler.class
--------------------------------------------------------------------+===> JavaObjectFromString.class


 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method #1, launching the web page from the file explorer will not work unless you change the action to include the full web URL, but don't do that.
<form action="http://localhost:8080/myproject/Compiler" method="post">

With the second method, getting the html file from Tomcat, you should see an entry in Tomcat's access log file. On my workstation, it is named apache-tomcat-7.0.27/logs/localhost_access_log.2014-04-27.txt . The log should show a GET access for the html file, and a POST access when the form is submitted.

One other thing, web browsers sometimes cache content for you, and even-though you have made a change on the server-side, the browser may try and be clever and think that it already has what you need. Try forcing the web context to be reloaded just in case that has something to do with your problem. On Windows, applications generally use the F5 key for that.

 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the localhost_access_log.2014-04-28 file and it has a bunch of these:
127.0.0.1 - - [28/Apr/2014:01:02:08 -0300] "GET /objetosenlaweb/prueba.html HTTP/1.1" 200 175

("objetosenlaweb" is the project name, and "prueba.html" is the test html file with the form).

This is how I load the file in the browser (using Chrome this time):


Clicking on "submit" does nothing at all (I made the screenshot after clicking it).

I'm actually pressing ctrl+f5 every time I test, to make sure all content is retrieved fresh, avoiding cache.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like your image didn't work properly - you can attach the image rather than embed one using the Attachments tab.

After you have navigated to prueba.html, select the option to view the page source, and post that here.

Also, are you on a Linux platform? If yes, do you have access to the tcpdump command? You could use that to monitor the network communications between the browser and the server.
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, sorry about the image. I'm attaching it here.
I'm on Windows 7, 64 bits.
This is the source code I get when clicking ctrl+u:



(I switched from "compiler" to "Compiler" in the form action now, and also tried /Compiler just in case, but didn't work).
test.png
[Thumbnail for test.png]
test
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The body tag in your html document is missing the closing angle-bracket: <body
 
Pat Mig
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GOOD LORD!!

I can't believe I've struggled for hours until almost 2 AM without noticing the missing bracket!!
Now it works...

FACEPALM · FACEPALM · FACEPALM


Thanks so much for all the help! And for not making fun of me :P
facepalm.jpg
[Thumbnail for facepalm.jpg]
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been my experience that when doing software development often complex tasks turn out to be much less work than expected, but problems with simple tasks will more than make up for it. More than once I've spent 2-3 days swearing at something because a comma was missing or because code was doing the right thing but I was confused and expecting something else. Getting someone to take a look at stuff who doesn't "know" what's there can often be a big help.

Stuff like that is why I gnash my teeth when people say "It's simple. No time at all. All You Have To Do Is..."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!