Tag Archives: http

URL’s Passing Parameters

So after chatting with everyone’s favorite M5’er Sean Kaliber I came to a realization that not everyone in the world understands how web forms, and URL parameters work. So to rectify this situation, I am spawning this new post. We all use the internet every day basically but most of us don’t think twice about the flow of information from page to page, or how even a simple query get to googles search engine. The answer is html forms and url parameters.

So when we go to a webpage that has a simple text box, and a submit button and enter some information and hit that button what exactly happens. Its quite the simple. If that HTML <form> tag has an action parameter associated with it the page that is referenced in this action field is called and any input fields associated with this form are appended on the request url as parameters.

Parameters are appended on a URL in a very simple way. Lets use a simple google query as an example. Say you want to google the word “hello” without actually going to google and typing in the word hello and clicking the search button. This can be accomplished by adding a few parameters to your URL request. Let me show you.

So www.google.com will take us to googles main page. If we enter “hello” and hit submit we will get a new page and the url at the top will change to the following.

http://www.google.com/search?q=hello you might see other parameters appended on but the most basic extension is this one shown. The ‘?’ means that the following fields are key value pairs and the q=hello says that variable q is equal to the string hello. This field can then be passed to whatever page or jsp page that processes this request to render the search results.

Try it for yourself. Change the hello in the above link to whatever you like and see how the query changes. Better yet host your own local webpage that has a html form and another page that takes the recieved parameters and puts them as text on the page. Try checking out the JSP post I made earlier in the semester to make this happen.


Let the Servlet’s Serve Us – Java Servlets 101

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

-TJ http://umassamherstm5.org/about/people/staffers/tj-pavlu