⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 check.jsp

📁 机械工业出版社的《jsp2.0动态网站开发实例指南》的源代码
💻 JSP
字号:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="shop.logBean"%>
<%!
	public String codeString(String s){
		String str=s;
                try{
			byte b[]=str.getBytes("ISO-8859-1");
			str=new String (b);
			return str;
		}catch(Exception e){
		return "error";
		}
	}
%>
<%
	String userid="";
	if(session.isNew()){
		%>
		<jsp:forward page="index.jsp"/>
		<%
	}
	else{
		%>
		<jsp:useBean id="log" class="shop.logBean" scope="session"/>
		<%
		String islog=log.getIslog();
		userid=log.getId();
		if(!islog.equals("true")){
			%>
			<jsp:forward page="index.jsp"/>
			<%
		}
	}
%>
<html>
<head>
<title>Shop</title>
</head>
<body bgcolor="#C0C0C0">
<center>
<jsp:getProperty name="log" property="id"/>
,结帐
<jsp:include page="head.txt"/>
<form name="form" method="post" action="check.jsp">
<input type="radio" name="checkway" value="mail">邮寄
<input type="radio" name="checkway" value="card">信用卡
<br>
<input type="submit" name="submit" value="确认">
</form>
</center>
<%
	String checkway="0";
	String cw=request.getParameter("checkway");
	if(cw==null)cw="";
	cw=codeString(cw);
	if(cw.equals("mail")||cw.equals("card"))
	{	
		if(cw.equals("mail"))checkway="0";
		if(cw.equals("card"))checkway="1";
		java.util.Date date = new java.util.Date();
		String time=date.toString();
		String goodsid="";
		String goodsname="";
		long oldamount=0;
		long amount=0;
		float price=0;
		float checkout=0;
		HashMap cart;
		if(session.getAttribute("myCart")==null){
		cart=new HashMap();
		session.setAttribute("myCart",cart);
		}
		cart=(HashMap)session.getAttribute("myCart");
		Set cartKeySet=cart.keySet();
		Iterator cartKeyIterator=cartKeySet.iterator();
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  			Connection con;
  			String condition;
  			con=DriverManager.getConnection("jdbc:odbc:myshop","","");
			while(cartKeyIterator.hasNext()){
				goodsname=(String)(cartKeyIterator.next());
				if(!(goodsname==null)){
					//out.println("over");
					Integer temp=(Integer)(cart.get(goodsname));
					amount=temp.intValue();
					//out.println(goodsname+"<br>");
					condition="select * from goods where name='"+goodsname+"'";
					Statement sql=con.createStatement();
    					ResultSet rs=sql.executeQuery(condition);
     					if(rs.next()){
     						//out.println(goodsname+"<br>");
        					price=rs.getFloat("price");
        					goodsid=rs.getString("id");
        					oldamount=rs.getLong("amount");
        					oldamount-=amount;
        					checkout=price*amount;
        				}
        				sql=con.createStatement();
        				String update="update goods set amount="+oldamount+" where name='"+goodsname+"'";
        				sql.executeUpdate(update);
        				sql.close();
        				PreparedStatement pstmt=con.prepareStatement("insert into check values (?,?,?,?,?,?,?,?);");
        				pstmt.setString(1,time);
        				pstmt.setString(2,goodsid);
        				pstmt.setString(3,goodsname);
        				pstmt.setString(4,userid);
        				pstmt.setString(5,String.valueOf(amount));
        				pstmt.setString(6,String.valueOf(price));
        				pstmt.setString(7,String.valueOf(checkout));
        				pstmt.setString(8,checkway);
        				pstmt.executeUpdate();
        				pstmt.close();
      					
				}
			}
			cart.clear();
			session.setAttribute("myCart",cart);
			con.close();
		}catch(SQLException e){
			out.println("error");
      			//response.sendRedirect("search.jsp");
    	  	}catch(ClassNotFoundException e){
    	  		out.println("error");
      			//response.sendRedirect("search.jsp");
  		}
  	//out.println("error");
	response.sendRedirect("search.jsp");		
  	}
%>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -