关闭

用jsp+servlet写一个类似于excel表格的网页表单

标签: servletjspexceldeletexhtmljava
4594人阅读 评论(0) 收藏 举报

再DreamweaverCS5下要看到这样的效果:

 

 

 

需要编写jsp网页如下,这里使用了同一个表单,多个submit的分发的方法,具体可以看里面的js函数:

 

[xhtml] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.     <%@ page import="java.util.*"%>  
  4.     <%@ page import="com.waleking.property.db.*" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12.     <mce:script type="text/javascript"><!--  
  13.         //同一个表单,多个submit的js分发   
  14.         function deleteSubmit(form){  
  15.             form.submitType.value="delete";  
  16.         }  
  17.         //同一个表单,多个submit的js分发   
  18.         function updateSubmit(form){  
  19.             form.submitType.value="update";  
  20.         }  
  21.         //同一个表单,多个submit的js分发   
  22.         function addSubmit(form){  
  23.             form.submitType.value="add";  
  24.         }  
  25.       
  26. // --></mce:script>  
  27.       
  28.       
  29.     <table border="1">  
  30.         <tr>  
  31.             <td>SearcherIP</td>  
  32.             <td>TopNum</td>  
  33.             <td>IndexerPort</td>  
  34.             <td>Operation</td>  
  35.         </tr>  
  36.     <%  
  37.     List<Searcher_PropertyVO> lSearcher=Searcher_PropertyDAO.queryAll();  
  38.       
  39.         for(int i=0;i<lSearcher.size();i++){  
  40.             Searcher_PropertyVO property=lSearcher.get(i);        
  41.               
  42.     %>  
  43.         <tr>  
  44.             <form action="SearcherSheetServlet" method="post">  
  45.             <td>  
  46.                 <%=property.getSearcherIP() %>  
  47.             </td>  
  48.             <td>  
  49.                 <input type="text" name="topNum" value="<%=property.getTopNum() %>"/>  
  50.             </td>  
  51.             <td>  
  52.                 <input type="text" name="indexerPort" value="<%=property.getIndexerPort() %>"/>  
  53.             </td>  
  54.             <td>  
  55.             <table>  
  56.             <tr>  
  57.                 <td>  
  58.                     <input type="hidden" name="submitType"/>  
  59.                     <input type="hidden" name="searcherIP" value="<%=property.getSearcherIP()%>"></input>   
  60.                 </td>  
  61.                 <td><input type="submit" value="delete" onclick="deleteSubmit(form)"></input></td>  
  62.                 <td><input type="submit" value="update" onclick="updateSubmit(form)"></input></td>  
  63.             </tr>  
  64.             </table>  
  65.             </td>  
  66.             </form>  
  67.         </tr>   
  68.     <%  
  69.         }  
  70.     %>  
  71.     <tr>  
  72.         <form action="SearcherSheetServlet" method="post">  
  73.             <td>  
  74.                 <input type="text" name="searcherIP"/>  
  75.             </td>  
  76.             <td>  
  77.                 <input type="text" name="topNum"/>  
  78.             </td>  
  79.             <td>  
  80.                 <input type="text" name="indexerPort"/>  
  81.             </td>  
  82.             <td>  
  83.               
  84.             <table>  
  85.             <tr>  
  86.                 <td>  
  87.                     <input type="hidden" name="submitType"/>  
  88.                 </td>  
  89.                 <td><input type="submit" value="add" onclick="addSubmit(form)"></input></td>  
  90.             </tr>  
  91.             </table>  
  92.               
  93.             </td>  
  94.         </form>  
  95.     </tr>   
  96. </table>  
  97. </body>  
  98. </html>  

 

用servlet来处理请求,因为是用post方法发送的参数,所以在doPost里面修改:

 

[java] view plaincopy
  1. package com.waleking.servlet;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import com.waleking.property.db.Searcher_PropertyDAO;  
  8. import com.waleking.property.db.Searcher_PropertyVO;  
  9. /** 
  10.  * Servlet implementation class SearcherSheet 
  11.  */  
  12. public class SearcherSheetServlet extends HttpServlet {  
  13.     private static final long serialVersionUID = 1L;  
  14.     private Searcher_PropertyDAO searcherDAO = new Searcher_PropertyDAO();  
  15.     private Searcher_PropertyVO searcherProperty = new Searcher_PropertyVO();  
  16.     /** 
  17.      * Default constructor. 
  18.      */  
  19.     public SearcherSheetServlet() {  
  20.         // TODO Auto-generated constructor stub  
  21.     }  
  22.     /** 
  23.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
  24.      *      response) 
  25.      */  
  26.     protected void doGet(HttpServletRequest request,  
  27.             HttpServletResponse response) throws ServletException, IOException {  
  28.         System.out.println("ok");  
  29.         String searcherIP = request.getParameter("searcherIP");  
  30.         String topNum = request.getParameter("topNum");  
  31.         System.out.println(topNum);  
  32.         String indexerPort = request.getParameter("indexerPort");  
  33.         System.out.println(indexerPort);  
  34.         searcherProperty.setSearcherIP(searcherIP);  
  35.         searcherProperty.setTopNum(Integer.parseInt(topNum));  
  36.         searcherProperty.setIndexerPort(Integer.parseInt(indexerPort));  
  37.         System.out.println(searcherProperty);  
  38.         searcherDAO.insert(searcherProperty);  
  39.     }  
  40.     /** 
  41.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
  42.      *      response) 
  43.      */  
  44.     protected void doPost(HttpServletRequest request,  
  45.             HttpServletResponse response) throws ServletException, IOException {  
  46.         // TODO Auto-generated method stub  
  47.           
  48.         //String s=request.getParameter("ip");  
  49.         String submitType=request.getParameter("submitType");  
  50.         System.out.println(submitType);  
  51.         if(submitType.equals("add")){//添加  
  52.             String searcherIP = request.getParameter("searcherIP");  
  53.             String topNum = request.getParameter("topNum");  
  54.             String indexerPort = request.getParameter("indexerPort");  
  55.             searcherProperty.setSearcherIP(searcherIP);  
  56.             searcherProperty.setTopNum(Integer.parseInt(topNum));  
  57.             searcherProperty.setIndexerPort(Integer.parseInt(indexerPort));  
  58.             System.out.println(searcherProperty);  
  59.             searcherDAO.insert(searcherProperty);  
  60.               
  61.             response.sendRedirect("index.jsp");  
  62.         }else if(submitType.equals("update")){//修改  
  63.             String searcherIP = request.getParameter("searcherIP");  
  64.             String topNum = request.getParameter("topNum");  
  65.             String indexerPort = request.getParameter("indexerPort");  
  66.             searcherProperty.setSearcherIP(searcherIP);  
  67.             searcherProperty.setTopNum(Integer.parseInt(topNum));  
  68.             searcherProperty.setIndexerPort(Integer.parseInt(indexerPort));  
  69.             searcherDAO.update(searcherProperty);  
  70.               
  71.             response.sendRedirect("index.jsp");  
  72.         }else if(submitType.equals("delete")){//删除  
  73.             String searcherIP = request.getParameter("searcherIP");  
  74.             searcherDAO.deleteByKey(searcherIP);  
  75.               
  76.             response.sendRedirect("index.jsp");  
  77.         }  
  78.     }  
  79. }  

 

关于web.xml的编写可以参看上一篇servlet的编写。

 

可以看到最终的效果:

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6001344次
    • 积分:58401
    • 等级:
    • 排名:第56名
    • 原创:330篇
    • 转载:1711篇
    • 译文:19篇
    • 评论:173条
    博客专栏
    网上乐园
    最新评论