Friday, August 28, 2009

Chapter 07: Working with Forms



 

Download Video Source: High-Quality : 15 MB

Click below arrow to download….

Why Forms ?

Forms are the foremost feature found in any web application. I refer form to the HTML "Form" Tag. We do know that we can add Text Box, Buttons, List box to the web page using this Form tag.
So to interact with the user, using forms is inevitable. Let's go and see some examples around the web & figure out how important Forms are....

Examples

Yahoo! Mail - Registration Page
This site is world famous & most of us have created an account in Yahoo!
This registration page is built using HTML "Forms" Tag only.
Google - Advanced Search Window
Yet another example of a form...See, if you want to get data from the user, you need a form. Just imagine how it is processed. Let's imagine the steps that follows...
  1. You will fill the form.
  2. Then submit the form.
  3. Then Google server will catch all your inputs.
  4. It will completely 'change' it's way of search.
  5. When you search again, you get different results.
This is indeed a terrific example of a web application. Based on user input in the form, What you see after that completely changes. Ok...Let's stop our crap stories...we had enough...Now let's go to our favorite...programming....

HTML Forms

Let’s start with a sample login form. We will have a username & password text box.
And finally, we will also add one login button at the bottom of the page.
Here is how the code will look like. Name it, “one.jsp”.
<HTML>
<FORM ACTION = "two.jsp" METHOD = "POST">
<BR><B>Username : <INPUT TYPE = TEXT NAME = "username">
<BR><B>Password : <INPUT TYPE = TEXT NAME = "password">
<BR><INPUT TYPE = SUBMIT VALUE = "LOGIN">
</FORM>
</HTML>
HTML Forms & JSP
Now let’s find out what JSP has to do with this. Listen, you designed a page. You got text boxes to type. Think what next. Well, the answer is processing. Once you have your data, you need to process. For example, say once you click on the login button, you have to search the database and validate the login. Whether the user actually has an account & given the right password. Too much theory ? Don’t worry.
HTML can only design the form. It is not having any feature to process data. But JSP can…
JSP uses the request attribute to get the form data. So let’s see how actually JSP does it with an example.
The following file is two.jsp
<HTML>
<h3>Form submitted successfully. </h3>
User name : <%=request.getParameter("username").toString()%>
<BR>Password : <%=request.getParameter("password").toString()%>
</HTML>

response Object

Just like the request, we have another object called response. It’s evident from the name that it contains details regarding a page response. One of the famous example used is sendRedirect. This method is used to navigate from one page to another page. Below example will check if the password entered in page one is ‘jothika’.
If yes, then it goes to success.jsp. Otherwise, it will go to failed.jsp. Try it…See it for yourselves.
<HTML>
<h3>Form submitted successfully. </h3>
User name : <%=request.getParameter("username").toString()%>
<BR>Password : <%=request.getParameter("password").toString()%>
<%
String username = request.getParameter("username").toString();
String password = request.getParameter("password").toString();
if(password.equals("jothika"))
response.sendRedirect("success.jsp");
else
response.sendRedirect("failed.jsp");
%>
</HTML>

8 comments:

  1. Machi... Super....
    Super explanation...
    Waiting for more of your uploads on JSP and servlets....
    Thanks a lot man.... U have given me a start!!!

    ReplyDelete
  2. @VJ
    Thanks. JSP is over. Now recording Servlets.

    ReplyDelete
  3. Nice one..but isnt this a very insecure application.u can get the password if u know the address of the jsp where the if-else check takes place.but j2ee is meant to be secure one right.

    ReplyDelete
  4. @Above: Yeah, you are right. Thanks for your comment. For someone, who just starts learning web development, i thought it's first good to learn the navigation flow.
    Later on, we can take up the security issues, sessions, encryption....

    ReplyDelete
  5. Thanks a Lot . Never Had a friend to teach like this but enjoyed learning your tutorials .Thanks Buddy.

    ReplyDelete
  6. A clear view of the servlet & Jsp .... thanks dude.. nice work... awaiting for more uploads. great work keep doing....

    ReplyDelete
  7. Go jod dude keeping going. I highly appreciate your attempt to tutor in tamil and your really doing a great job.. our support is always with you dude. expecting lot more releases from you. Thumbs up.

    ReplyDelete
  8. Thanks a Lot,nice work... awaiting for more uploads. i am expecting lot more releases from you.

    ReplyDelete

Our Google Group

Our Facebook Group