1、绵阳师范学院信息工程学院JSP 设计技术课程实验报告学 期: 2017-2018 第 1 学期 实验地点: C448姓 名 梁四平 学 号 1508040420 班 级 15.4 班主讲教师 李海洋 指导教师 李海洋 专 业 计算机科学与技术实验名称 实验 5 JSP 之 Servlet 技术 实验日期 11.20 实验组员一、 实验目的1、 掌握 Servlet 的创建和使用方法。二、 实验内容及要求1、 在 MyEclipe10.5 开发环境下建立一个 web 工程,并部署到服务器;2、 练习值 Servlet 的创建和使用。三、 实验条件(可含设备、材料、工具、软件等)1、 MyEcli
2、pe10.52、 Win7 操作系统3、 PC 机四、 实验原理(可含实验思想、原理图、程序框图等)五、 实验过程(可含操作步骤、现象、结果、参考代码等)GoodsSingle.java:package com.yxq.valuebean;public class GoodsSingle private String name;private float price;private int num;public String getName() return name;public void setName(String name) this.name = name;public float
3、getPrice() return price;public void setPrice(float price) this.price = price;public int getNum() return num;public void setNum(int num) this.num = num;MyTools.java:package com.yxq.toolbean;import java.io.UnsupportedEncodingException;public class MyTools public static int strToint(String str)if(str=n
4、ull|str.equals(“)str=“0“;int i=0;tryi=Integer.parseInt(str);catch(NumberFormatException e)i=0;e.printStackTrace();return i;public static String toChinese(String str)if(str=null)str=“;trystr=new String(str.getBytes(“ISO-8859-1“),“gb2312“);catch(UnsupportedEncodingException e)str=“;e.printStackTrace()
5、;return str;ShopCar.java:package com.yxq.toolbean;import java.util.ArrayList;import com.yxq.valuebean.GoodsSingle;public class ShopCar private ArrayList buylist=new ArrayList();public void setBuylist(ArrayList buylist)this.buylist= buylist;public void addItem(GoodsSingle single)if(single!=null)if(bu
6、ylist.size()=0)GoodsSingle temp=new GoodsSingle();temp.setName(single.getName();temp.setPrice(single.getPrice();temp.setNum(single.getNum();buylist.add(temp);elseint i=0;for(;i=buylist.size()GoodsSingle temp=new GoodsSingle();temp.setName(single.getName();temp.setPrice(single.getPrice();temp.setNum(
7、single.getNum();buylist.add(temp);public void removeItem(String name)for(int i=0;i1)temp.setNum(temp.getNum()-1);break;else if(temp.getNum()=1)buylist.remove(i);Index.jsp:IndexServle.java:package com.yxq.servlet;import java.io.IOException;import java.util.ArrayList;import javax.servlet.ServletExcept
8、ion;import javax.servlet.http.*;import com.yxq.valuebean.*;public class IndexServlet extends HttpServlet private static ArrayList goodslist=new ArrayList();protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOExceptiondoPost(request,response);protec
9、ted void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOExceptionHttpSession session=request.getSession();session.setAttribute(“goodslist“, goodslist);response.sendRedirect(“show.jsp“);staticString names=“苹果“ ,“香蕉“ ,“梨“,“橘子“;float prices=2.8f,3.1f,2.5f,2.3f
10、;for(int i=0;i提供商品如下名称 价格 购买 没有商品显示! “购买查看购物车BuyServlet.java:package com.yxq.servlet;import java.io.IOException;import java.util.ArrayList;import javax.servlet.http.*;import javax.servlet.*;import com.yxq.toolbean.*;import com.yxq.valuebean.*;import com.yxq.toolbean.ShopCar;public class BuyServlet e
11、xtends HttpServletprotected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOExceptiondoPost(request,response);protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOExceptionString action=request.getParam
12、eter(“action“);if(action=null) action=“;if(action.equals(“buy“)buy(request,response);if(action.equals(“remove“);remove(request,response);if(action.equals(“clear“);clear(request,response);private void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
13、/ TODO Auto-generated method stubHttpSession session=request.getSession();ArrayList buylist=(ArrayList)session.getAttribute(“buylist“);buylist.clear();response.sendRedirect(“shopcar.jsp“);private void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOExceptio
14、n / TODO Auto-generated method stubHttpSession session=request.getSession();ArrayList buylist=(ArrayList)session.getAttribute(“buylist“);String name=request.getParameter(“name“);ShopCar mycar=new ShopCar();mycar.setBuylist(buylist);mycar.removeItem(name);response.sendRedirect(“showcar.jsp“);private
15、void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException / TODO Auto-generated method stubHttpSession session=request.getSession();String strId=request.getParameter(“id“);int id=MyTools.strToint(strId);ArrayList goodslist=(ArrayList)session.getAttribute(
16、“goodslist“);GoodsSingle single=(GoodsSingle)goodslist.get(id);ArrayList buylist=(ArrayList)session.getAttribute(“buylist“);if(buylist=null)buylist=new ArrayList();ShopCar mycar=new ShopCar();mycar.setBuylist(buylist);mycar.addItem(single);session.setAttribute(“buylist“, buylist);response.sendRedirect(“show.jsp“);Web.xml:indexServletcom.yxq.servlet.IndexServletindexServlet/indexbuyServletcom.yxq.servlet.BuyServletbuyServlet/doCar六、 实验中存在的问题和解决办法七、 意见、建议或感想八、 教师评语(或成绩)教师签字: 年 月 日(说明:本实验报告可以先填写完成后再打印出来,也可以先按实际填写版面需要调整后再打印出来填写)