1数据库连接池的现实
shopcart10中
在容器中初始化数据库的连接池 其只能在跟容器相关的类中使用,因为连接都是在服务器,不是在你本地PC上 所以本地代码不能用数据库连接池
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Stack;
//数据库产生连接 关闭文件
public class DBPool {
private String driver;
private String url;
private String username;
private String password;
private Stack<Connection> pool;
public DBPool(){
pool = new Stack<Connection>();
}
/**
* @explain
* @param
* @exception
* @return
*/
private synchronized Connection openConn(){
System.out.println(driver);
System.out.println(url);
System.out.println(username);
System.out.println(password);
System.out.println("openConn");
Connection con = null;
try{
Class.forName(driver);
con =DriverManager.getConnection(url,username,password);
System.out.println("产生连接");
}catch(Exception e){
e.printStackTrace();
}
return con;
}
public synchronized Connection getConn(){
if(pool.size()<=0){
Connection con = this.openConn();
pool.push(con);
}
return pool.pop();
}
public synchronized void close(Connection con){
pool.push(con);
}
public String getDriver() {
return driver;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public Stack<Connection> getPool() {
return pool;
}
public void setDriver(String driver) {
this.driver = driver;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setPool(Stack<Connection> pool) {
this.pool = pool;
}
}
*********
package db;
//新建一个连接池
public class GlobalVars {
static public DBPool pool = new DBPool();
//static public int uid=125;
}
************
package commons;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.DBPool;
import db.GlobalVars;
//开启服务,初始化连接池
public class WebInit extends HttpServlet {
@Override
public void init() throws ServletException{
//获取数据库连接参数
System.out.println("环境正在初始化...");
String driver = this.getServletContext().getInitParameter("driver");
String url = this.getServletContext().getInitParameter("url");
String username = this.getServletContext().getInitParameter("user");
String password = this.getServletContext().getInitParameter("password");
//初始化连接池
// GlobalVars.pool = new DBPool();
GlobalVars.pool.setDriver(driver);
GlobalVars.pool.setPassword(password);
GlobalVars.pool.setUrl(url);
GlobalVars.pool.setUsername(username);
System.out.println(driver);
System.out.println(url);
System.out.println(username);
System.out.println(password);
System.out.println("getConn:"+GlobalVars.pool.getConn());
System.out.println("getDriver:"+GlobalVars.pool.getDriver());
System.out.println("环境初始化完毕");
}
@Override
protected void service(HttpServletRequest req,HttpServletResponse resp){
}
}
**********
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Parameters of Database Connection -->
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://127.0.0.1:3306/tarena_shopcart?characterEncoding=gb2312</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value></param-value>
</context-param>
<servlet>
<servlet-name>globlinit</servlet-name>
<servlet-class>commons.WebInit</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
**********************************
**********************************
2登入界面的实现,采用自定义标签
index.jsp测试页面
<%@page pageEncoding="utf-8"%>
<%@taglib prefix="lg" uri="/WEB-INF/tlds/ui.tld"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>在线购物</title>
<script type="text/javascript">
function login() {
var suser = document.getElementById("user").value;
var spass = document.getElementById("password").value;
var scode = document.getElementById("code").value;
var url = "http://127.0.0.1:8080/Testshopcart/servlet/LoginServlet";
url += "?";
url += "user=" + suser + "&";
url += "password=" + spass + "&";
url += "code=" + scode;
alert(url);
var aj = new XMLHttpRequest();
aj.open("get", url, true);
aj.onreadystatechange = function() {
if (aj.readyState == 4 && aj.status == 200) {
var flag = aj.getResponseHeader("flag");
alert(flag);
if (flag == 1) {
alert("成功" + aj.responseText);
} else {
alert("失败" + aj.responseText);
}
}
}
aj.send(null);
}
</script>
</head>
<body>
<lg:login imgurl="servlet/ImgServlet" event="login();" />
</body>
</html>
**********
自己定义的标签文件
package taglibs;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
*
* @author tarena
*1 写TagSupport
*写tld 文件 修改模板 属性
*在jsp中使用《%@taglib prefix="" uri=""%》
*
*
*/
public class LoginTag extends TagSupport {
private String imgurl;
private String event;
@Override
public int doEndTag() throws JspException {
JspWriter out=pageContext.getOut();
//判定session是否登入
HttpSession session=pageContext.getSession();
try {
if(session !=null&session.getAttribute("login")!=null){
//没有登入,显示登入ui 已经登入显示用户维护ui
out.println("已经登入");
}else{
out.println("<div>");
out.println("<table>");
//登入名
out.println("<tr>");
out.println("<td align=\"left\">用户:<input id='user' type='text' name='user' style='width:60;'/></td>");
out.println("</tr>");
//口令
out.println("<tr>");
out.println("<td align=\"left\">口令:<input id='password' type='password' name='password' style='width:60;'/></td>");
out.println("</tr>");
//教研码
out.println("<tr>");
out.println("<td>校验码: <input id='code' type='text' name='code'/><img width='80px' height='20px' src='"+imgurl+"?param='"+new Date().getTime()+" width='40' height='15' οnclick='location.reload()'/></td>");
out.println("</tr>");
//确定
out.println("<tr>");
out.println("<td align='center'><input type='button' value='登入' οnclick='"+event+"'/></td>");
out.println("</tr>");
out.println("</table>");
out.println("</div>");
}
} catch (IOException e) {
throw new JspException("登入问题");
}
return EVAL_PAGE;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
}
*******************
标签配置文件 存放在/WEB-INF/tlds/ui.tld
<?xml version="1.0" encoding="utf-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="..."
version="2.0">
<tlib-version>2.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>shopcart</short-name>
<tag>
<name>login</name>
<tag-class>taglibs.LoginTag</tag-class>
<body-content>EMPTY</body-content>
<attribute>
<name>imgurl</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>event</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
***************
package com.ren;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
//绘制验证码的,随机产生4个数字
public class ImgServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
//产生随机数,绘制成图像 输出图像
Random r=new Random();
String scode="";
for(int i=0;i<4;i++){
scode=scode+r.nextInt(10)+"";
}
//产生缓冲图像
BufferedImage imgbuf=new BufferedImage(60,20,BufferedImage.TYPE_INT_BGR);
//取得缓冲的绘制环境
Graphics2D g=imgbuf.createGraphics();
//开始绘制
g.setColor(Color.white);
g.fillRect(0, 0, 60, 20);//填充指定颜色
g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.BOLD,18));
g.drawString(scode,3,18);
//转码
ServletOutputStream out=response.getOutputStream();
//
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
encoder.encode(imgbuf);
out.close();
//
}
}
***************
package com.ren;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//用户登入 并通过查数据库进行检测
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String code=request.getParameter("code");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String uname = request.getParameter("user");
String upassword = request.getParameter("password");
if(uname.equals("ren")&&upassword.equals("123")&&code!=null){
response.addHeader("flag", "1");
out.println("success rensongxin");
}
else{
response.addHeader("flag", "2");
out.println("failure rensongxin");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.ren.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
***************************
***************************
3其他部分重要代码
package commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import db.GlobalVars;
//点击购买商品 把商品信息添加到购物的session
public class CarServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String pid = (String) request.getParameter("pid");//商品的编号
Connection con = GlobalVars.pool.getConn();
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
HashSet<Item> set=(HashSet) session.getAttribute("set");
//第一次购买商品 有问题
if(set==null){session.setAttribute("set", new HashSet<Item>());}
else{
Item item=new Item();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select pname,pprice,pamount,ptypeid from Products where pid=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, pid);
rs = ps.executeQuery();
if(rs.next()){
out.println("pid:"+pid+"<br>");
out.println("pname:"+rs.getString(1)+"<br>");
out.println("pprice:"+rs.getString(2)+"<br>");
out.println("pamount:"+rs.getString(3)+"<br>");
item.setPid(pid);
item.setPname(rs.getString(1));
item.setPprice(Double.parseDouble(rs.getString(2)));
item.setPamount(Integer.parseInt(rs.getString(3)));
///暂时为2
item.setBuyamount(2);
set.add(item);
session.setAttribute("set", set);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Iterator<Item> it=set.iterator();
while(it.hasNext()){
Item p=it.next();
out.println(p.getPname()+p.getPprice());
}
}
GlobalVars.pool.close(con);
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
************
package commons;
import java.io.IOException;
//相应商品图片的产生
public class ImgProduct extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String key1=(String)request.getParameter("pid");
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
Connection con = GlobalVars.pool.getConn();
System.out.println("con:" + con);
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select pphoto from Products where pid=?";
try {
ps = con.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(key1));
rs = ps.executeQuery();
if (rs.next()) {
InputStream input = rs.getBlob(1).getBinaryStream();
byte[] b = new byte[1024];
int x = 0;
while ((x = input.read(b)) != -1) {
out.write(b, 0, x);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
GlobalVars.pool.close(con);
}
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
***********
package commons;
//商品类,描述商品的基本信息
public class Item {
private String pid;
private String pname;
private double pprice;
private int pamount;
private int buyamount;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPprice() {
return pprice;
}
public void setPprice(double pprice) {
this.pprice = pprice;
}
public int getPamount() {
return pamount;
}
public void setPamount(int pamount) {
this.pamount = pamount;
}
public int getBuyamount() {
return buyamount;
}
public void setBuyamount(int buyamount) {
this.buyamount = buyamount;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pid == null) ? 0 : pid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item other = (Item) obj;
if (pid == null) {
if (other.pid != null)
return false;
} else if (!pid.equals(other.pid))
return false;
return true;
}
}
**********
package commons;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.jms.Session;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import db.GlobalVars;
//输入商品类别,通过数据库来查询,包括商品图片的查询
public class SelectP extends HttpServlet {
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String key1 = request.getParameter("key1");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
Connection con = GlobalVars.pool.getConn();
System.out.println("con:" + con);
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select pid,pname,pprice,pphoto,pamount,ptypeid from Products where ptypeid=?";
try {
ps = con.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(key1));
rs = ps.executeQuery();
out.println("<table border=1 width=‘60%’ >");
while (rs.next()) {
out.println("<tr>");
out.println("<table border=1 width=‘60%’>");
out.println("<tr>");
out.println("<td width=‘30’>");
String pid = rs.getString(1);
//图片的产生
out.println("<img width='200px' height='90px' src='servlet/ImgProduct?pid="+pid+"'/>");
out.println("</td>");
out.println("<td width=‘300’>");
//date
out.println(" Pid:"+rs.getString(1)+"<br>");
out.println(" Pname:"+rs.getString(2)+"<br>");
out.println(" Pprice:"+rs.getString(3)+"<br>");
out.println(" Pamount:"+rs.getString(5)+"<br>");
out.println(" Ptype:"+rs.getString(6)+"<br>");
out.println("</td>");
out.println("<td width=‘300’>");
//date2添加到购物车
out.println("<form method='get' action='servlet/CarServlet'>");
out.println("<input type='text' value='"+pid+"' name='pid'>");
out.println("<input type='submit' value='添加到购物车'>");
out.println("</form>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</tr>");
}
out.println("</table>");
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
GlobalVars.pool.close(con);
}
out.flush();
out.close();
}
}
********************
package commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.GlobalVars;
//显示购物车中的商品
public class ShowServlet extends HttpServlet {
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
HashSet<Item> set=(HashSet)request.getSession().getAttribute("set");
out.println("<h3 align='center'>欢迎使用SHOPCART1.0系统 cart显示部分<h3>");
out.println("<hr>");
out.println("<table align='center' border=1 width='80%' height='100'>");
out.println("<tr>");
out.println("<td>商品名称</td>");
out.println("<td>单价</td>");
out.println("<td>库存</td>");
out.println("<td>购买数量</td>");
out.println("<td>金额</td>");
out.println("<td>退货</td>");
out.println("</td>");
out.println("</tr>");
if(set==null){
out.println("<tr>");
out.println("暂时没有商品");
out.println("</tr>");
}
else{
Iterator<Item> it=set.iterator();
while(it.hasNext()){
Item p=it.next();
out.println("<tr>");
out.println("<td>"+p.getPname()+"</td>");
out.println("<td>"+p.getPprice()+"</td>");
out.println("<td>"+p.getPamount()+"</td>");
out.println("<td>"+p.getBuyamount()+"</td>");
out.println("<td>"+p.getPprice()*p.getBuyamount()+"</td>");
out.println("<td>退货</td>");
out.println("</td>");
}
out.println("<tr>");
out.println("总金额:"+0+" "+"结算"+"清空购物车");
out.println("</tr>");
}
out.println("</table>");
out.flush();
out.close();
}
}
**********
package commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.GlobalVars;
//用户的功能 ,包括用户的登入 注册 写的很乱
public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
String path = request.getServletPath();
path = path.substring(0, path.indexOf("."));
if (path.equals("/register")) {
System.out.println("my register");
addUser(request, response);
} else if (path.equals("/login")) {
System.out.println("my login");
//findUser(request, response);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "the path : "
+ path + " is not a valid path.");
}
}
//int uid 由时间产生;//添加用户的方法
public void addUser(HttpServletRequest request, HttpServletResponse response) {
Date d = new Date();
SimpleDateFormat bartDateFormat2 = new SimpleDateFormat(
"yyMMddHHmm");
int uid=Integer.parseInt(bartDateFormat2.format(d));
Connection con = GlobalVars.pool.getConn();
System.out.println("con:" + con);
PreparedStatement ps = null;
String umail = uid + "@tarena.com";
String uname = request.getParameter("userName");
String upassword1 = request.getParameter("password1");
String upassword2 = request.getParameter("password2");
if(findUser(request,response)==1){
System.out.println("username, password haved");return ;
}
if (upassword1.equals(upassword2)) {
String sql = "insert into UserInfo(umail,uid,uname,upassword) values(?,?,?,?)";
System.out.println(sql);
try {
ps = con.prepareStatement(sql);
int index = 1;
ps.setString(index++, umail);
ps.setInt(index++,uid);
ps.setString(index++, uname);
ps.setString(index++, upassword1);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
GlobalVars.pool.close(con);
}
} else {
System.out.println("2次密码不同");
}
}
//查找是否有username passwrod 相同的 有返回1
public int findUser(HttpServletRequest request,
HttpServletResponse response) {
String uname = request.getParameter("userName");
String upassword = request.getParameter("password1");
Connection con = GlobalVars.pool.getConn();
System.out.println("con:" + con);
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select uname,upassword from UserInfo where uname=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, uname);
rs = ps.executeQuery();
while (rs.next()) {
String name=rs.getString(1);
String pass=rs.getString(2);
if(name.equals(uname)&&pass.equals(upassword)){
return 1;
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
GlobalVars.pool.close(con);
}
return 0;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
-********
可以显示时间的js文件<SCRIPT language=javascript src="<%=path%>/js/date.js" type=text/javascript></SCRIPT>
var date1=new Date();
var HH=date1.getHours();
var aa;
if(HH<6){
aa="凌晨";
}else if(HH<12)
{
aa="上午";
}else if(HH<14)
{
aa="中午";
}else if(HH<18)
{
aa="下午";
}else
{
aa="晚上";
}
document.writeln(aa+"好,欢迎光临本站!");
var date=new Date();
var YY=date.getYear();
var MM=date.getMonth()+1;
var DD=date.getDate();
var KK=date.getDay();
var HH=date.getHours();
var mm=date.getMinutes();
//var SS=date.getSeconds();
if(MM<10)MM="0"+MM;
if(DD<10)DD="0"+DD;
if(HH<10)HH="0"+HH;
if(mm<10)mm="0"+mm;
//if(SS<10)SS="0"+SS;
switch(KK)
{
case 1:
KK="星期一";
break;
case 2:
KK="星期二";
break;
case 3:
KK="星期三";
break;
case 4:
KK="星期四";
break;
case 5:
KK="星期五";
break;
case 6:
KK="星期六";
break;
case 0:
KK="星期日";
break;
}
document.writeln(" "+YY+"年"+MM+"月"+DD+"日 "+KK+" "+HH+"∶"+mm) ;
************
<%@page pageEncoding="utf-8"%>
<%@taglib prefix="ui" tagdir="/WEB-INF/tags"%>
<%@taglib prefix="lg" uri="/WEB-INF/tlds/ui.tld"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>在线购物</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript">
function login() {
//alert($("user").value);
var suser = $("user").value;
var spass = $("password").value;
var scode = $("code").value;
var url = "http://127.0.0.1:8080/shopcart/servlet/LoginServlet.do";
url += "?";
url += "user=" + suser + "&";
url += "password=" + spass + "&";
url += "code=" + scode;
alert(url);
var a = new Ajax.Request(url, {
method :"get",
onComplete :deal
});
}
function deal(a) {
if (a.readyState == 4 && a.status == 200) {
var flag = a.getResponseHeader("flag");
alert(flag);
if (flag == 1) {
alert("成功" + a.responseText);
} else {
alert("失败" + a.responseText);
}
}
}
function selectP(a){
alert(a);
var aj=new XMLHttpRequest();
var inf=document.getElementById("data");
aj.open("post","http://127.0.0.1:8080/shopcart/servlet/SelectP?key1="+a,
true);
aj.onreadystatechange=function(){
if(aj.readyState==4){
inf.innerHTML=aj.responseText;
}
}
aj.send(null);
}
</script>
</head>
<body>
<table align="center" border="1" width="65%" height="80%">
<!-- logo -->
<tr height="12%">
<td>
<ui:logo />
</td>
</tr>
<!-- data -->
<tr>
<td>
<table border="1" width="100%" height="100%">
<tr>
<td>
<jsp:include flush="true" page="js/navigate.jsp"></jsp:include>
</td>
</tr>
<tr>
<td>
<table border="1" width="100%" height="100%">
<tr>
<td width="140">
<table border="1" width="100%" height="100%">
<tr height="100">
<td>
<lg:login imgurl="servlet/ImgServlet" event="login();" />
</td>
</tr>
<tr height="100">
<td>
*商品类别*
</td>
</tr>
<tr valign="top"
style="height: 160px; margin-bottom: 0px; padding-bottom: 4px;">
<td width="100%">
<input type="button" value="Electron!"
οnclick="selectP(1);" />
<br>
<input type="button" value="Book!" οnclick="selectP(2);" />
<br>
<input type="button" value="Film!" οnclick="selectP(3);" />
<br>
<input type="button" value="Other!" οnclick="selectP(4);" />
<br>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<!-- foot -->
<tr height="20%">
<td>
<ui:foot />
</td>
</tr>
</table>
</body>
</html>