书签 分享 收藏 举报 版权申诉 / 14

类型javaweb实验9.docx

  • 文档编号:6263434
  • 上传时间:2023-05-09
  • 格式:DOCX
  • 页数:14
  • 大小:19.97KB

S1-7:

在src目录下新建web.LogoutServlet,映射为/logoutServlet,实现用户的退出登录处理。

在其doGet方法中添加代码如下:

request.getSession().removeAttribute("user");

response.sendRedirect(getServletContext().getContextPath()+"/index.jsp");

R2:

利用Cookie技术实现用户的自动登录。

S2-1:

修改LoginServlet,实现用户登录后,利用Cookie技术在客户端保存用户的登录信息,以便下次访问web站点时,可以直接从客户端Cookie中读取登录信息,直接登录。

修改代码如下:

if(user!

=null&&user.getPass().equals(pass)){

/*利用Cookie技术保存用户登录信息

*①创建Cookie对象

*②设置cookie对象的有效期限,单位为妙

*③添加cookie对象到response对象中,由response对象发送其至客户端

*/

Cookiecookie=newCookie("username",username);

cookie.setMaxAge(5*60);//5秒

Cookiecookie1=newCookie("pass",pass);

cookie1.setMaxAge(5*60);

response.addCookie(cookie);

response.addCookie(cookie1);

session.setAttribute("user",user);

response.sendRedirect(this.getServletContext().getContextPath()+"/index.jsp");

}else{

session.setAttribute("username",username);

response.sendRedirect(this.getServletContext().getContextPath()+"/login.jsp");

}

S2-2:

修改index.jsp,读取客户端携带的cookie,找到名为username的cookie信息,显示用户欢迎信息。

修改代码如下:

80%">

<%--先读取客户端携带的cookie,获取用户登录的信息--%>

<%

Useruser=null;

Stringusername=null;

Stringpass=null;

Cookiecookies[]=request.getCookies();

if(cookies!

=null){

for(Cookiecookie:

cookies){

if("username".equals(cookie.getName())){

username=cookie.getValue();

}

if("pass".equals(cookie.getName())){

pass=cookie.getValue();

}

if(username!

=null&&pass!

=null){

user=newUser(username,pass);

break;

}

}

}

%>

……

R3:

在Servlet中利用Session机制实现模拟购物车。

S3-1:

在src目录下创建ShoppingCartItem类domain.ShoppingCartItem,代码如下:

publicclassShoppingCartItem{

privateBookbook;

privateintcount;

privatedoublesubPrice;

publicdoublegetSubPrice(){returnbook.getPrice()*count;}

publicvoidsetSubPrice(doublesubPrice){this.subPrice=subPrice;}

publicBookgetBook(){returnbook;}

publicvoidsetBook(Bookbook){this.book=book;}

publicintgetCount(){returncount;}

publicvoidsetCount(intcount){this.count=count;}

}

S3-2:

在src目录下创建ShoppingCart类domain.ShoppingCart,代码如下:

publicclassShoppingCart{

privateMapitems=newHashMap();

privatedoubletotalPrice;

publicMapgetItems(){returnitems;}

publicvoidsetItems(Mapitems){this.items=items;}

publicdoublegetTotalPrice(){

totalPrice=0;

Set>set=items.entrySet();

for(Entryentry:

set){

totalPrice+=entry.getValue().getSubPrice();

}

returntotalPrice;

}

publicvoidsetTotalPrice(doubletotalPrice){this.totalPrice=totalPrice;}

}

S3-3:

在src目录下新建web.BuyServlet,映射为/buy.do,实现图书购买处理。

在其doGet方法中添加代码如下:

//获取图书id

intid=Integer.parseInt(request.getParameter("id"));

//根据id获取图书对象

BookDaodao=newBookDao();

Bookbook=dao.getOne(id);

/*将待购买的图书存放到购物车中

*①从session中获取购物车,若还没有购物车则创建购物车对象,并添加到session中

*②若待购买的图书在购物车中已经存在,则修改购物车中该购物项的数量+1

*③若待购买的图书在购物车中不存在,则在购物车中新增购物项,且数量为1

*/

HttpSessionsession=request.getSession();

ShoppingCartcart=(ShoppingCart)session.getAttribute("cart");

if(cart==null){

cart=newShoppingCart();

session.setAttribute("cart",cart);

}

ShoppingCartItemitem=cart.getItems().get(id);

if(item==null){

item=newShoppingCartItem();

item.setBook(book);

item.setCount

(1);

}else{

item.setCount(item.getCount()+1);

}

cart.getItems().put(id,item);

//到showCart.jsp中显示购物车

//request.getRequestDispatcher("/showCart.jsp").forward(request,response);?

为什么不能用

response

配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
javaweb 实验
提示  冰点文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:javaweb实验9.docx
链接地址:https://www.bingdoc.com/p-6263434.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2


收起
展开