在线点餐系统
目录
步骤一:搭建开发环境
Tomcat8的安装配置
配置测试结果截图:打开D:\java\TOMCAT8\bin,注意安装的TOMCAT8与jre放在同一文件夹下,先打开tomcat8w,再打开tomcat,再将tomcat8w关闭,运行测试。
步骤二:SQLServer数据库设计
创建数据库WubifenDatabase,里面创建foodInfo、foodOrderInfo、orderInfo、userInfo四张表。.
表头分别(测试数据已存入数据库):
FoodInfo:
FoodOrderInfo:
OrderInfo:
UserInfo:
为了连接方便,我使用SQL身份默认sa用户连接,安全起见,给数据库服务器设置了访问密码:970000
Java中对数据库的连接
代码如下:
package com.sxdx.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDAO {
public static Connection getConn()
{
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost;databasename=WubifenDatabase","sa","970000");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.printf("没连上数据库");
}
return conn;
}
public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs)
{
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null)
{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
步骤三:程序_JavaBean
实体类FoodInfo:
package com.sxdx.vo;
public class FoodInfo {
private String foodID;
private String foodName;
private String remark;
private double foodPrice;
private String description;
private String foodImage;
public String getFoodID() {
return foodID;
}
public void setFoodID(String foodID) {
this.foodID = foodID;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public double getFoodPrice() {
return foodPrice;
}
public void setFoodPrice(double foodPrice) {
this.foodPrice = foodPrice;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFoodImage() {
return foodImage;
}
public void setFoodImage(String foodImage) {
this.foodImage = foodImage;
}
}
实体类FoodOrderInfo:
package com.sxdx.vo;
public class FoodOrderInfo {
private String customerName;
private String address;
private String telephone;
private double totalPrice;
private String state;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public String getstate() {
return state;
}
public void setstate(String state) {
this.state = state;
}
public FoodOrderInfo(String customerName, String address,
String telephone, double totalPrice, String state) {
super();//用super操作被隐藏的成员变量和方法/使用super调用父类的构造方法
this.customerName = customerName;
this.address = address;
this.telephone = telephone;
this.totalPrice = totalPrice;
this.state= state;
}
}
实体类Item:
package com.sxdx.vo;
public class Item {
private FoodInfo foodInfo;
private int count;
public FoodInfo getFoodInfo() {
return foodInfo;
}
public void setFoodInfo(FoodInfo foodInfo) {
this.foodInfo = foodInfo;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
实体类UserInfo:
package com.sxdx.vo;
public class UserInfo {
private String loginName;
private String loginPass;
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPass() {
return loginPass;
}
public void setLoginPass(String loginPass) {
this.loginPass = loginPass;
}
}
步骤四:程序_ Dao
截取部分:顾客/商家
UserInfoDAO:
package com.sxdx.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.Cookie;
public class UserInfoDAO {
//验证用户是否成功登录
public int checkLogin(String username,String userpass)
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
int result=0;
try
{
conn=BaseDAO.getConn();
ps=conn.prepareStatement("select * from userinfo where loginname=? and loginpass=?");
ps.setString(1, username);
ps.setString(2, userpass);
rs=ps.executeQuery();
if(rs.next())
{
String str1="customer";
String str2="business";
if(str1.equals(rs.getString("Permission")))
result=1;
else if(str2.equals(rs.getString("Permission")))
result=2;
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
BaseDAO.closeAll(conn, ps, rs);
}
return result;
}
public boolean insertUser(String username,String userpass) {
Connection conn=null;
PreparedStatement ps=null;
int rs=0;
boolean flag=false;
try {
conn=BaseDAO.getConn();
ps=conn.prepareStatement( "insert into userInfo (loginName,loginPass,Permission) values(?,?,?)");
ps.setString(1, username);
ps.setString(2, userpass);
ps.setString(3, "customer");
rs = ps.executeUpdate();
if(rs==1)
{
flag=true;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return flag;
}
}
步骤五:程序_ Servlet
截取部分:登录
package com.sxdx.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sxdx.dao.UserInfoDAO;
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 {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String userpass=request.getParameter("userpass");
UserInfoDAO userDAO=new UserInfoDAO();
int result=userDAO.checkLogin(username, userpass);
if(result!=0)
{
Cookie cookie_username = new Cookie("username", java.net.URLEncoder.encode(username, "utf-8"));
cookie_username.setMaxAge(60*60);
cookie_username.setPath("/");
response.addCookie(cookie_username);
if(result==1)
{
request.getRequestDispatcher("/foodinfoservlet").forward(request,response);
}
if(result==2)
{
response.sendRedirect("guanli.jsp");
}
}
else
{
PrintWriter out = response.getWriter();
out.print("<script>" + "alert('您输入的账号密码有误');"+ "document.location.href='index.jsp';"+"</script>");
out.close();
}
}
/**
* 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 {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
步骤六:Web层_JSP
截取部分:index
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</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">
-->
</head>
<body style="width:760px; height:700px; text-align: center;">
<div style="width:760px; align="center">
<div style="height:141px;">
<%@ include file="top.jsp" %>
</div>
<div id="centent" style="width:760px;height:100%; position:relative;">
<div style=" width:160px; height:240px; position:absolute; left:0px;top:0px; float:left;">
<%@ include file="left.jsp" %>
</div>
<div style="width:600px;height:111px;float:right;">
<%@ include file="right_top.jsp" %>
</div>
<div style="width:600px; height:240px;float:right;">
<div style="height:20px; background-color:#AAB9BD">
新系统用户请先完成注册
</div>
<form action="/buyEat/loginservlet?op=login" method="post" style="height: 100px; width:300px; ">
<br/>用户名:<input type="text" name="username" style="border:1px solid #666666; size: 17px;" /><br/><br/>
密 码: <input type="password" name="userpass" style="border:1px solid #666666;size: 17px;" /><br/><br/>
<input type="submit" value="登录">
</form>
<input type="button" value="注册" onClick="window.location.href='zhuce.jsp'">
</div></div>
</div>
<div>
<%@ include file="bottom.jsp" %>
</div>
</body>
</html>
实验结果
以用户名wubifen,密码970000完成以下结果测试
效果一: 实现商家和用户两种角色的注册与登录
- 用户注册与登录
主页:
新用户注册:
查询数据库中对应的表:
登录:成功后可以浏览店铺信息
- 商家登录
效果二: 用户名存入cookie并显示在页面上
用户名存入cookie后显示在页面上的效果
效果三: 选某一件菜品,选好之后提交
对应图片点击进去后:
再次点击订购完成提交
效果四: 使用socket通信
完成订单的提交后
使用商家用户登录查看订单
商家点击确认订单
对应看顾客的订单状态:处于confirm被接受状态
附加效果五: 一次提交多件菜品
附加效果六:查看订单历史
源码:
链接:https://pan.baidu.com/s/1QwoEJEB8PPGsXY76ifAYnw 密码:7lsi
总结:
这次实验的课程设计java web项目,使用JSP+Servlet+JavaBean实现MVC模式,使用cookie并把信息显示在页面上,使用socket实现网络通信,进行注册、登录、浏览菜品、下订单、查看订单、查看历史订单、多角色用户登录登操作,遇到了很多困难,抓取了网上数据库数据以及显示时钟时间等功能去美化页面。
第一次尝试完成使用Java语言实现的系统,除了巩固课堂知识,是更多的意识到自身对Java学习的不够。Java是很有用的语言,JSP让我接触到了全新的编程,也让我知道,学无止境。