连接数据库:
package com.anhuiU.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
public class ConnectionManage {
public static Connection getConnection(){
Connection con=null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel?useUnicode=true&characterEncoding=GBK","root","1234");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void stmtClose(Statement stmt) {
// TODO Auto-generated method stub
if(stmt !=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
}
public static void rsClose(ResultSet rs) {
// TODO Auto-generated method stub
if(rs !=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
}
}
数据库设计:
/*
MySQL Data Transfer
Source Host: localhost
Source Database: hotel
Target Host: localhost
Target Database: hotel
Date: 2009-5-7 7:48:30
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for message
-- ----------------------------
CREATE TABLE `message` (
`id` int(11) NOT NULL auto_increment,
`topic` varchar(255) default NULL,
`content` text,
`dates` date default NULL,
`type` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for order
-- ----------------------------
CREATE TABLE `order` (
`id` int(11) NOT NULL auto_increment,
`orderId` int(11) NOT NULL,
`addressId` int(11) default NULL,
`roomType` varchar(255) default NULL,
`roomNum` int(11) default NULL,
`inTime` date default NULL,
`outTime` date default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;
-- ----------------------------
-- Table structure for order_main
-- ----------------------------
CREATE TABLE `order_main` (
`orderId` int(11) NOT NULL auto_increment,
`Contatct` int(11) default NULL,
`tel` varchar(255) default NULL,
PRIMARY KEY (`orderId`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;
-- ----------------------------
-- Table structure for roommain
-- ----------------------------
CREATE TABLE `roommain` (
`Id` int(255) NOT NULL auto_increment,
`roomType` varchar(255) NOT NULL,
`leftRoom` int(11) default NULL,
`roomNum` int(11) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=gb2312;
-- ----------------------------
-- Table structure for rooms
-- ----------------------------
CREATE TABLE `rooms` (
`Id` int(11) NOT NULL auto_increment,
`num` varchar(255) default NULL,
`roomType` int(11) default NULL,
`price` double default NULL,
`img` varchar(255) default NULL,
`state` varchar(255) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
-- ----------------------------
-- Table structure for user
-- ----------------------------
CREATE TABLE `user` (
`userId` int(11) NOT NULL auto_increment,
`userName` varchar(255) NOT NULL,
`sex` int(11) default NULL,
`tel` varchar(255) default NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`name` varchar(255) default NULL,
`regDate` date default NULL,
`jifen` int(11) default NULL,
PRIMARY KEY (`userId`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=gb2312;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `message` VALUES ('4', 'fnbh', '[Ljava.lang.String;@1e384de', '2009-05-06', 'news');
INSERT INTO `message` VALUES ('3', 'sdfgsdfg', 'dfghdfgh', '2009-04-08', 'news');
INSERT INTO `message` VALUES ('5', 'ghjfghj', 'ghjghjghkhgjkhjk,hjk,hj,khakjsdfnasbdfnbsdnbf snb sdb fjsgdfgsdfg', '2009-05-06', 'news');
INSERT INTO `roommain` VALUES ('1', '单人间', '20', '20');
INSERT INTO `roommain` VALUES ('2', '双人间', '20', '20');
INSERT INTO `roommain` VALUES ('3', '多人间', '10', '5');
INSERT INTO `roommain` VALUES ('4', '商务间', '10', '3');
INSERT INTO `roommain` VALUES ('5', '豪华间', '10', '5');
INSERT INTO `roommain` VALUES ('6', '总统套房', '5', '2');
INSERT INTO `user` VALUES ('1', '123456', '1', '5446565', '123456', '123@qq.com', 'accp400', '2009-04-16', null);
INSERT INTO `user` VALUES ('2', 'accp001', '1', 'sdfg', '123456', '123@qq.com', 'asd', '2009-04-16', null);
INSERT INTO `user` VALUES ('4', '654321', '1', '544656', '123', '383928', 'bb', '2009-04-16', '0');
INSERT INTO `user` VALUES ('5', 'aaa', '0', '544656', '123', '383928', '383928', '2009-04-16', '0');
INSERT INTO `user` VALUES ('6', 'longwu', '1', '544656', '123456', '123@qq', 'longwu', '2009-04-20', '0');
登录servlet:
package com.anhuiU.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.anhuiU.dao.MessagesDao;
import com.anhuiU.dao.UserDao;
import com.anhuiU.model.User;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
String name=request.getParameter("userName");
String pwd=request.getParameter("pwd");
String code=request.getParameter("code");
String rand=request.getSession().getAttribute("rand").toString();
UserDao ud=new UserDao();
User user= ud.checkPwd(name);
if(!code.equals(rand)){
response.sendRedirect("../error.jsp");
}
else if(!pwd.equals(user.getPassword())){
response.sendRedirect("../error2.jsp");
}
else{
request.getSession().setAttribute("user", user);
response.sendRedirect("../index.jsp");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
首页.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
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 href="<%=basePath%>">
<title>XX酒店</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
*{font-size:14px; line-height:1.5em;}
#main{width:300px;}
#title{float:left;padding:10px;width:110px;}
#content{width:110px; float:left;padding:10px;}
.font{font-size: 3; }
</style>
<script type="text/javascript">
function changeImg(){
var imgObj=document.getElementById("rdImg");
imgObj.src="";
imgObj.src="servlet/ImgServlet?num="+Math.random();
//alert(imgObj.src);
}
function forwards(){
window.location.href="regist.jsp"
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" align="center">
<tr>
<td><img src="photos/pos_r1_c1.jpg"></img></td>
<td><img src="photos/pos_r1_c2.jpg"></img></td>
</tr>
<tr>
<td colspan="2"><hr color="blue"></td>
</tr>
<tr>
<td colspan="2">
<table cellpadding="0" cellspacing="0" >
<tr>
<td>
<c:choose >
<c:when test="${sessionScope.user==null}">
<form action="servlet/LoginServlet" method="post">
<table cellpadding="0" cellspacing="0">
<tr>
<td><font size="2">用户名:</font></td>
<td><input type="text" name="userName" maxlength="20"></td>
<td></td>
</tr>
<tr>
<td><font size="2">密码:</font></td>
<td><input type="password" name="pwd" maxlength="10"></td>
<td></td>
</tr>
<tr>
<td ><font size="2">验证码:</font></td>
<td><input type="text" name="code"></td>
<td><img id="rdImg" src="servlet/ImgServlet" border=0 onClick="changeImg();"></img></td>
</tr>
<tr>
<td><input type="submit" value="登陆"></td>
<td><input type="button" value="注册" onclick="forwards()"></td>
<td></td>
</tr>
</table>
</form>
</c:when>
<c:otherwise>
<td>
<table cellpadding="0" cellspacing="0" >
<tr>
<td><font size="2">会员名:</font></td>
<td>${sessionScope.user.userName }</td>
</tr>
<tr>
<td><font size="2">积分:</font></td>
<td>${sessionScope.user.jifen }</td>
</tr>
<tr>
<td colspan="2">注销</td>
</tr>
</table>
</td>
</c:otherwise>
</c:choose>
</td>
<td> </td>
<td valign="top" align="right">
<table cellpadding="0" cellspacing="0" border="1">
<c:forEach items="${sessionScope.messageList}" var="message">
<tr>
<td width="50" align="right">${message.type }</td>
<td width="186" align="center"><a href="servlet/MessageServlet?id=${message.id}">${message.topic}</a></td>
<td width="100" align="right"><fmt:formatDate value="${message.date }" pattern="yyyy年MM月dd日"></fmt:formatDate></td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>