1. 程式人生 > >用jsp寫一個動態表格

用jsp寫一個動態表格

兩個jsp頁面,一個jsp頁面根據使用者輸入的行和列,在另一個jsp頁面顯示該表格

第一個jsp的頁面的程式碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> <head> <base href="<%=basePath%>"> <title>My JSP 'input.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"
>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="output.jsp"
>
row:<input type="text" name="row"><br> col:<input type="text" name="col"><br> <input type="submit" value="submit"> </form> </body> </html>

第二個接收並顯示錶格的jsp程式碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'output.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
      <%int row =Integer.parseInt(request.getParameter("row"));
        int col=Integer.parseInt(request.getParameter("col"));     
       %>
       <table border="1" width="80%">
       <%for(int i=0;i<row;i++)
       { 
       %>
       <tr>

           <%for(int j=0;j<col;j++)
           { %>

           <td>
           </td>

           <%
           } %>







       </tr>
       <%
       }
        %>


       </table>    

  </body>
</html>