Servlet商品管理系統(tǒng)_第1頁
Servlet商品管理系統(tǒng)_第2頁
Servlet商品管理系統(tǒng)_第3頁
Servlet商品管理系統(tǒng)_第4頁
Servlet商品管理系統(tǒng)_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、S e r v l e t 商品管理系統(tǒng)設(shè)計目錄五、28六、七、2網(wǎng)上商品信息處理需求分析servlet對數(shù)據(jù)庫信息進行操作,編程實現(xiàn)網(wǎng)上商品處理界面,與數(shù)據(jù)庫相連后,實現(xiàn)通過 包括查詢、添加、刪除、更新修改記錄等,并翻頁顯示結(jié)果總體設(shè)計總體設(shè)計框架如下圖所示:其中為主頁面,分別鏈接到四個子頁面進行不同的操作。為查詢界面,窗體顯示商品ID,商品名稱,商品數(shù)量標識,用戶填入想要進行的查詢操作后,點擊查詢按鈕,觸發(fā) QueryServlet中Post函數(shù);為添加界面,窗體同樣顯示商品ID,商品名稱,商品數(shù)量標識,用戶必須填入要加入商品的ID號,而商品名稱和數(shù)量可以為空值,點擊添加按鈕觸發(fā)AddSe

2、rvlet中的Post函數(shù);為修改界面,窗體同樣顯示商品ID,商品名稱,商品數(shù)量標識,用戶必須填入要修改 的商品ID號、商品名稱及商品數(shù)量,此處不支持填寫空值,點擊修改按鈕觸發(fā)UpdateServlet 中Post函數(shù);為刪除界面,窗體同樣顯示商品ID,商品名稱,商品數(shù)量標識,用戶可以填入其中一項進行查詢,點擊刪除按鈕,觸發(fā) DeleteServlet中Post函數(shù);考慮到缺省情況,上述子頁面遇到無任何輸入的情況下,默認連接數(shù)據(jù)庫,直接查詢所 有表項,并在中分頁顯示結(jié)果。詳細設(shè)計一、查詢模塊設(shè)計查詢功能分為四種情況,首先,若填入ID不為空,則按照ID查詢,調(diào)用QueryById()函數(shù); 若I

3、D為空,判斷name是否為空,若不為空,則按照name查詢,調(diào)用QueryByName()函數(shù); 若name為空,判斷num是否為空,若不為空,則按照num查詢,調(diào)用QueryByNum()函數(shù); 最后,若均為空,則為默認情況,調(diào)用 ShowAll()函數(shù)顯示數(shù)據(jù)庫中所有表項。大體框架如下QueryById()、QueryByName()、QueryByNum()函數(shù)均為了實現(xiàn)查詢功能,大致相同,僅傳 遞的SQL語句不同;為了使結(jié)果能夠分頁顯示,這里將從數(shù)據(jù)庫返回的表項一次填入到自 定義類Product實例中,并加入ArrayList容器,用到函數(shù)MakeList(),并用session傳遞到

4、進 行分頁處理顯示。二、添加模塊設(shè)計添加模塊主要有兩種情況,首先判斷 JSP中輸入的ID是否為空,若為空,因為主鍵為空, 所以添加無效,因此為默認情況,調(diào)用 ShowAll()函數(shù)返回所有表項,分頁顯示;若不為空, 則可執(zhí)行添加工作,為顯示添加成功,此處再次調(diào)用ShowAll()顯示出你所添加的項已經(jīng)存在于表項當(dāng)中。大致框架如下:三、修改模塊設(shè)計修改模塊功能分為兩種情況首先,若填入ID不為空,則按照填入消息對SQL語句進行傳遞, 修改后再次查詢所有表項,顯示出來后證明確實將所需修改的表項進行了修改; 若ID為空, 為默認情況,調(diào)用ShowAll()函數(shù),全部顯示。大致框架如下:四、刪除模塊設(shè)計

5、刪除功能分為四種情況,首先,若填入 ID不為空,則按照ID刪除,調(diào)用DeleteById() 函數(shù);若ID為空,判斷name是否為空,若不為空,則按照name刪除,調(diào)用DeleteByName()函數(shù);若name為空,判斷num是否為空,若不為空,則按照num刪除,調(diào)用DeleteByNum() 函數(shù);最后,若均為空,則為默認情況,調(diào)用ShowAll()函數(shù)顯示數(shù)據(jù)庫中所有表項。大致框架如下:五、Product類設(shè)計該類的主要功能是將表項封裝到一個實例中去,而后將實例一次放入一個list中,而后可以通過檢索list來一次輸出,因此,Product類中主要有與數(shù)據(jù)庫表項相匹配的變量和相應(yīng) 變量的

6、get、set方法。六、JSP設(shè)計上述四個JSP子頁面的主要功能是為了給用戶提供輸入框,根據(jù)條件獲取相應(yīng)數(shù)據(jù)庫表項, 因此JSP大致需一下幾個部分:首先是為了和Servlet相關(guān)聯(lián)的form:<formname="query"method=postaction="servlet/QueryServlet"></form>在form中添加入文字和文本域供用戶使用,例如:<inputtype="text"name="id"size=30>還與提交按鈕,觸發(fā)Servlet行為,例如:&

7、lt;inputtype="submit"value="Query">七、UI界面美化為了使用戶使用時更加美觀、方便,這里使用可視化的Dreamweaver軟件進行界面設(shè)計、 圖片插入,而后將JSP代碼填入具體JSP進行路徑、鏈接配置。八、環(huán)境配置1、Java運行環(huán)境:, Javawebproject;2、數(shù)據(jù)庫平臺:MySql;3、服務(wù)器平臺:TomCat6;關(guān)鍵程序源代碼1、JSP源代碼1)代碼:<body><divalign="center"></div><divalign=&qu

8、ot;center"><tablewidth="606"height="435"border="0"><tr><tdheight="334"colspan="4"><imgsrc="images/"width="627"height="333"/></td></tr><tr><tdwidth="155"heigh

9、t="95"><ahref=""><imgsrc="images/"width="150"height="85"/></a></ td><tdwidth="146"><ahref=""><imgsrc="images/"width="150"height="85"/></a></td>

10、 <tdwidth="134"><ahref=""><imgsrc="images/"width="150"height="85"/></a></td> <tdwidth="153"><ahref=""><imgsrc="images/"width="150"height="85"/></a>

11、;</td></tr></table></div><pclass="STYLE1 ">&nbsp;</p></body>2)代碼:<body><formname="add"method="Post"action="servlet/QueryServlet"><divalign="center"></div><divalign="center&

12、quot;><tablewidth="606"height="671"border="0"><tr><tdheight="342"colspan="2"><imgsrc="images/"width="627"height="333"/></td></tr><tr><tdwidth="164"height="85

13、"><imgsrc="images/"width="160"height="80"/></td><tdwidth="459"><divalign="center"><inputtype="text"name="id"></div></td></tr><tr><tdheight="77"><imgs

14、rc="images/"width="160"height="80"/></td><td><divalign="center"><inputtype="text"name="name"></div></td></tr><tr><tdheight="73"><imgsrc="images/"width="160

15、"height="80"/></td><td><divalign="center"><inputtype="text"name="num"></div></td></tr><tr><tdheight="82"colspan="2"><divalign="center"><inputtype="submit&

16、quot;value=" 查詢"></div></td></tr></table></div><pclass="STYLE1 ">&nbsp;</p></form></body>3)代碼:<body><formname="add"method="Post"action="servlet/QueryServlet"><divalign="

17、center"></div><divalign="center"><tablewidth="606"height="671"border="0"><tr><tdheight="342"colspan="2"><imgsrc="images/"width="627"height="333"/></td></tr&g

18、t;<tr><tdwidth="164"height="85"><imgsrc="images/"width="160"height="80"/></td><tdwidth="459"><divalign="center"><inputtype="text"name="id"></div></td></t

19、r><tr><tdheight="77"><imgsrc="images/"width="160"height="80"/></td><td><divalign="center"><inputtype="text"name="name"></div></td></tr><tr><tdheight="73&

20、quot;><imgsrc="images/"width="160"height="80"/></td><td><divalign="center"><inputtype="text"name="num"></div></td></tr><tr><tdheight="82"colspan="2"><diva

21、lign="center"><inputtype="submit"value=" 查詢"></div></td></tr></table></div><pclass="STYLE1 ">&nbsp;</p></form></body>4)代碼:<body><formname="add"method="Post"action=&

22、quot;servlet/UpdateServlet"><divalign="center"></div><divalign="center"><tablewidth="606"height="671"border="0"><tr><tdheight="342"colspan="2"><imgsrc="images/"width="6

23、27"height="333"/></td></tr><tr><tdwidth="164"height="85"><imgsrc="images/"width="160"height="80"/></td><tdwidth="459"><divalign="center"><inputtype="text&qu

24、ot;name="id"></div></td></tr><tr><tdheight="77"><imgsrc="images/"width="160"height="80"/></td><td><divalign="center"><inputtype="text"name="name"></div>

25、;</td></tr><tr><tdheight="73"><imgsrc="images/"width="160"height="80"/></td><td><divalign="center"><inputtype="text"name="num"></div></td></tr><tr><t

26、dheight="82"colspan="2"><divalign="center"><inputtype="submit"value="</tr></table></div><pclass="STYLE1">&nbsp;</p></form></body>5)代碼: <body><formname="delete"method=&q

27、uot;Post"action="servlet/DeleteServlet"><divalign="center"></div><divalign="center”><tablewidth="606"height="671"border="0"><tr><tdheight="342"colspan="2"><imgsrc="images/&q

28、uot;width="627"height="333"/></td></tr><tr><tdwidth="164"height="85"><imgsrc="images/"width="160"height="80"/></td><tdwidth="459"><divalign="center"><inputt

29、ype="text"name="id"></div></td></tr><tr><tdheight="77"><imgsrc="images/"width="160"height="80"/></td><td><divalign="center"><inputtype="text"name="name&quo

30、t;></div></td></tr><tr><tdheight="73"><imgsrc="images/"width="160"height="80"/></td><td><divalign="center"><inputtype="text"name="num"></div></td></tr&g

31、t;<tr><tdheight="82"colspan="2"><divalign="center"><inputtype="submit"value=" 刪除 "></div></td></tr></table></div><pclass="STYLE1">&nbsp;</p></form></body>6)代碼

32、:<body><%int currentPage=1;int firstCount=1;int endCount=1;Listlist= null ;list=(ArrayList)("list");int everyPage=2;int totalCount=();int totalPage=totalCount/everyPage+1;+totalPage);+totalCount);StringpageNum=("pageNum");if(pageNum= null |("") currentPage=1;el

33、se currentPage=(pageNum);if(currentPage<=0) currentPage=1;elseif (currentPage>totalPage) currentPage=totalPage;if (currentPage>0&&currentPage<=totalPage) firstCount=(currentPage-1)*everyPage+1;+currentPage);+firstCount);if (currentPage>0&&currentPage<=(totalPage-1)

34、endCount=firstCount+everyPage;elseif (currentPage)>0&&(currentPage=totalPage) endCount=firstCount+totalCount%everyPage;+endCount);%><tablewidth="485"border="1"align="center"><tr><tdalign="center"> id</td><tdalign="

35、center"> 商品名稱 </td><tdalign="center"> 商品數(shù)量 </td></tr><%for (int i=firstCount;i<endCount;i+)%><tr><tdalign="center"><%=(i-1).getId()%></td><tdalign="center"><%=(i-1).getName()%></td><td

36、align="center"><%=(i-1).getNum()%></td><%></tr><tr><td><ahref="<%=basePath%>?pageNum=<%=currentPage-1%>"> 上一頁 </a></td><td><ahref="<%=basePath%>?pageNum=<%=currentPage+1%>"> 下一頁

37、 </a></td><td><ahref="<%=basePath%>/">gobacktomainpage</a></td></tr></table></body>2、Servlet源代碼1) AddServlet代碼:(僅列出關(guān)鍵部分)orward(request,response);catch(SQLExceptione) publicvoidShowAll(HttpServletRequestrequest,HttpServletResponser

38、esponse)throwsIOException,Serv letExceptionArrayListl=newArrayList(); try catch(Exceptione) )try(stmt=();Stringsql=newString("select*fromshangpin");rs=(sql);l=(ArrayList)MakeList(rs);HttpSessions=();("list",l);("/").forward(request,response);)catch(SQLExceptione)()publi

39、cList<Product>MakeList(ResultSetrs)throwsSQLException (List<Product>l=newArrayList<Product>();while()(Productp=newProduct(1),(2),(3);(p);)returnl;)publicvoidinit()throwsServletException)2) QueryServlet代碼:publicvoid doPost(HttpServletRequestrequest,HttpServletResponseresponse) throw

40、s ServletException,IOExceptionid=("id");name=("name");a=("num");if(a=null |("")num=0;)else num=(a);if(id!=null &&!("")QueryById(request,response,id);)elseif (name!= null &&!("")QueryByName(request,response,name);)elseif (num

41、!=0)(QueryByNum(request,response,num);)elseShowAll(request,response);)publicvoid QueryById(HttpServletRequestrequest,HttpServletResponseresponse,Stringid )(ArrayListl= new ArrayList();try(conn=)catch (Exceptione)("Openconn:"+();)try(stmt=();Stringsql= new String("select*fromshangpinwh

42、ereid="+""'+id+""');rs=(sql);l=(ArrayList)MakeList(rs);HttpSessions=();("list",l);("/").forward(request,response);)catch (Exceptione)("SQLException");)3) UpdateServlet代碼:publicvoid doPost(HttpServletRequestrequest,HttpServletResponseresp

43、onse) throws ServletException,IOExceptionid=("id");name=("name");a=("num");if(a=null |("")(num=0;)else num=(a);ArrayListl= new ArrayList();if(id!=null &&!("")(try(conn=)catch (Exceptione)("Openconn:"+();)try(stmt=();sql1="updat

44、eshangpinsetname=?,num=?whereid=?"ps=(PreparedStatement)(sql1);(1,name);(2,num);(3,id);();rs=("select*fromshangpin");l=(ArrayList)MakeList(rs);HttpSessions=();("list",l);("/").forward(request,response);)catch (Exceptione)("SQLException");)4) DeleteServlet

45、代碼: publicvoid DeleteById(HttpServletRequestrequest,HttpServletResponseresponse,Stringi d)(ArrayListl= new ArrayList();try(conn=)catch (Exceptione)("Openconn:"+();)try(stmt=();sql="deletefromshangpinwhereid="+""'+id+"”'(sql);rs=("select*fromshangpin");l=(ArrayList)MakeList(rs);HttpSessions=();("list",l);("/").forward(request,response);)catch (Exceptione)("SQLException");)3、Product類代碼publicclass Product public

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論