J2EE聊天室和购物车实验报告.docx
- 文档编号:3819210
- 上传时间:2022-11-25
- 格式:DOCX
- 页数:18
- 大小:288.96KB
J2EE聊天室和购物车实验报告.docx
《J2EE聊天室和购物车实验报告.docx》由会员分享,可在线阅读,更多相关《J2EE聊天室和购物车实验报告.docx(18页珍藏版)》请在冰豆网上搜索。
J2EE聊天室和购物车实验报告
实验报告
|
|
实验名称实现聊天室和购物车
课程名称JavaEE编程技术
|
|
一、实验目的及要求
学生在了解Java语言的基础上了解并掌握J2EE的构架及相关技术,重点掌握核心技术中最基本的Servlet、JSP、Javabean、JDBC,初步具有基于J2EE平台的软件开发能力、设计并实现小型应用系统
1.设计并实现简单的聊天室:
1)要求使用JSP或者servlet,使用session控制
2)至少3个页面
3)使用application对象或者servletcontext保存聊天信息
4)能进行用户登录验证
5)能显示在线用户名及数量
6)供多个用户同时聊天
7)选作:
数据库存用户名、注册、登录
2.实现购物车
1)使用servlet、JSP实现购物车,要求使用java的集合对象、session控制,购物车在结算前能进行购买数量的反复修改。
2)使用javaBean对购物车进行重新整合实现,实现表示层和业务逻辑层的分离。
3)选作:
使用数据库保存货物和购物订单、用户登录、注册
二、所用仪器、设备
PC机,基于windowxp或者windowsserver平台,使用JDK+Netbeans+MySQL为开发工具
三、实验原理
1.设计结构框架如下图
2.聊天室由index.jsp,logincheck.jsp,room.jsp,say.jsp,out.jsp构成。
购物车由index.jsp,shopping.jsp,sure.jsp构成。
控制层是用来实现各个表示层间的跳转以及对数据的操作。
数据层由session和application构成。
3.javabean实质是一个java类;使用javabean可以实现代码的重复利用,减少代码的使用量。
四、实验结果
1.聊天室
登陆聊天室
张三和李四同时在线
李四退出
2、购物车
五、结论
通过实验,我进一步理解和掌握了JAVAEE的编程思想.实验中,由于第一次接触网络编程,写代码很不规范,经老师的指点,受益良多.
对于本次实验的一些改进思想:
聊天器:
聊天器我已经实现了密码的认证,但是用户信息是存储与application里面的,不能长期保存,应该放入数据库.
功能方面应该增加密码保护和找回密码的功能,如果可能,最好再加入加载好友和私聊功能.
购物车:
购物车我用session实现了可以返回修改,但是商品是静态的,应该加入数据库,实现商品的动态添加和删除.另外,购物车最好可以实现登录功能.
六、附录代码
聊天室的实现:
Index.html
姓名
密码
Logcheck.jsp
<%
request.setCharacterEncoding("gb2312");
StringUsername=(String)request.getParameter("Username");
ArrayListUsers=(ArrayList)application.getAttribute("Users");
Stringtem="";
if(Users==null)
{
Users=newArrayList();
Users.add(Username);
application.setAttribute("Users",Users);
session.setAttribute("Username",Username);
tem=Username+",欢迎登入!
";
session.setAttribute("tem",tem);
}
else
{
booleanhasLog=false;
for(inti=0;i { Stringtemp=(String)Users.get(i); if(temp.equals(Username)) { session.setAttribute("Username",Username); tem=Username+",您已登入! 欢迎回来"; session.setAttribute("tem",tem); hasLog=true; } session.setAttribute("Username",Username); } if(! hasLog) { Users.add(Username); application.setAttribute("Users",Users); session.setAttribute("Username",Username); tem=Username+",欢迎登入! "; session.setAttribute("tem",tem); } } response.sendRedirect("room1.jsp");%> Room1.jsp
<%response.setHeader("refresh","10");%>
<%=session.getAttribute("tem")%>
已有的聊天信息:
<%
ArrayListapp=(ArrayList)application.getAttribute("app");
if(app!
=null){
%>
<%
Iteratorite=app.iterator();
while(ite.hasNext()){
Stringinfo=(String)ite.next();
%>
<%=info%>
<%
}
}
%>
输入聊天信息:
<%
ArrayListUsers=(ArrayList)application.getAttribute("Users");
out.println("在线人数:
"+Users.size());
%>
在线人员列表:
<%for(inti=0;i {%> <%=Users.get(i)%> <%}%> Say.jsp
<%
response.setContentType("text/html;charset=GB2312");
request.setCharacterEncoding("gb2312");
Stringinfo=session.getAttribute("Username")+":
"+(String)request.getParameter("talk");
ArrayListappList=null;
appList=(ArrayList)application.getAttribute("app");
if(appList!
=null){
appList.add(info);
}
else{
appList=newArrayList();
appList.add(info);
}
application.setAttribute("app",appList);
response.setCharacterEncoding("gb2312");
response.sendRedirect("room1.jsp");
%>
Out.jsp
<%
ArrayListUsers=(ArrayList)application.getAttribute("Users");
Stringexitname=(String)session.getAttribute("Username");
Stringtemp=newString();
for(inti=0;i { temp=(String)Users.get(i); if(temp.equals(exitname)) { Users.remove(i); break; } } %> 购物车实现: Index.jsp//复选框和购买量我实现了动态显示 <%Stringac=(String)session.getAttribute("applewaschecked");%> <% if(ac==null) ac="0"; %> <%Stringa=(String)session.getAttribute("apple");%> <% if(a==null) a="0"; %> <%Stringb=(String)session.getAttribute("banana");%> <% if(b==null) b="0"; %> <%Stringbc=(String)session.getAttribute("bananawaschecked");%> <% if(bc==null) bc="0"; %> <%Stringg=(String)session.getAttribute("grape");%> <% if(g==null) g="0"; %> <%Stringgc=(String)session.getAttribute("grapewaschecked");%> <% if(gc==null) gc="0"; %>
Shopping.jsp
<%
request.setCharacterEncoding("gb2312");
session.setAttribute("apple",request.getParameter("apple_count").toString());
session.setAttribute("banana",request.getParameter("banana_count").toString());
session.setAttribute("grape",request.getParameter("grape_count").toString());
session.setAttribute("applewaschecked","0");
session.setAttribute("bananawaschecked","0");
session.setAttribute("grapewaschecked","0");
String[]cheke={"applewaschecked","bananawaschecked","grapewaschecked"};
ArrayListcount=newArrayList();
Stringa=newString();
Stringb=newString();
Stringc=newString();
a=request.getParameter("apple_count").toString();
count.add(a);
b=request.getParameter("banana_count").toString();
count.add(b);
c=request.getParameter("grape_count").toString();
count.add(c);
ArrayListfuitcost=newArrayList();
fuitcost.add(3);
fuitcost.add(4);
fuitcost.add(5);
shop.setCount(count);
shop.setfuitcost(fuitcost);
shop.setAllcost();
%>
总物品:
苹果香蕉葡萄
价格:
<%
for(inti=0;i { %> <%=fuitcost.get(i).toString()%> <%;}%> 已买物品: <% //ArrayListcatfuit=newArrayList(); String[]catfuit=request.getParameterValues("checkbox1"); for(inti=0;i { %> <%=catfuit[i]%> <%;}%> 数量: <% for(inti=0;i {if(count.get(i).toString().equals("0")) continue; session.setAttribute(cheke[i],"checked"); %> <%=count.get(i).toString()%> <%;}%> 费用: <%=shop.getAllcost()%> Return.jsp <% request.setCharacterEncoding("gb2312"); %> forwardpage="index.jsp"> forward> Sure.jsp 欢迎再次光临! Javabeans: publicclasscatForbuy{ privateintallcost; privateArrayListfuitcost; privateArrayListcount; publicintgetAllcost(){ returnallcost; } publicvoidsetAllcost(){ this.allcost=0; for(inti=0;i this.allcost=this.allcost+Integer.valueOf(this.fuitcost.get(i).toString())*Integer.valueOf(this.count.get(i).toString()); } publicArrayListgetfuitcost(){ returnfuitcost; } publicvoidsetfuitcost(ArrayListfuitcost){ this.fuitcost=fuitcost; } publicArrayListgetCount(){ returncount; } publicvoidsetCount(ArrayListcount){ this.count=count; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- J2EE 聊天室 购物 实验 报告