Java简单购物车设计

        还是存在一些问题(比如clear函数没效果、乱码,单独测试也没,确实找不到问题),路过的给解决下。。。

第一部分     会话控制

会话跟踪:

  • 使用隐藏表单字段
  • URL重写,实际就是直接把状态信息加到URL
  • 持久Cookie
  • 会话HttpSession

        今天写filter的时候找不到web.xml了,急坏了我,原来是我在创建动态网站项目的时候,输入项目名称后就立刻按finish了,我刚才创建了另一个项目,一步一步next地来,在最后那步可以是否创建web.xml文件,最后把这个web.xml文件拷贝到原来项目的WEB-INF目录下就正常运行了(servlet3.0)。

页面编码问题:

        首先,1在 eclipse 中配置 workspace 项下 text file encoding 属性的值来决定此工作区间下所有的 eclipse 项目的文档的编码属性。 Window-->Preferences-->General-->Workspace, 在 workspace 下配置 text file encoding 的属性值是 other, 在 other 中选择 ”utf-8”, 配置完成后,点出 ok 保存。

         其次,windows->Preferences...打开"首选项"对话框,左侧导航树,导航到 general->Content Types,右侧Context Types树,点开Text,选择Java Source File,在下面的Default encoding输入框中输入UTF-8,点Update,则设置Java文件编码为UTF-8。已经搞好的就用批量转码工具。

session的常用方法:

1、 getAttribute():从session中获取以前存储的值
2、 getAttributeNames():返回session中所有属性的名称
3、 setAttribute():将键与值关联起来,存储进session
4、 removeAttribute():删除session中存储的对应键的值
5、 invalidate():删除整个session及其存储的键值
6、 logout():注销当前用户
7、 getId():获取每个session对应的唯一ID
8、 getCreationTime():获取session创建的时间
9、 getLastAccessedTime():获取session最后被访问的时间
10、getMaxInactiveInterval():在用户没有访问的情况下,会话在被自动废弃之前应该保持多长时间
  

第二部分   JS控制

        html页面的所有元素均可以通过document.getElementById得到;document.forms["表单明"]或者document.forms["表单序号"]访问表单元素;document.frames["框架明"].document访问框架。

         值得注意的getElementById这个方法查找的时候,会以传入的参数与第一次name属性值相同的元素查找出来。

 
 
 <script type="text/javascript">
  
  function get()
  {
     //也可以直接通过元素的属性Id来直接获取
     var ipv6 = document.getElementById("ip").value;
     alert("ipv6: " + ipv6); //55::66
     
     var ipv4 = document.getElementById("ip").value;
     alert("ipv4: "+ipv4);  //55::66
     
     var ipv44 = document.getElementById("ipv4").value;
     alert(ipv44);       //1.1.1.1
  }
  
  </script>
  
  <body>
    <form action="hehe.jsp" id="one" method="post" name="one1" >
      ipv6 : <input type="text"   name="ip"  value="55::66" /><br>
      ipv4 : <input type="text"   id="ip" name="ipv4" value="1.1.1.1" /><br>
      <input type="button" value = "button" onclick="get()">
      </form>
  </body>


        getElementsByName(name),name这个属性在元素中是可以相同的也就是这个方法取出的是个集合(ID默认是唯一的,所以不实际和)。注意方法名,我老是写错少了“s”,这个方法与getElementById不太一样。

 
 
 <script type="text/javascript">
  
  function get()
  {
     //也可以直接通过元素的属性name来直接获取
     var ip = document.getElementsByName("ip");
     var ipv6 = ip[0].value;
     alert("ipv6: " + ipv6); //55::66
     
     var ipv4 = ip[1].value;
     alert("ipv4: "+ipv4);  //1.1.1.1
     
  }
  
  </script>
  
  <body>
    <form action="hehe.jsp" id="one" method="post" name="one1" >
      ipv6 : <input type="text"   id="ipv6" name="ip" value="55::66" /><br>
      ipv4 : <input type="text"   id="ipv4" name="ip" value="1.1.1.1" /><br>
      <input type="button" value = "button" onclick="get()">
      </form>
  </body>

        getElementByTagName(tagname),根据元素来查询,返回的值肯定也是集合啦!可以用来得到表单各个字段的名字。

 
 
 <script type="text/javascript">
  
  function get()
  {
     //也可以直接通过元素的属性tagname来直接获取
     var ip = document.getElementsByTagName("input");
     for(var i=0;i<ip.length;i++)
     {
        alert(ip[i].value); //55::66  1.1.1.1  button
     }
  }
  
  </script>
  
  <body>
    <form action="hehe.jsp" id="one" method="post" name="one1" >
      ipv6 : <input type="text"   id="ipv6" name="ip" value="55::66" /><br>
      ipv4 : <input type="text"   id="ipv4" name="ip" value="1.1.1.1" /><br>
      <input type="button" value = "button" onclick="get()">
      </form>
  </body>

 

        servlet处理复选框String[] param = request.getParameterValues("checkbox1")
        表格变色:document.getElementById(row).style.backgroundColor="#FF0000";


第三部分  代码设计

 
 
import java.io.IOException;
 
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class FilterChar implements Filter {
 
 
	//�ýӿھ����������init��destroy��doFilter
	private String config;
 
 
	public void destroy() {
		this.config = null;
	}
 
 
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req;
		req = (HttpServletRequest) request;
 
 
		HttpServletResponse res;
		res = (HttpServletResponse) response;
		
		req.setCharacterEncoding(config);
		res.setCharacterEncoding(config);
		
		chain.doFilter(req, res);
 
 
	}
 
 
	public void init(FilterConfig config) throws ServletException {
		this.config = config.getInitParameter("encode");
	}
}
 
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
 
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
 
 
 
public class FruitBuy extends HttpServlet{
 
 
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
 
 
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
 
 
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = req.getSession(true);
		HashMap cart = (HashMap)session.getAttribute("cart");
		if(null==cart) {
			cart = new HashMap();
			//Ĭ����object
			cart.put("J2SE","0");
			cart.put("J2EE","0");
			cart.put("J2ME","0");
			session.setAttribute("cart", cart);
		}
		PrintWriter out = res.getWriter();
		res.setContentType("text/html; charset=UTF-8");
		req.setCharacterEncoding("UTF-8");
		int j2se = Integer.parseInt(req.getParameter("book1amount"));
		int j2ee = Integer.parseInt(req.getParameter("book2amount"));
		int j2me = Integer.parseInt(req.getParameter("book3amount"));
		
		//object����ֱ��תΪint����
		int oldJ2se = (int)cart.get("J2SE");
		int oldJ2ee = (int)cart.get("J2EE");
		int oldJ2me = (int)cart.get("J2ME");
		
		cart.put("J2SE", oldJ2se);
		cart.put("J2EE", oldJ2ee);
		cart.put("J2ME", oldJ2me);
		
		out.println("����������"+cart.get("J2SE")+cart.get("J2EE")+cart.get("J2ME"));
		out.println("<hr>");
		out.println("�ص����"+"<a href=\"res.encodeRedirectURL(\"buy.jsp\") />");
	}
}
 
 
 
 
<%@ page language="java" contentType="text/html; Charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>ÍøÉÏÊéµê是</title>
	<script type="text/javascript" language="JavaScript">
		function clear1() {
			document.forms["fruitform"].book1amount.value="";
		}
		function clear2() {
			document.forms["fruitform"].book2amount.value="";
		}
		function clear3() {
			document.forms["fruitform"].book3amount.value="";
		}
		function changecolor( row) {
		//C大写
			document.getElementById(row).style.backgroundColor="#FF0000";
		}
		function resetcolor( row) {
		//C大写
			document.getElementById(row).style.backgroundColor="#FFFFFF";
		}
		//ÒòΪajax²»»á£¬È¨µ±Á·Ï°js
		function show1() {
			document.forms["fruitform"].book1amount.value =document.getElementById(book1amount).value;
			window.alert("Äú¹ºÂòÁË:" + document.forms["fruitform"].book1amount.value+"±¾");
		}
		function show2() {
			document.forms["fruitform"].book2amount.value =document.getElementById(book2amount).value;
			window.alert("Äú¹ºÂòÁË:" + document.forms["fruitform"].book2amount.value+"±¾");
		}function show3() {
			document.forms["fruitform"].book3amount.value =document.getElementById(book3amount).value;
			window.alert("Äú¹ºÂòÁË:" + document.forms["fruitform"].book3amount.value+"±¾");
		}
	</script>
</head>
<body>
<form action="FruitBuy" name="fruitform" method="get">
	<hr align="center" size="1">
	<table >
		<!-- ±ØÐë·ÅÔÚtableÄÚ²¿ -->
		<caption>»¶Ó­À´µ½ÎÒµÄÍøÉÏÊéµê</caption>
		<tr id="row">
			<th>ÖÖÀà</th><th>µ¥¼Û</th><th>ÊýÁ¿</th>
		</tr>
		<tr id="row1"  onMousemove="changecolor('row1')" onMouseout="resetcolor('row1')">
			<td>J2SE¾«±à</td><td>22.0</td>
			<td><input type="text" name="book1amount"  id="book1amount" value="ÇëÊäÈ빺ÂòÊýÁ¿" onFocus="clear()" onBlur="show1()"></td>
		</tr>
		<tr id="row2"  onMousemove="changecolor('row2')"  onMouseout="resetcolor('row2')">
			<td>J2EE¾«±à</td><td>36.0</td>
			<td><input type="text" name="book2amount"  id="book2amount" value="ÇëÊäÈ빺ÂòÊýÁ¿" onFocus="clear()" onBlur="show2()"></td>
		</tr>
		<tr id="row3" onMousemove="changecolor('row3')"  onMouseout="resetcolor('row3')">
			<td>J2ME¾«±à</td><td>22.0</td>
			<td><input type="text" name="book3amount"  id="book3amount" value="ÇëÊäÈ빺ÂòÊýÁ¿" onFocus="clear()" onBlur="show3()"></td>
		</tr>
	</table>
	<hr align="center" size="1">
	<input type="submit" value="·ÅÈ빺Îï³µ">
</form>
</body>
</html>
 
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>WEB</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>FilterChar</filter-name>
    <filter-class>FilterChar</filter-class>
    <init-param>
      <param-name>encode</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>FilterChar</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值