Java web:含個(gè)人詳細(xì)總結(jié),EL表達(dá)式param,JSP,servlet作業(yè)【詩書畫唱】
實(shí)現(xiàn)珠寶店購物車功能:



package com.Servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
?* Servlet implementation class CartServlet
?*/
@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public CartServlet() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request,
*? HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,?
HttpServletResponse response) throws?
ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest
*? request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)?
throws ServletException, IOException {
// TODO Auto-generated method stub
//獲取商品名稱和價(jià)格:
String name = request.getParameter("name");
name = new String(name.getBytes("iso8859-1"),"utf-8");
String strPrice = request.getParameter("price");
//將strPrice轉(zhuǎn)換成數(shù)字:
Double price = 0.0;
if(strPrice != null && strPrice.length() > 0) {
price = Double.parseDouble(strPrice);
}
// System.out.println(name);
// System.out.println(strPrice);
//封裝數(shù)據(jù):
Product p = new Product();
p.setName(name);
p.setPrice(price);
//獲取session對(duì)象:
HttpSession session = request.getSession();
//嘗試著從session中獲取購物車對(duì)象(_cart):
Object objCart = session.getAttribute("_cart");
//什么樣的情況下就是第一次添加購物車:
if(objCart == null) {
//如果是第一次添加購物車,就必須new出這個(gè)購物車
//個(gè)人的總結(jié):因?yàn)閟ession.getAttribute("_cart")獲取自要用
// session.setAttribute("_cart", cart);發(fā)送到的
// cartShow.jsp文件。:
Map<String,Integer>cart = new HashMap<String,Integer>();
cart.put(name, 1);
System.out.println(cart);
//將cart放到session中去:
session.setAttribute("_cart", cart);
} else {
//如果是第n次添加購物車,將objCat轉(zhuǎn)換成Map:
Map<String,Integer>cart = (HashMap<String,Integer>)objCart;
//判斷選中的商品在map中是否已經(jīng)存在:
//contain:包含【英[k?n?te?nz] 】
if(cart.containsKey(name)){//如果選中的商品已經(jīng)存在
//數(shù)量加1:
Integer count = cart.get(name);
count ++;
//將加1以后的數(shù)量放回map中
cart.put(name, count);
} else {
cart.put(name, 1);
}
System.out.println(cart);
//將cart放回session中:
session.setAttribute("_cart", cart);
}
//跳轉(zhuǎn)回shopping.jsp:
request.getRequestDispatcher("shoppingStart.jsp")
? ? .forward(request, response);
}
}


package com.Servlet;
public class Product {
? ? private Integer id;
? ? private String name;
? ? private Double price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}


package com.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* Servlet implementation class shoppingServ
?*/
@WebServlet("/shoppingServlet")
public class shoppingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public shoppingServlet() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//商品名稱:
String name = request.getParameter("name");
//中文亂碼處理:
name = new String(name.getBytes("iso8859-1"),"utf-8");
// String strPrice = request.getParameter("price");
// //商品價(jià)格
// Double price = 0.0;
// //如果strPrice不為空,就轉(zhuǎn)換成Double類型
// if(strPrice != null && strPrice.length() > 0) {
// price = Double.parseDouble(strPrice);
// }
// //商品圖片路徑:
// String src = request.getParameter("src");
request.setCharacterEncoding("gbk");
request.getRequestDispatcher("detailShow.jsp?name=" + name)
? ? .forward(request, response);
}
}


<%@page import="java.util.Iterator"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()
? ? +"://"+request.getServerName()
? ? +":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? * {
? ? ? ? ? ? ? ? font-size:50px;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ? ? ? <h1>購物車列表</h1>
? ? ? ? <ul>
? ? ? <%--自己的總結(jié):
? ? ??
? ? 當(dāng)點(diǎn)擊“添加商品按鈕后”,觸發(fā)方法addCart,通過
? ? “addCart('${param.name}',${param.price})”??
? ? 傳值給CartServlet.java文件中。
? ? 在CartServlet.java文件中用
? ? request.getParameter("name")等獲取。
用session.setAttribute("_cart", cart);
將cart放回session中。 --%>
? ? ? ? <%
? ? ? ? ? ? // 因?yàn)樵贑artServlet.java
? ? ? ? ? ?// 文件中有session.setAttribute("_cart", cart);
? ?///? 所以可以用 session.getAttribute("_cart");獲取購物車cart:
? ? ? ? ? ? Object objCart = session.getAttribute("_cart");
? ? ? ? ? ? Map<String,Integer>cart = new HashMap<String,Integer>();
? ? ? ? ? ? if(objCart != null) {
? ? ? ? ? ? cart = (HashMap<String,Integer>)objCart;
? ? ? ? ? ? }
? ? ? ? ? ? Set<String>keys = cart.keySet();
? ? ? ? ? ??
? ? ? ? ? ? //下面是用迭代器遍歷添加到購物車中的內(nèi)容:
? ? ? ? ? ? Iterator<String> it = keys.iterator();
? ? ? ? ? ? while(it.hasNext()) {
? ? ? ? ? ? //商品名稱:
? ? ? ? ? ? String name = it.next();? ??
? ? ? ? ? ? //商品數(shù)量:
? ? ? ? ? ? Integer count = cart.get(name);
? ? ? ? ? ? %>
? ? ? ? ? ? <li><%=name %>:<%=count %></li>
? ? ? ? ? ? <%
? ? ? ? ? ? }
? ? ? ? %>
? ? ? ? </ul>
? ? ? ? <a hreff="shoppingStart.jsp">返回</a>
? ? </body>
</html>


<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()
? ? +"://"+request.getServerName()
? ? +":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <script type="text/javascript">
? ? ? ? ? ? //添加到購物車功能
? ? ? ? ? ? function addCart(name,price){
? ? ? ? ? ? window.location.hreff = 'CartServlet?name=' + name?
? ? ? ? ? ? + '&price=' + price;
? ? ? ? ? ? }
? ? ? ? </script>
? ? </head>
? ? <body>
? ? ? ? <h1>顯示珠寶詳情</h1>
? ? ? ? <%--
? ? ? ? 個(gè)人的總結(jié):
? ? ? ? 如果用“?XXX=YYY”等的格式地址欄等傳參,
? ? ? ? 那么可以用${param.XXX }獲取YYY的值
? ? ? ? ?--%>
? ? ? ? <h1>商品名稱:${param.name }</h1>
? ? ? ? <h1><img src="${param.src }" /></h1>
? ? ? ? <h1>¥${param.price }</h1>
? ? ? ? <input id="btn" type="button" value="加入購物車"
? ? ? ? ? ? onclick="addCart('${param.name}',${param.price});" />
? ? </body>
</html>


<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()
? ? +"://"+request.getServerName()
? ? +":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? * {
? ? ? ? ? ? ? ? font-size:52px;
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? </style>
? ? ? <%-- 個(gè)人的總結(jié):
? ? ? ? 如果路徑名為XXX格式,那么XXX是servlet文件中
? ? ? ? 用@WebServlet("/shoppingServlet")聲明的路徑;
? ? ? ? ? ? ? ?如果路徑名為XXX.jsp格式,那么XXX是jsp文件
? ? ? ? 的路徑,在同級(jí)目錄下相對(duì)路徑為“文件名+后綴”;
? ? ? ? ?--%>?
? ? ? ? <script type="text/javascript">
? ? ? ? ? ? function detail(name,price,src){
? ? ? ? ? ? window.location.hreff = 'shoppingServlet?name=' + name?
? ? ? ? ? ? + '&price=' + price + '&src=' + src;
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? function viewCart(){
? ? ? ? ? ? window.location.hreff= 'cartShow.jsp';
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? </script>
? ? </head>
? ? <body>
? ? ? ? <table border="1" align="center">
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <th>珠寶名稱</th>
? ? ? ? ? ? ? ? <th>珠寶圖片</th>
? ? ? ? ? ? ? ? <th>珠寶價(jià)格</th>
? ? ? ? ? ? ? ? <th><a hreff="javascript:viewCart();">查看購物車</a></th>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <td>橙寶石</td>
? ? ? ? ? ? ? ? <td><img src="img/14.png" /></td>
? ? ? ? ? ? ? ? <td>¥200000</td>
? ? ? ? ? ? ? ? <td><a hreff="javascript:detail
? ? ? ? ? ? ? ? ('橙寶石',200000,'img/14.png');">查看詳情</a></td>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <td>紫晶</td>
? ? ? ? ? ? ? ? <td><img src="img/16.png" /></td>
? ? ? ? ? ? ? ? <td>¥300000</td>
? ? ? ? ? ? ? ? <td><a hreff="javascript:detail
? ? ? ? ? ? ? ? ('紫晶',300000,'img/16.png');">查看詳情</a></td>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <td>紅寶石</td>
? ? ? ? ? ? ? ? <td><img src="img/41.png" /></td>
? ? ? ? ? ? ? ? <td>¥500000</td>
? ? ? ? ? ? ? ? <td><a hreff="javascript:
? ? ? ? ? ? ? ? detail('紅寶石',500000,'img/41.png');">查看詳情</a></td>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <td>紫寶石</td>
? ? ? ? ? ? ? ? <td><img src="img/5.png" /></td>
? ? ? ? ? ? ? ? <td>¥100000</td>
? ? ? ? ? ? ? ? <td><a hreff="javascript:
? ? ? ? ? ? ? ? detail('紫寶石',100000,'img/5.png');">查看詳情</a></td>
? ? ? ? ? ? </tr>
? ? ? ? </table>
? ? </body>
</html>









