Java – The Power Language (for Web or Console)

Recently, I wrote my first Facebook application. It was a small project, and it’s main purpose was to replace the common note entitled something like “Xth grade Schedule!!!” and it was designed to be more efficient for the end user. After inputting your schedule, you would go through and see who is in your classes (that the system already knows). There were other cool features like using some Facebook Social Plugins like the “Like” button and Facebook comments.

Now for this application, as I wrote it in a matter of days and released it the day of schedule releases, I had to go with a language I knew very well, and I had to have a reliable, dependable source to host it at. I automatically though of the Google App Engine as it is pretty sweet to play around with and now would be a great time to test it in a live environment. My previous application I had wrote on Google’s free, scalable engine was written in Python. It utilized the amazing Django framework, but I felt like I didn’t really understand the Python language as well as some others. The only other option for selection on the App Engine was Java. This was perfect as I had just completed a summer class on Java and it was extremely fresh in my mind.

Core Java Servlets

Now coming from PHP for web development the idea of Servlets and JSP’s was totally a different way of thinking. With PHP I was used to putting what code I needed to on each page to achieve what needed to be done. With a bit of Python/Django experience, Java’s way was still different, but not as different as PHP to Java. Java’s JSP method reminded me a lot of Django templates, except that JSP’s are a load more powerful. As Django templates (at least from my experience) only allow a few tags, a JSP allows practically any Java code (that would normally go inside a main method) and allows imports and special features unique to JSP’s, like includes. JSP’s includes in fact reminded me some more of Django’s templates with all the splitting up possible of the HTML files.

Now that we briefly know what a JSP is, a servlet is just like a Java class that implements the standard Apache servlet. Within these servlets you have two methods by default doGet(HttpServletRequest req, HttpServletResponse resp) and doPost() with the same arguments. There are a few differences from a standard Java class beyond those previously noted:

resp.getWriter().println(); // to print to the screen you must get the PrintWriter via the resp.getWriter() method
resp.setContentType("text/html"); // must do if you print out any HTML and don't want it to appear as standard text
resp.sendRedirect(String url); // a simple way to send a user to be redirected

There is (obviously) no console input on a web browser, so the standard way to get input via the web is through GET and POST methods. These are handled via the corresponding doGet() and doPost() methods. Now to actually get the value passed, you must know the variable name and use it accordingly.

req.getParameter(String paramName); // where paramName is the ?name=Joe&age=18 name or age

Sometimes you will also find yourself in need of getting an array, from say a multiple choice drop down.

req.getParameterValues(String paramName); // just once and it will return a String[]

Now onto more of the theory behind servlets, and less of the syntax. Now please remember when I say theory, this is just the way I perceive it. In my application if I would have a full blown HTML page, I would always put it in a JSP as it is so simple and better looking. However when there was a simpler page that had a bit more code behind it, I would make that a servlet. An example of a simpler page with more code that I used as a servlet would be my form handlers (any adding or updating interaction with the database), and also I handled the OAuth authentication with servlets.


JSP’s, or Java Server Pages, are a mix between Java code & client-side scripting. It allows you to mix the power of Java and your the models in your MVC framework with the simplicity of a HTML file. You probably see them all over the web, as they are quite effective, especially in larger businesses and universities.
I have not personally gone that far into all the powers of JSP’s but enough to know that I like them. I am not going to try and write a tutorial but just point out a few things I use and a good tutorial or two.
Now one of the simplest, but greatest is the server-side include of static content. This allows you to keep your web pages on the server side leaner and able to easily change base things like headers or footers.

<%@ include file="/include.html" %>

Other useful things to know, if you need to get any parameters or need to mess with the request or response, they are simply called request and response in all JSP’s. So you could use a request.getParameter("id"); for example.
If you need to write anything to the page from within the Java code just throw a out.println(String value); or out.print(String value).

For anything more than what I included check Google for some JSP tutorials or here is a good looking one.


I see there being several benefits of using Java as the language behind more complex applications because of it is an object oriented language. It being OO allows code to get so many more times complex. Now, in web development at least, a OO language is only as good as the database that supports it . I happened to be very lucky with Google App Engine as it was powered by a OO database allowing me to store some of my unique data types like “Student”, “Teacher”, “Course”, and “Schedule” in the database and retrieve it like it has been open since it was created. Most of the interface for this was provided in the JDO classes in javax.JDO.
With this and a few classes for getting and selecting certain Students/Teachers/Courses, it was very easy to get exactly the results I want in a few lines of code in a JSP. And I know that in Python and PHP you can have classes and objects, but it’s just not the same as it is in Java. While Java can be pretty strict with it’s types and defining them, it in turn gives a lot of power to create awesome and dynamic applications for the end user.


As I have been reading in my “Clean Code” book, it is better to take longer and write software better the first time. Better being Javadoc’d and Object-Oriented. If you were to write it the other way, with say PHP or Python and have each script access the relational database, get the results, sort the results into the data you need, and output it correctly, that is so much code duplication. One script could be anywhere from 20% – 50% of the same code over and over, why not just write it great in the beginning in an OO way so the only code duplication you have is one or two lines to load the pre-written-by-you libraries that does all that work in one place, one time.

And sorry Brent I think I like Java Servlets & JSP’s a wee bit more than Python/Django even though I think Django is a great framework and would love if it were written in Java instead, Python just isn’t as big and powerful as Java.

blog comments powered by Disqus