实验17JSP与Servlet综合案例.docx
- 文档编号:12638347
- 上传时间:2023-04-21
- 格式:DOCX
- 页数:28
- 大小:285.14KB
实验17JSP与Servlet综合案例.docx
《实验17JSP与Servlet综合案例.docx》由会员分享,可在线阅读,更多相关《实验17JSP与Servlet综合案例.docx(28页珍藏版)》请在冰豆网上搜索。
实验17JSP与Servlet综合案例
实验17JSP与Servlet综合案例
17.1实验目的
1.了解项目开发的基本流程和开发步骤知识
2.掌握项目分析和软件设计的主要步骤
3.回顾掌握JDBC编程的技术和方法
4.掌握JSP·Servlet综合知识和编程方法
17.2实验环境
1.MyEclipse插件平台、
2.Weblogic(或者Tomcat)容器
3.MySQl(或者SQLServer、Oracle)数据库
17.3实验知识背景
17.3.1项目背景
随着网络技术的飞速发展,网络电子商务业务迅速扩大。
商家可以将商品的展示、销售和结算在网站中进行,客户可以将喜爱的商品和网站资料保存下来,把珍藏的商品列为精品。
并对其对应的网站链接地址保存到本地数据库中,以供查询、搜索和编辑使用。
基于这个需求背景,结合Web编程技术,采用MVC软件设计模式设计本项目案例。
知名精品网站URL如表17-1所示。
表17-1精品网站
精品网店
收藏URL
凡客诚品
京东
麦考林
玛莎玛索
珂兰
一号店
好乐买
创业邦
优购
试客联盟
易物网
http:
//www.ewu.cc/
9钻网
慧聪网
银座网
金蚂蚁
17.3.2编程思想
1.分层开发思想
软件的层次结构可以分为四层:
表现层
控制层
业务逻辑层
数据逻辑层(持久层)
2.面向接口编程思想
在编程中将业务逻辑抽象出接口,以供上次调用
依赖抽象(接口),而非具体(接口实现)的编程思想,又称之为控制反转(InversionofControl)
17.3.3设计模式
1.DAO设计模式
DAO的全称是:
DataAccessObject,数据访问对象。
使用DAO设计模式,来封装数据库持久层的所以操作(CRUD),使低级的数据逻辑和高级的业务逻辑分离,达到解耦合的目的。
一个典型的DAO实现有如下的组件:
一个DAO接口
一个实现了DAO接口的具体类
一个DAO工厂类
数据传输对象(有时称为值对象)
以维护一个客户信息为例,具体组件如下所示:
CustomerDao接口
Customer值对象(VO)
CustomerDaoImpl(接口的具体实现类)
CustomerFactory(工厂类,实例化用)
编程思想和设计模式的具体应用参照精品收藏项目案例。
2.MVC设计模式
项目MVC设计模式,参见表17-2所示
表17-2MVC设计模式
MVC模型、视图、控制器关系如图17-1所示。
图17-1MVC模式关系图
17.4实验内容与步骤
17.4.1收藏管理
案例描述
本实例运用了分层开发思想、面向接口编程两种思想;和DAO、MVC设计模式来实现一个收藏管理程序,程序的主要功能是,把感兴趣的链接做维护,包括添加一个新的链接、删除一个链接、显示一个链接列表、修改链接等内容。
案例中使用到的表
收藏表(LinkTbl)如表17-2所示。
表17-2LinkTbl
列名
数据类型
长度
允许为空
是否主键
说明
id
integer
11
√
编号
url
varchar
50
√
链接URL
name
varchar
50
√
链接名
【例17-1】综合程序案例——收藏管理。
程序运行结果示意如图17-2、图17-3所示
图17-2收藏管理结果示意图
图17-3修改收藏示意图
17.4.2项目结构图
项目结构图如图17-4所示
图17-4项目结构图
17.4.3数据库连接
//LinkDao.java
packagecom.sise.dao;
importjava.util.ArrayList;
importjava.util.List;
importcom.sise.vo.Link;
publicinterfaceLinkDao{
publicvoidadd(Linkl);
publicvoidupdate(Linkl);
publicListlist();
publicvoiddelete(String[]ids);
publicLinkget(intid);
}
//LinkDaoImpl.java
packagecom.sise.dao.impl;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;
importjava.util.List;
importcom.sise.dao.LinkDao;
importcom.sise.util.ConnectionUtil;
importcom.sise.util.SQLConstants;
importcom.sise.vo.Link;
publicclassLinkDaoImplimplementsLinkDao,SQLConstants{
publicvoidadd(Linkl){
Connectionconn=newConnectionUtil().openConnection();
try{
PreparedStatementpstmt=conn.prepareStatement(ADD_LINK_SQL);
pstmt.setString(1,l.getName());
pstmt.setString(2,l.getUrl());
pstmt.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
publicvoiddelete(String[]ids){
Connectionconn=newConnectionUtil().openConnection();
try{
PreparedStatementpstmt=conn.prepareStatement(DELETE_LINK_SQL);
if(ids!
=null&&ids.length>0)
for(inti=0;i pstmt.setInt(1,Integer.parseInt(ids[i])); pstmt.executeUpdate(); } }catch(SQLExceptione){ e.printStackTrace(); }finally{ try{ conn.close(); }catch(SQLExceptione){ e.printStackTrace(); } } } publicLinkget(intid){ Connectionconn=newConnectionUtil().openConnection(); try{ PreparedStatementpstmt=conn.prepareStatement(GET_LINK_SQL); pstmt.setInt(1,id); ResultSetrs=pstmt.executeQuery(); if(rs.next()){ //intid=rs.getInt (1); Stringname=rs.getString (2); Stringurl=rs.getString(3); Linkl=newLink(); l.setId(id); l.setName(name); l.setUrl(url); returnl; } }catch(SQLExceptione){ e.printStackTrace(); }finally{ try{ conn.close(); }catch(SQLExceptione){ e.printStackTrace(); } } returnnull; } publicListlist(){ Connectionconn=newConnectionUtil().openConnection(); try{ Statementstmt=conn.createStatement(); ResultSetrs=stmt.executeQuery(QUERY_LINK_SQL); Listlist=newArrayList(); while(rs.next()){ intid=rs.getInt (1); Stringname=rs.getString (2); Stringurl=rs.getString(3); Linkl=newLink(); l.setId(id); l.setName(name); l.setUrl(url); list.add(l); } returnlist; }catch(SQLExceptione){ e.printStackTrace(); }finally{ try{ conn.close(); }catch(SQLExceptione){ e.printStackTrace(); } } returnnull; } publicvoidupdate(Linkl){ Connectionconn=newConnectionUtil().openConnection(); try{ PreparedStatementpstmt=conn.prepareStatement(UPDATE_LINK_SQL); pstmt.setString(1,l.getName()); pstmt.setString(2,l.getUrl()); pstmt.setInt(3,l.getId()); pstmt.executeUpdate(); }catch(SQLExceptione){ e.printStackTrace(); }finally{ try{ conn.close(); }catch(SQLExceptione){ e.printStackTrace(); } } } } //LinkServlet.java packagecom.sise.servlet; importjava.io.IOException; importjava.util.List; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importcom.sise.dao.LinkDao; importcom.sise.dao.impl.LinkDaoImpl; importcom.sise.vo.Link; publicclassLinkServletextendsHttpServlet{ publicLinkServlet(){ super(); } publicvoiddestroy(){ super.destroy();//Justputs"destroy"stringinlog //Putyourcodehere } publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ doPost(request,response); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ StringmethodName=request.getParameter("methodName"); if(methodName! =null&&methodName.equals("add")){ add(request,response); }elseif(methodName! =null&&methodName.equals("query")){ query(request,response); }elseif(methodName! =null&&methodName.equals("delete")){ delete(request,response); }elseif(methodName! =null&&methodName.equals("forward")){ forward(request,response); }elseif(methodName! =null&&methodName.equals("update")){ update(request,response); }else{ return; } /* //响应用户请求 Stringname=request.getParameter("name"); Stringurl=request.getParameter("url"); //调用后台逻辑 LinkDaodao=newLinkDaoImpl(); Linkl=newLink(); l.setName(name); l.setUrl(url); dao.add(l); Listlist=dao.list(); request.setAttribute("LinkList",list); //转发 request.getRequestDispatcher("/link.jsp").forward(request,response); */ } publicvoidadd(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ //响应用户请求 Stringname=request.getParameter("name"); Stringurl=request.getParameter("url"); //调用后台逻辑 LinkDaodao=newLinkDaoImpl(); Linkl=newLink(); l.setName(name); l.setUrl(url); dao.add(l); query(request,response); } publicvoidquery(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ //调用后台逻辑 LinkDaodao=newLinkDaoImpl(); Listlist=dao.list(); request.setAttribute("LinkList",list); //转发 request.getRequestDispatcher("/link.jsp").forward(request,response); } publicvoiddelete(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ //调用后台逻辑 LinkDaodao=newLinkDaoImpl(); String[]ids=request.getParameterValues("ids"); dao.delete(ids); query(request,response); } publicvoidforward(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ //调用后台逻辑 Stringid=request.getParameter("id"); LinkDaodao=newLinkDaoImpl(); Linklink=dao.get(Integer.parseInt(id)); request.setAttribute("link",link); request.getRequestDispatcher("/editLink.jsp") .forward(request,response); } publicvoidupdate(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ //响应用户请求 Stringid=request.getParameter("id"); Stringname=request.getParameter("name"); Stringurl=request.getParameter("url"); //调用后台逻辑 LinkDaodao=newLinkDaoImpl(); Linkl=newLink(); l.setId(Integer.parseInt(id)); l.setName(name); l.setUrl(url); dao.update(l); query(request,response); } publicvoidinit()throwsServletException{ //Putyourcodehere } } //ConnectionUtil.java packagecom.sise.util; importjava.sql.Connection; importjava.sql.DriverManager; importjava.util.Properties; publicclassConnectionUtil{ publicstaticvoidmain(String[]args){ ConnectionUtilcu=newConnectionUtil(); System.out.println(cu.openConnection()); } publicConnectionopenConnection(){ Stringurl=""; Stringdriver=""; Stringuser=""; Stringpassword=""; Propertiesprop=newProperties(); try{ prop.load(this.getClass().getClassLoader().getResourceAsStream( "DBConfig.properties")); driver=prop.getProperty("driver"); url=prop.getProperty("url"); user=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验17 JSP与Servlet综合案例 实验 17 JSP Servlet 综合 案例