再DreamweaverCS5下要看到这样的效果:
需要编写jsp网页如下,这里使用了同一个表单,多个submit的分发的方法,具体可以看里面的js函数:
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@pageimport="java.util.*"%>
- <%@pageimport="com.waleking.property.db.*"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
- <title>Inserttitlehere</title>
- </head>
- <body>
- <mce:scripttype="text/javascript"><!--
- //同一个表单,多个submit的js分发
- functiondeleteSubmit(form){
- form.submitType.value="delete";
- }
- //同一个表单,多个submit的js分发
- functionupdateSubmit(form){
- form.submitType.value="update";
- }
- //同一个表单,多个submit的js分发
- functionaddSubmit(form){
- form.submitType.value="add";
- }
- //--></mce:script>
- <tableborder="1">
- <tr>
- <td>SearcherIP</td>
- <td>TopNum</td>
- <td>IndexerPort</td>
- <td>Operation</td>
- </tr>
- <%
- List<Searcher_PropertyVO>lSearcher=Searcher_PropertyDAO.queryAll();
- for(inti=0;i<lSearcher.size();i++){
- Searcher_PropertyVOproperty=lSearcher.get(i);
- %>
- <tr>
- <formaction="SearcherSheetServlet"method="post">
- <td>
- <%=property.getSearcherIP()%>
- </td>
- <td>
- <inputtype="text"name="topNum"value="<%=property.getTopNum()%>"/>
- </td>
- <td>
- <inputtype="text"name="indexerPort"value="<%=property.getIndexerPort()%>"/>
- </td>
- <td>
- <table>
- <tr>
- <td>
- <inputtype="hidden"name="submitType"/>
- <inputtype="hidden"name="searcherIP"value="<%=property.getSearcherIP()%>"></input>
- </td>
- <td><inputtype="submit"value="delete"onclick="deleteSubmit(form)"></input></td>
- <td><inputtype="submit"value="update"onclick="updateSubmit(form)"></input></td>
- </tr>
- </table>
- </td>
- </form>
- </tr>
- <%
- }
- %>
- <tr>
- <formaction="SearcherSheetServlet"method="post">
- <td>
- <inputtype="text"name="searcherIP"/>
- </td>
- <td>
- <inputtype="text"name="topNum"/>
- </td>
- <td>
- <inputtype="text"name="indexerPort"/>
- </td>
- <td>
- <table>
- <tr>
- <td>
- <inputtype="hidden"name="submitType"/>
- </td>
- <td><inputtype="submit"value="add"onclick="addSubmit(form)"></input></td>
- </tr>
- </table>
- </td>
- </form>
- </tr>
- </table>
- </body>
- </html>
用servlet来处理请求,因为是用post方法发送的参数,所以在doPost里面修改:
- packagecom.waleking.servlet;
- importjava.io.IOException;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importcom.waleking.property.db.Searcher_PropertyDAO;
- importcom.waleking.property.db.Searcher_PropertyVO;
- /**
- *ServletimplementationclassSearcherSheet
- */
- publicclassSearcherSheetServletextendsHttpServlet{
- privatestaticfinallongserialVersionUID=1L;
- privateSearcher_PropertyDAOsearcherDAO=newSearcher_PropertyDAO();
- privateSearcher_PropertyVOsearcherProperty=newSearcher_PropertyVO();
- /**
- *Defaultconstructor.
- */
- publicSearcherSheetServlet(){
- //TODOAuto-generatedconstructorstub
- }
- /**
- *@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponse
- *response)
- */
- protectedvoiddoGet(HttpServletRequestrequest,
- HttpServletResponseresponse)throwsServletException,IOException{
- System.out.println("ok");
- StringsearcherIP=request.getParameter("searcherIP");
- StringtopNum=request.getParameter("topNum");
- System.out.println(topNum);
- StringindexerPort=request.getParameter("indexerPort");
- System.out.println(indexerPort);
- searcherProperty.setSearcherIP(searcherIP);
- searcherProperty.setTopNum(Integer.parseInt(topNum));
- searcherProperty.setIndexerPort(Integer.parseInt(indexerPort));
- System.out.println(searcherProperty);
- searcherDAO.insert(searcherProperty);
- }
- /**
- *@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponse
- *response)
- */
- protectedvoiddoPost(HttpServletRequestrequest,
- HttpServletResponseresponse)throwsServletException,IOException{
- //TODOAuto-generatedmethodstub
- //Strings=request.getParameter("ip");
- StringsubmitType=request.getParameter("submitType");
- System.out.println(submitType);
- if(submitType.equals("add")){//添加
- StringsearcherIP=request.getParameter("searcherIP");
- StringtopNum=request.getParameter("topNum");
- StringindexerPort=request.getParameter("indexerPort");
- searcherProperty.setSearcherIP(searcherIP);
- searcherProperty.setTopNum(Integer.parseInt(topNum));
- searcherProperty.setIndexerPort(Integer.parseInt(indexerPort));
- System.out.println(searcherProperty);
- searcherDAO.insert(searcherProperty);
- response.sendRedirect("index.jsp");
- }elseif(submitType.equals("update")){//修改
- StringsearcherIP=request.getParameter("searcherIP");
- StringtopNum=request.getParameter("topNum");
- StringindexerPort=request.getParameter("indexerPort");
- searcherProperty.setSearcherIP(searcherIP);
- searcherProperty.setTopNum(Integer.parseInt(topNum));
- searcherProperty.setIndexerPort(Integer.parseInt(indexerPort));
- searcherDAO.update(searcherProperty);
- response.sendRedirect("index.jsp");
- }elseif(submitType.equals("delete")){//删除
- StringsearcherIP=request.getParameter("searcherIP");
- searcherDAO.deleteByKey(searcherIP);
- response.sendRedirect("index.jsp");
- }
- }
- }
关于web.xml的编写可以参看上一篇servlet的编写。
可以看到最终的效果: