Here is a program that dynamically prints a table, means
this program allows user to enter the number of rows and columns. I have
designed a jsp page (table_initial.jsp) that allows you to input rows
and columns through form and redirect he rows and columns to another jsp file (print_table.jsp).
If you don’t have much knowledge of jsp pages then don’t worry, as jsp pages
are just the combinations of html code and java code. One more interesting
thing in this is that inside each cell of my table I have printed the row and
column number, this in-turn needs two for loops for it’s implementation.
Source code # 1
/* table_initial.jsp */
/*********************************************************************/
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Input</title>
</head>
<body>
<form method=post action="http://localhost:8080/FirstWebSite/print_table.jsp">
<br/>enter number of rows<input type=text name="rows">
<br/>enter number of
columns<input type=text name="cols">
<br/><input type=submit value="OK">
</form>
</body>
</html>
/******************************************************************************/
/* print_table.jsp */
/******************************************************************************/
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Your table is here</title>
</head>
<body>
<table border=3 align=”center” bgcolor=”orange”>
<%
String
rows=request.getParameter("rows");
String
cols=request.getParameter("cols");
if(rows == null || cols == null)
{
response.sendRedirect("table_initial.jsp");
return;
}
for (int i=0;i<Integer.parseInt(rows);i++){
%>
<tr>
<%
for (int j=0;j<Integer.parseInt(cols); j++){
%>
<td> %=(i+1) + "," + (j+1)%> </td>
<%
}
%>
<%
}
%>
</tr>
</table>
</body>
</html>
/******************************************************************************/
Output :-
/**********************************************************************/
/****************************************************************/
Note that the first page can also be designed in jsp as it contains
no java code.
No comments:
Post a Comment