Yo yo yo… Hope everyone is having a grand old Thursday. Actually if you are reading this I am currently on a plane to Seattle…. can u say WIN :). O
Ok.. so as per the title above this post is about servlets. Now since I know you all know everything there is to know about the Internet, networking, http, etc. I am just going to skip to the good stuff… just kidding.
So it all starts with understanding the idea of a website, specifically http. OK, so you want to go to google.com, your browser or whatever application that is going to get the site for you sends an application layer (wikipedia: OSI layers) http request to the webserver that google.com points to (wikipedia: DNS). All this is done via socket connections, normally over port 80, the default http port. Magically this request gets to the server and the server’s web hosting application must process it. This application interprets the request and then responds by sending back a stream of text, the html code of the webpage. Then your browser parses this code and finds other neccessary files. For example, it may pull images from other webpages in order to render the page. It requests these files from the server and the server responds in turn. So we have this sequence of events:
User ->(GIVE ME GOOGLE.COM)->browser Request index.html from google.com -> Server browser
<- Response index.html as text stream
browser Request img1.jpg from google.com -> Server User <- GOOGLE.COM <- browser <- Response img1.jpg as byte stream
This request/response structure is how all webpages are built by your browser… ok, wait, weren’t we talking about servlets? Right, so where do they fit in? Servlets can be thought of as mini-page servers, so if we take our block diagram for website hosting and we insert a java program that can handle these requests on the server by executing you write you would have a servlet container. I will show you.
So your Java code can run within the servlet container and serve web pages to your user. There are two ways to approach servlets, creating actual java servlets, extending that class from the api etc or you can use pages called jsp’s. Jsp’s stand for JavaServer Pages. These pages are java code are compiled by the container when a request for them is made, and can be very useful and simple to implement.
Ok before I wrap up a quick example on how/why to use a JSP. Say we want a website that can keep track of how many users have visited it, this would be a great place to use a jsp. First off install tomcat or another servlet/jsp hosting software on your local machine. I personally like tomcat, here is a link on how to get started with that http://tomcat.apache.org/
Next we have to write a jsp. So once you have tomcat running you will have to make a JSP file in a new web-application folder. To do this make a new file called index.jsp in new folder called watever you like, i will call mine bob in this directory TOMCAT_INSTALL_DIRECTORY/web-apps/. Now open index.jsp in your favorite text editor I recommend notepad++(google is your friend). Once in there paste in this code
int count = 0;
First thing to note about above that all code between the <% %> is java code that will be compiled by tomcat when your webpage is polled. the <%! means that block is global and that variable will reside between webpage calls on the server and can be updated. So this page when called will simply print out the value of count after it is incremented, visit http://localhost:8080/bob/index.html in your browser to see this effect. out is the default print out stream for an http response. Every jsp is passed impliciet information from the request and is given the ability to modify its response to that request. This is done by interacting with the request and the response objects that you magically have access to in your jsp. To see kinda what im talking about add the line out.println(request.getParameter(“HELLOWORLD”)); after the print out of count. Now when you call your webpage in tomcat at http://localhost:8080/bob/index.html it should print out the number and null. If you modify this page request with the parameter HELLOWORLD you can have it print out what you pass. So try this, http://localhost:8080/bob/?HELLOWORLD=OMG_IT_WORKS!!!
Hope you enjoyed getting your feet wet in Servlets, hit me up on facebook or at m5 with any questions