More Handlers – Web Development

More Handlers – Web Development


Okay, let’s make some changes to this file. The first thing we’re going to do is we’re going to get rid of this Google action and replace it with /testform, and now that’s going to cause our form when we submit it to submit to /testform instead of to Google. And since our application can only respond to slash, we’re going to need to add a handler for that as well. We’ll come down here to our URL handler section, and because we only have a handler for slash, we’re going to add another handler for /testform. /testform will be handled by the handler called TestHandler, which doesn’t exist yet. Let’s create that. Okay, so we’ve added this new handler called TestHandler. This also has a method on it called get, like our other handler, and what this does is it sets a variable called q, which comes from self.request. Like response is the object that represents the response we’re going to send back to the client, request represents the request that came from the browser, and you can call get on it to get different parameters, so in this case we’re going to get the parameter q. And then all we’re going to do is we’re going to turn on the response self.response.out.write(q). So if we go to our browser, we enter some words, and we hit submit, we see the string hello world! You can see in the URL q=hello World! which is what we answered, so this is hitting the same URL we were hitting before, but this time there’s a web application on the other end listening for requests and returning responses. The exclamation mark turned into a %21. That was the escaping I was referring to before. Okay, now I’d like to make another change. I’m going to comment out these 2 lines and show you something neat. I’m going to take this content-type line from up here because we want to print some text, and we want it to work properly. This time, instead of writing out q, I’m going to write out self-request itself. Okay, let’s go to our browser and try this out. Here’s our form. You may have to go back to URL and reload. That’s fine. Type some text in the form and click submit. When you click submit, what just happened? Did you see garbage? Did you see the HTTP request? Do you see a message from the future, or nothing happens at all?

1 Comment

Leave a Reply

Your email address will not be published.


*