Pat Mig

+ Follow
since Feb 26, 2012
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Pat Mig

I'm trying to learn JSP + Servlets by building a simple page that allows users to register and log in. I'd like to use a Postgres database for that. I'm using Eclipse as an IDE and Tomcat as a container.
I managed to get my JSP page working and now I'm building my servlet, but I'm not sure about how to approach the database connection. The info I found online is a bit confusing as there seem to be different ways of connecting to a database, is that so?

For example, I've seen this pooling thing where a context.xml is created and then the db is accessed by creating a Context object.
But I've also seen a different approach where Class.forname(drivername) is called to then create a Connection object from a DriverManager.
On some other tutorials I've seen people create a database connection profile through Eclipse Data Source Explorer, where they import a driver they download from

I'm not sure if there are more ways or those are the main ones. I'm also confused about what are the differences between them and which approach would be the best, given that I am using Eclipse. I downloaded the jdbc driver but I don't know if or how I should use it.
Any help or links to a good tutorial that might clarify this will be very welcome

5 years ago
Thanks a bunch, guys
Everything you mentioned is really interesting and opened my mind to possibilities I didn't know about.
And lots of great stuff to read!
7 years ago
Thanks, Ulf
About how to run the code, I'd go with the easiest choice. I guess that would be from within the servlet container, but I'm not too aware of the implications.
And I got a bit lost with the interface implementation. What would be the exact purpose of it in this case? How would it help me test?

What I actually did was replace the second line of the "compile" code (String toCompile = "class test {" + codeToCompile + "}") with a call to a method that receives 2 parameters: the code itself to be compiled and a number indicating what "kind of exercise" it is. That way, if I ask the user to just declare a variable I can surround his code with class test {" + code + "} but if I'm asking him to write a for loop I surround it with class test {public void testMethod() {" + code + "}} and this way the user only focuses on just typing the required code, without having to wrap it in a whole class structure.
That's as far as I got, other than the classes I posted above :P
7 years ago
I built a servlet that compiles java code and returns compilation errors (if any). I'm calling it from an html form where I type the code.
The servlet is working great (mostly thanks to Ron McLeod, who helped me A LOT on this forum).
The thing is, it just compiles code and tells you the results, but it doesn't test the code. Since I'm using it for educational purposes (teaching java to beginners), I thought it would be nice to check that syntactically correct code actually does what the user intends it to.
Currently the servlet is set to create a .class file on my c: drive if the compilation succeeds. Maybe there's a way I can run that class somehow?

This is my servlet class:

It uses a class called "JavaObjectFromString" which looks like this:

The code I'm expecting to compile is very simple, however it covers a wide variety of situations: in some cases the user is only asked to declare a variable and assign a value to it, sometimes they have to write a while loop to increment a variable, in some other cases they need to write a whole method.

Is this testing I'm trying to do something too complex? I'm not sure where to start...

7 years ago
Duh! Right! Since I'm wrapping everything with class test { } the code is like hanging there with no structure.
You're my hero of the month :P
7 years ago

Ron McLeod wrote:Since you are specifying that the content is URL-encoded, you should encode it before POSTing it to your servlet with something like ..

llamadaAjax.send('codigo=' + encodeURIComponent(codigo));

And once again, Ron, you saved my day
That worked perfectly!

However, I still see some weirdness with specific code structures. They do get correctly posted now, but they seem not to be correctly handled by the compiler (and this time the servlet is the problem, not the html where it gets its request from).
For example, if I type this in the form:
and send it to the servlet to be compiled, I get and "illegal start of type" error.

If I try:
int n=0;
if (n==0)

I get the "illegal start of type" error on line 2.

I debugged and the code is never altered, so it doesn't seem to be a matter of encoding this time. When adding a breakpoint right after this line in the servlet: String results = this.compile(code); the "results" variable is correctly set with the code. So the problem has to be somewhere in the "compile" method. I just don't know what exactly it could be...
7 years ago
Actually, I found out that ajax is breaking my code right before it's sent.

I have this HTML file that captures the text typed in a textarea within a form, and using ajax sends it to my servlet.

I found out that using just the form without any ajax the characters are correctly sent. Also, I debugged the javascript and saw that the correct string is assigned to the variable:
var codigo = document.getElementById('codigo').value;
the variable "codigo" gets correctly assigned with the contents of the textarea.

But then I also inspected the servlet request with chrome dev tools and the request object is the one that strips out characters (see attached image).
I'm really new to ajax, so I'm trying to find out how to prevent the request object from altering the code.
7 years ago

Ron McLeod wrote:Right -- I didn't see that in the code - sorry.

I just tried your code with:and I got a result message of SUCCESS and a test.class file created.

I did need to change diagnostic.getMessage(new Locale(null)) to diagnostic.getMessage(null) though to avoid having an exception thrown.

Ron, you're saying you ran my exact same code and tested it with that method and worked? Because I'm trying it inside and outside Eclipse, and I get error on line 2, ';' expected
I'm running it as a system admin, so I don't believe permissions is the issue here.

My guess was that you were using some other character which looked exactly the same as a + character. But what you posted was a + character... but maybe you didn't copy and paste the code with the problem? Anyway the next thing I would have tried was some other operator, like for example a - character.

Actually, my guess after that was that some failure in URL-encoding was happening and that the + character was being URL-decoded to a space. Did you try debugging the code and seeing what was actually there? (And why do you need to call toString() on the result of request.getParameter()? Isn't that already a String?)

Paul: I'm 100% certain I'm using the correct + symbol. I copied and pasted the code. I even tried it in a fresh new project in Eclipse and it compiles correctly there. Other symbols I've tried do work as expected: -, *, /
However, I just tried the % operator and got a 500 server error. So it seems it's not just the + but other characters are getting stuck as well.

When debugging, I noticed the + is stripped out on line 4 of the doPost method:
String code = request.getParameter("codigo").toString();
since the variable "code" contains public double testMethod(){return 4 5;}
I tried removing the .toString() part and I get the same, so the String conversion is not to blame here

So the problem is with how the parameter is obtained. Somewhere in the parameter passing the + is getting lost...
7 years ago
Thanks, Ron
I do have a class definition, as I add it before the code is compiled:
String toCompile = "class test {" + codeToCompile + "}";

Any other code works. Everything compiles correctly until I try to use a + operator (either to concatenate strings or to add up numbers).
It's like the compiler chokes on the + for some reason... Maybe I should be escaping the character somehow?
7 years ago
I built a servlet that compiles java code and returns compilation errors (if any). I'm calling it from an html form where I type the code. The thing is, whenever I use the "+" operator for arithmetic addition or to concatenate strings, it just doesn't like it.
Other than that, the servlet works, so this is not a servlet specific question.

It instantiates a class called "JavaObjectFromString" which looks like this:

Everything works fine until I attempt to use the + operator either with Strings or with numbers.

I've been testing different scenarios. For example, some code like this:

trows Error on line 1: ';' expected. Also happens when I use the operator inside a method:

If I then try:

it throws Error on line 2: not a statement

If I do:

I also get Error on line 1: ';' expected

I've tested it in eclipse and also directly from within the tomcat container, using an html with a form that has a textarea like this: <textarea rows="18" cols="70" id="codeToCompile" name="codeToCompile"></textarea>
I enter the code in that textarea and when I click on the submit button the code is sent to the servlet, compiled and results are returned (I capture them with ajax, but that's not important here).
Everything in the compilation works fine, until I use the + operator. What could be wrong?
7 years ago

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


Thanks so much for all the help! And for not making fun of me :P
7 years ago
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).
7 years ago
I checked the localhost_access_log.2014-04-28 file and it has a bunch of these: - - [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.
7 years ago
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:

------------------------+===> test.html
------------------------+===> /WEB-INF
--------------------------------------+===> web.xml
--------------------------------------+===> /classes
-----------------------------------------------------+===> /servlet
--------------------------------------------------------------------+===> Compiler.class
--------------------------------------------------------------------+===> JavaObjectFromString.class

7 years ago
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.
7 years ago