Saturday, July 28, 2007

Basic Authentication For XDB Realm With Tomcat

I've seen a few people asking this question recently: "When I try to access my instance of Tomcat with the URL http://localhost:8080, I get a dialog box asking for a username and password to log into the XDB Realm". Even with fresh installs, people face this problem.

The problem is that by default, Oracle listens at port 8080. And coincidentally, so does Tomcat, with default settings! And so, if you start Oracle first and Tomcat after, port 8080 is already in use when Tomcat tries to bind to it. In fact, if you look at the console or the logs, you'll see an error which says something like

Address already in use:JVM_BIND 8080

When you try to access http://localhost:8080, you are actually trying to connect to Oracle and not to Tomcat. That's why none of the default username/password combinations that come with Tomcat work.

The solution is simple: change the port that Tomcat listens on. This is assuming, of course, you want to use Oracle also. You could just get rid of it :D.

Changing the port is simple. You'll find lots of detailed explanations if you Google'ed for it. But to give it in short, open $CATALINA_HOME/conf/server.xml and look for a line which starts with

<Connector port="8080"

and change the port value to something different. I prefer using 8888. It's so much easier to type than 8080 or 8081!

Wednesday, July 25, 2007

Escape Character In Microsoft SQL Server 2000

Here's something I was spent half a morning trying to find out: how do I escape a single quote (') in Microsoft SQL Server 2000? I first tried the most obvious character- the backslash (\), but that didn't work. Then I randomly tried a few other characters but to no avail. So I ran a Google search but most of the stuff I got was for MySQL or Oracle and they seem to accept backslashes for escaping.

I finally managed to discover that the escape character is, in fact, the single quote itself! So to insert a string with a single quote in it, you need to use an SQL query like:

INSERT INTO mytable ( randomstring) VALUES ( 'Where''s the answer?' )

So now you know. What I want to know is, why couldn't they just use a backslash like everyone else?

You could also check this article out though it didn't really work for me but I didn't try that hard.

Monday, July 16, 2007

HTML Single Text Field Submit Caveat

This is something that I discovered by accident and thought it was a bug till I read this in the HTML specification:

When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.

I was using the name of the button clicked to identify the action to take when I started receiving a null value on some occasions. After a little investigation, I realized that if I clicked the button, the code worked fine; but if I pressed the Enter key while typing in the text field, I would get a null value for the button.

If there is only one text field in a form along with a submit button, pressing Enter would cause a form submission as usual, but, the submit button would not be passed. The text field would be the recipient of the request to submit the form.

Consider this simple form that submits to itself using the GET method ( default ) so that the query string can be used to easily verify the parameters being passed.
<html>
    <body>
        <form>

            <input type="text" name="loneTextField" />

            <input type="submit" name="theSubmitButton" value="Click Me" />

        </form>
    </body>
</html>
On clicking on Click Me, the query string is:

submitCheck.html?loneTextField=Test+Contents&theSubmitButton=Click+Me

whereas on pressing Enter in the text field:

submitCheck.html?loneTextField=Test+Contents

You can easily work around this though and it's not an issue if you don't need the button, but it's worth knowing.

Thursday, July 12, 2007

Wrapping Text In JLabels

I recently picked up Swing while I was developing Tweeter!, a Java client for Twitter. A nifty little trick that I found out about was how to wrap the text inside a JLabel.

JLabels support HTML markup. So all you need to do is surround the text with HTML tags ( <html> and </html> ) and voila! The contents wrap around! If you need it to be centered, simply add a <center> tag. It's that simple!