旅游网
内容是基于Java Web的JSP+Cookie+Servlet框架进行开发的小型旅游网站
本小型旅游网站采用JSP+MySQL技术,对数据库进行了操作。在各个页面的跳转,以及页面内部需要进行动态操作的位置,使用了Servlet技术,对数据库进行了操作。在各个页面的跳转,以及页面内部需要进行动态操作的位置,使用了Servlet技术
运用程序服务器使用所层结构:Dao层, Entity层, Service层和 Servlet层
本小型旅游网包含以下几个模块:
一、前台
1.用户注册模块。用户进入注册页面后,将输入账号、用户名、密码等信息,注册之前还需输入验证码方可注册成功,刷新数据库就可以看到注册的新信息
2.用户登录模块。使用已注册的用户名登入系统,在登录界面输入用户名及密码。登录时也有自动登录和验证登录是否重复功能。不进行登录任可浏览网站中大部分信息,但网站中的关键信息无法使用,如购物车不登录便无法将商品加入购物车
//一、如果是已保存到Cookie,则直接读取数据
String loginID = null;
String loginPWD = null;
boolean idbl = false;
boolean pwdbl = false;
//1. 判断是否在Cookie保存过用户信息
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(int i=0; i
if(cookies[i].getName().equals(“loginID”)){
loginID = cookies[i].getValue();
idbl = true;
}
if(cookies[i].getName().equals(“loginPWD”)){
loginPWD = cookies[i].getValue();
pwdbl = true;
}
if(idbl && pwdbl){
break;
}
}
}
//二、否则根据参数读取数据
if(!(idbl && pwdbl)){
request.setCharacterEncoding(“utf-8”);
//1. 得到checkbox的值
String chkSave = request.getParameter(“checkbox”);
loginID = request.getParameter(“loginid”);
loginPWD = request.getParameter(“loginpwd”);
if(chkSave != null){
/* response.getWriter().write(chkSave); /
//2. 如果选中,则把账号和密码保存到Cookie
Cookie loginIDCookie = new Cookie(“loginID”, loginID);
Cookie loginPWDCookie = new Cookie(“loginPWD”, loginPWD);
loginIDCookie.setMaxAge(6060247);
loginPWDCookie.setMaxAge(606024*7);
response.addCookie(loginIDCookie);
response.addCookie(loginPWDCookie);
}
}
//3. 如果是登录成功,则显示账号信息,否则跳转到登录页面
String loginid =customer.getAccount();
Customer customers=new Customer();
customers= userLoginService.login(customer);
if (null!=customers.getState()&&customers.getState().equals("正常")) {
session.setAttribute("userLogin", loginid);
response.sendRedirect("index.jsp");
}else {
session.setAttribute("err", "账号密码错误或被禁用");
response.sendRedirect("login.jsp");
}
String str= “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;
private Random random = new Random();
//随机生成四个字符
public String getStr() {
String s ="";
int len = str.length();
for (int i = 0; i <4; i++) {
s +=str.charAt(random.nextInt(len));
}
return s;
}
//随机生成颜色
public Color getColor(){
int red = random.nextInt(256);
int green = random.nextInt(256);
int block = random.nextInt(256);
Color color = new Color(red,green,block);
return color;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 因为a链接是get请求,所以代码写在doGet方法中
// 画一个面板
BufferedImage image = new BufferedImage(70, 30,
BufferedImage.TYPE_INT_RGB);
// 画笔
Graphics pen = image.getGraphics();
pen.setColor(Color.white);
// 画一个矩形
pen.fillRect(0, 0, 70, 30);
pen.setFont(new Font("微软雅黑", Font.BOLD, 20));
// 把四个字符写到图片中
String msg = getStr();
//将生成的验证码,放入到session范围中
HttpSession session=request.getSession();
session.setAttribute("autoCode", msg);
for (int i = 0; i < 4; i++) {
pen.setColor(getColor());
pen.drawString(msg.charAt(i) + "", i * 15 + 5, 20);
}
// 从response对象中获取到一个输出流,能够将画好的图片传递给客户端
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "png", sos);
sos.flush();
sos.close();
修改用户状态:正常和禁用
int s;
String stateString=request.getParameter(“state”);
int id=Integer.parseInt(request.getParameter(“id”));
UserLoginService userLoginService=new UserLoginService();
if(stateString.equals(“正常”)){
s= userLoginService.updateUserByStop(id);
}else {
s= userLoginService.updateUserByRestar(id);
}
if (s==1) {
response.sendRedirect("SelectUserServlet");
}
商品详情页:所有数据、图片都通过SQL语句从数据库中读取,代码如下:
// 查询最新4个类型
public List ProSelectIndex() {
String sql = “SELECT linetypeID, typename FROM linetype ORDER BY linetypeid DESC LIMIT 4”;
return select(sql);
}
// 查询 6个境外游 显示在左边
public List selectOverseas() {
String sql = "SELECT lineID,lineName FROM line WHERE lineTypeID=7 ORDER BY lineID DESC LIMIT 6";
return selectLeftOne(sql);
}
// 查询 6个海岛游 显示在左边
public List selectIsland() {
String sql = "SELECT lineID, lineName FROM line WHERE lineTypeID=4 ORDER BY lineID DESC LIMIT 6";
return selectLeftOne(sql);
}
// 查询 6个自驾游 显示在左边
public List selectSelfDriving() {
String sql = "SELECT lineID, lineName FROM line WHERE lineTypeID=6 ORDER BY lineID DESC LIMIT 6";
return selectLeftOne(sql);
}
// 查询 6个境内游 显示在左边
public List selectChurchyard() {
String sql = "SELECT lineID, lineName FROM line WHERE lineTypeID=5 ORDER BY lineID DESC LIMIT 6";
return selectLeftOne(sql);
}
// 查询 8个一日游 显示在左边
public List selectOneDayTour() {
String sql = "SELECT lineID, lineName FROM line WHERE days=1 ORDER BY lineID DESC LIMIT 8";
return selectLeftOne(sql);
}
// 查询 8个两日游 显示在左边
public List selectTwoDayTour() {
String sql = "SELECT lineID, lineName FROM line WHERE days=2 ORDER BY lineID DESC LIMIT 8";
return selectLeftOne(sql);
}
// 查询 8个多日游 显示在左边
public List selectmanyDayTour() {
String sql = "SELECT lineID, lineName FROM line WHERE days>2 ORDER BY lineID DESC LIMIT 8";
return selectLeftOne(sql);
}
// 根据线路类型查询新路详细信息 的 前6个
public List ProSelectTypeListDetail(Line line1) {
List list = new ArrayList();
String sql = "SELECT LINE.`lineID`, line.`lineTypeID`, `lineName`, `days`, `price`, `vehicle`, line.`introduction` , picture.`name`,linetype.typename FROM line,picture ,linetype WHERE line.linetypeid = linetype.linetypeid AND line.lineid = picture.lineid AND linetype.linetypeid=? ORDER BY lineID DESC LIMIT 6";
Object[] params = new Object[1];
params[0] = line1.getLineTypeID();
ResultSet rs = DBManage.getResultSet(sql, params);
try {
while (rs.next()) {
Line line = new Line();
line.setLineID(rs.getInt("lineID"));
line.setLineTypeID(rs.getInt("lineTypeID"));
line.setLineName(rs.getString("lineName"));
line.setDays(rs.getInt("days"));
line.setPrice(rs.getDouble("price"));
line.setVehicle(rs.getString("vehicle"));
line.setIntroduction(rs.getString("introduction"));
line.setPicture(rs.getString("name"));
line.setTypeName(rs.getString("typename"));
list.add(line);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
}
4.购物车:添加购物车、删除购物车、读取购物车数据
添加购物车:
// 添加购物车
public int AddCar(Car car) {
String strSql = “INSERT INTO car (customerName
,lineID
)VALUES(?,?)”;
Object[] params = new Object[2];
params[0] = car.getCutsomerName();
params[1] = car.getLineID();
return DBManage.modifyEntiy(strSql, params);
}
Car car = new Car();
LineService lineService = new LineService();
HttpSession session = request.getSession();
String userName = (String) request.getSession().getAttribute(“userLogin”);
car.setCutsomerName(userName);
car.setLineID(Integer.parseInt(request.getParameter(“id”)));
int state = lineService.AddCar(car);
String string = "";
if (state > 0) {
// 输出删除成功,再跳转回list页面对应的Servlet
string = "";
} else {
// 输出删除失败,跳转回list页面对应的Servlet
string = "";
}
response.getWriter().write(string);
删除购物车:
// 删除购物车
public int DeleteCart(int cartid) {
String strSql = “DELETE FROM car WHERE carID =” + cartid;
return DBManage.modifyEntiy(strSql, null);
}
读取购物车数据:
// 读取购物车数据
public List SelectCar(Car car) {
String strSql = "SELECT carID,picture. name
, line.lineID,lineName,days,vehicle,price "
+ "FROM line,car,picture WHERE line.lineID IN(SELECT lineid "
+ "FROM car WHERE customerName = ?) "
+ “and line.lineID=picture.lineID AND line.lineid = car.lineid”;
List list = new ArrayList ();
Object[] params = new Object[1];
params[0] = car.getCutsomerName();
ResultSet rs = DBManage.getResultSet(strSql, params);
try {
while (rs.next()) {
Line line = new Line();
line.setCarID(rs.getInt(“carID”));
line.setPicture(rs.getString(“name”));
line.setLineID(rs.getInt(“lineID”));
line.setLineName(rs.getString(“lineName”));
line.setDays(rs.getInt(“days”));
line.setVehicle(rs.getString(“vehicle”));
line.setPrice(rs.getDouble(“price”));
list.add(line);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
5.订单管理:增加、删除、查询、显示
添加订单在购物车里
OrderDetail orderDetail = new OrderDetail();
UserLoginService userLoginService = new UserLoginService();
HttpSession session = request.getSession();
String oName = (String)request.getSession().getAttribute("userLogin");
orderDetail.setCarID(Integer.parseInt(request.getParameter("ordercarID")));
orderDetail.setOrderDate(request.getParameter("playtime"));
orderDetail.setRealname(request.getParameter("realName"));
orderDetail.setTel(request.getParameter("tel"));
orderDetail.setCustomerName(oName);
int state = userLoginService.AddOrderInfo(orderDetail);
String string = "";
if (state > 0) {
// 添加订单成功
string = "";
}
response.getWriter().write(string);
response.sendRedirect("SelectAllOrderInfo");
}
查询用户信息:
// 根据id查询用户
public List selectUserByID(int customerID) {
String sql = “SELECT customerID,account,name,gender,identityID,tel,state FROM customer WHERE type is NULL and customerID =”
+ customerID;
List list = new ArrayList();
try {
ResultSet rs = DBManage.getResultSet(sql);
while (rs.next()) {
Customer cs = new Customer();
cs.setCustomerID(rs.getInt("customerID"));
cs.setAccount(rs.getString("account"));
cs.setName(rs.getString("name"));
cs.setGender(rs.getString("gender"));
cs.setIdentityID(rs.getString("identityID"));
cs.setTel(rs.getString("tel"));
cs.setState(rs.getString("state"));
list.add(cs);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
修改用户信息:
Customer customer = new Customer();
UserLoginService userLoginService = new UserLoginService();
HttpSession session = request.getSession();
String username = (String) request.getSession().getAttribute(“userLogin”);
customer.setAccount(username);
customer.setName(request.getParameter(“clname”));
customer.setGender(request.getParameter(“radio”));
customer.setTel(request.getParameter(“cltel”));
customer.setPassword(request.getParameter(“clpwd”));
int state =userLoginService.UpdateCustomerInfo(customer);
String string = “”;
if(state>0){
//输出删除成功,再跳转回list页面对应的Servlet
string = “”;
}
response.getWriter().write(string);
2.线路类型管理:增加、删除、修改、查询
3.分页