Saturday, September 5, 2009

Chapter 08: More JSP Examples

With this chapter, we will wind up jsp and start learning Servlets. Of course there are too many topics we missed out in jsp but the objective of my blog is to get you started. Once you get the start, you will learn everything on your own. From my experience, Self-Learning is the best learning.
I will discuss two more examples on jsp in this chapter.

Example 1: The Dynamic Table

We will build a multiplication table which we've come across during our early school days.
First, i will build it with HTML Alone. Then we will do it the JSP way.

table.html

The following table is a static one. It’s just a multiplication table of 5.

<html>
<h3>5 Table</h3>

<TABLE border = 1>

<TR><TH>A</TH><TH>B</TH></TR>

<tr><td>1 * 5</td><td>5</td></tr>
<tr><td>2 * 5</td><td>10</td></tr>
<tr><td>3 * 5</td><td>15</td></tr>
<tr><td>4 * 5</td><td>20</td></tr>
<tr><td>5 * 5</td><td>25</td></tr>

<TABLE>

</html>

table.jsp

Here we go…Our cute dynamic one. Notice the number of Multiply operation performed in both the examples. It’s 5 lines in the HTML version. But here, it’s just ONLY ONE line. The number you define in the for loop, decides the number of rows. Check out the video @ the end of this blog to see this in action.

<html>

<h3>5 Tables</h3>

<TABLE border = 1>
<TR><TH>A</TH><TH>B</TH></TR>

<%
int number = 5;
for(int i = 1; i <= number ; i++)
{
%>
<tr><td>1 * <%=number%></td><td><%=number*i%></td></tr>
<%
}
%>

<TABLE>

</html>


Example 2: Website Helper


where1.jsp

This is a typical “Re-direct” example. If you just say the name of the site, it takes you right there. This where1.jsp has the HTML form where you give the input.

<HTML>
<h3>Where do you want to go ?</h3>
Enter the site name alone. Dont give www, http.
<BR>Eg., Yahoo.

<FORM ACTION = "where2.jsp" METHOD = "POST">
<BR><B>Website : <INPUT TYPE = TEXT NAME = "website">
<BR><INPUT TYPE = SUBMIT VALUE = "Go !">
</FORM>
</HTML>

where2.jsp

Once you define a site, the following piece of code will attach http, www, com to the name and performs a re-direct.

<HTML>
<%
String siteName = request.getParameter("website");
String siteURL = "http://www."+ siteName + ".com";
response.sendRedirect(siteURL);
%>
</HTML>

What Next?

Well, there is a lot, lot & lot to learn. It will never end and you should never stop learning. What i have taught you in 8 chapters is just a start. You need to keep surfing more & explore JSP. We will now move to servlets.

Download Video Source: High-Quality : 23 MB

Click below arrow to download….

Video: Part 1



Video: Part 2

No comments:

Post a Comment

Our Google Group

Our Facebook Group