基于javaweb+mysql的jsp+servlet火车票网上订票系统(前台、后台)(java+jsp+servlet+javabean+mysql+tomcat)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
前台功能:用户查询车票信息、登录注册、购票、查看已购车票、修改密码等功能。
后台功能:管理员管理、用户管理、火车信息管理、站点信息管理、线路信息管理、车票信息管理、购票信息管理等。
后台:
前台
技术框架
JSP Servlet MySQL JDBC Tomcat CSS JavaScript
基于javaweb+mysql的JSP+Servlet火车票网上订票系统(前台、后台)(java+jsp+servlet+javabean+mysql+tomcat)
json.put("msg", "车次已注册");
}else{
int bls=db.deleteOrUpdate("update traininfo set tname='"+tnames+"', cz='"+cz+"' ,traintype='"+traintype+"',znums="+znums+",xcnums="+xcnums+" where id="+ids);
if(bls>0){
json.put("msg", "修改成功。");
}else{
json.put("msg", "修改失败");
}
}
}else{
json.put("msg", "操作错误01");
}
}
//站点信息修改
if("zhandian".equals(tname)){
String zdname=request.getParameter("zdname");
String ids=request.getParameter("idi");
ResultSet rs=db.query("select id from zhandian where zdname='"+zdname+"'");
if(rs!=null){
boolean bl=false;
int id=-1;
try {
while(rs.next()){
id=rs.getInt("id");
if(!String.valueOf(id).equals(ids)){
bl=true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bl){
json.put("msg", "站点名称已存在");
}else{
int bls=db.deleteOrUpdate("update zhandian set zdname='"+zdname+"' where id="+ids);
if(bls>0){
json.put("msg", "修改成功。");
}else{
json.put("msg", "修改失败");
}
}
}else{
json.put("msg", "操作错误01");
}
}
//车票信息修改
if("piao".equals(tname)){
String tid=request.getParameter("tid");
String szdid=request.getParameter("szdid");
String ezdid=request.getParameter("ezdid");
String stime=request.getParameter("stime");
String pval=request.getParameter("pval");
//获取前台传送的变量,账号,密码和用户类型
String uname=request.getParameter("uname");
String upassword=request.getParameter("upassword");
String utype=request.getParameter("utype");
//创建数据库操作对象
DbConn db=new DbConn();
//sql语句变量定义
String sql="";
int id=-1;
String usname="";
if("gly".equals(utype)){
sql="select id from userinfo where uname='"+uname+"' and upassword='"+upassword+"'";
}else if("cus".equals(utype)){
sql="select id,cname from cusinfo where cidcard='"+uname+"' and cpassword='"+upassword+"'";
}
JSONObject json=new JSONObject();
if("".equals(sql)){
json.put("msg", "系统错误01.");
}else{
ResultSet rs=db.query(sql);
if(rs==null){
json.put("msg", "系统错误02");
}else{
try {
while(rs.next()){
id=rs.getInt("id");
if("cus".equals(utype)){
usname=rs.getString("cname");
}else if("gly".equals(utype)){
usname=uname;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(id!=-1){
HttpSession session=request.getSession();
json.put("msg", 1);
session.setAttribute("username", usname);
session.setAttribute("userid", id);
session.setAttribute("utype", utype);
}else{
json.put("msg", "账号或密码错误。");
json.put("msg", "请重新登陆。");
return;
}
//获取前台数据tname
String tname=request.getParameter("tname");
//创建数据库操作对象
DbConn db=new DbConn();
//管理员信息添加
if("admin".equals(tname)){
String un=request.getParameter("uname");
String up=request.getParameter("upassword");
boolean bl=db.checkTrue("select id from userinfo where uname='"+un+"'");
if(bl){
json.put("msg", "账号已存在");
}else{
bl=db.insort("insert into userinfo(uname,upassword) values('"+un+"','"+up+"')");
if(!bl){
json.put("msg", "添加成功。");
}else{
json.put("msg", "添加失败");
}
}
}
//顾客信息添加
if("cusinfo".equals(tname)){
String un=request.getParameter("cidcard");
String up=request.getParameter("cpassword");
String um=request.getParameter("cname");
boolean bl=db.checkTrue("select id from cusinfo where cidcard='"+un+"'");
if(bl){
json.put("msg", "身份证已注册");
}else{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
bl=db.insort("insert into cusinfo(cidcard,cpassword,cname,intime) values('"+un+"','"+up+"','"+um+"','"+sdf.format(new Date())+"')");
if(!bl){
json.put("msg", "添加成功。");
}else{
json.put("msg", "添加失败");
}
}
}
//火车信息添加
if("traininfo".equals(tname)){
String tnames=request.getParameter("tnames");
String cz=request.getParameter("cz");
String znums=request.getParameter("znums");
String xcnums=request.getParameter("xcnums");
String traintype=request.getParameter("traintype");
boolean bl=db.checkTrue("select id from traininfo where tname='"+tnames+"'");
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
public class Add extends HttpServlet {
/**
* Constructor of the object.
*/
public Add() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public class HandlePiaoSvlt extends HttpServlet {
/**
* Constructor of the object.
*/
public HandlePiaoSvlt() {
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
Object userid=session.getAttribute("userid");
//创建json对象
JSONObject json=new JSONObject();
if(utype==null||userid==null){
json.put("msg", "请重新登陆。");
return;
}
//获取前台数据tname
String tname=request.getParameter("tname");
//创建数据库操作对象
DbConn db=new DbConn();
//管理员信息修改
if("admin".equals(tname)){
String un=request.getParameter("uname");
String up=request.getParameter("upassword");
String ids=request.getParameter("idi");
ResultSet rs=db.query("select id from userinfo where uname='"+un+"'");
if(rs!=null){
boolean bl=false;
int id=-1;
try {
while(rs.next()){
id=rs.getInt("id");
if(!String.valueOf(id).equals(ids)){
bl=true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bl){
json.put("msg", "账号已存在");
}else{
int bls=db.deleteOrUpdate("update userinfo set uname='"+un+"' ,upassword='"+up+"' where id="+ids);
if(bls>0){
json.put("msg", "修改成功。");
}else{
json.put("msg", "修改失败");
}
}
}else{
json.put("msg", "操作错误01");
* 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 {
//设置传输数据编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
HttpSession session=request.getSession();
Object userid=session.getAttribute("userid");
if(userid==null){
request.getRequestDispatcher("/CusCkSvlt").forward(request, response);
return;
}else{
DbConn db=new DbConn();
ResultSet rs=db.query("select dpiao.*,cxzh,ezdid,ezdname,ptype,pval,stime,szdid,szdname,tid,tname,traintype,yxsj from(select tmp.*,zdname as ezdname from(select piao.*,zdname as szdname,tname,traintype from piao,zhandian,traininfo where piao.tid=traininfo.id and piao.szdid=zhandian.id)tmp,zhandian where tmp.ezdid=zhandian.id)tm,dpiao,cusinfo where tm.id=dpiao.pid and dpiao.cidcard=cusinfo.cidcard and cusinfo.id="+userid);
List<Dpiao> dplist=FindService.getDpiao(rs);
request.setAttribute("alist", dplist);
request.getRequestDispatcher("/customer/main.jsp").forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
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("utf-8");
response.setCharacterEncoding("utf-8");
//获取前台传送的变量,账号,密码和用户类型
String uname=request.getParameter("uname");
String upassword=request.getParameter("upassword");
String utype=request.getParameter("utype");
//创建数据库操作对象
DbConn db=new DbConn();
//sql语句变量定义
String sql="";
int id=-1;
throws ServletException, IOException {
//设置传输数据编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
HttpSession session=request.getSession();
Object userid=session.getAttribute("userid");
if(userid==null){
request.getRequestDispatcher("/CusCkSvlt").forward(request, response);
return;
}else{
DbConn db=new DbConn();
ResultSet rs=db.query("select dpiao.*,cxzh,ezdid,ezdname,ptype,pval,stime,szdid,szdname,tid,tname,traintype,yxsj from(select tmp.*,zdname as ezdname from(select piao.*,zdname as szdname,tname,traintype from piao,zhandian,traininfo where piao.tid=traininfo.id and piao.szdid=zhandian.id)tmp,zhandian where tmp.ezdid=zhandian.id)tm,dpiao,cusinfo where tm.id=dpiao.pid and dpiao.cidcard=cusinfo.cidcard and cusinfo.id="+userid);
List<Dpiao> dplist=FindService.getDpiao(rs);
request.setAttribute("alist", dplist);
request.getRequestDispatcher("/customer/main.jsp").forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
public class DpSvlt extends HttpServlet {
/**
* Constructor of the object.
*/
public DpSvlt() {
super();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
HttpSession session=request.getSession();
//获取使用者信息
Object userid=session.getAttribute("userid");
Object utype=session.getAttribute("utype");
//创建json对象
JSONObject json=new JSONObject();
String sql=request.getParameter("sql");
if(userid==null||utype==null){
json.put("msg", "请重新登陆");
}else{
HandleProperties hp=new HandleProperties(session,utype+""+userid);
hp.setValue("sql", sql);
json.put("msg", 1);
}
PrintWriter out=response.getWriter();
out.print(json.toString());
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
public class LogoutSvlt extends HttpServlet {
/**
* Constructor of the object.
*/
public LogoutSvlt() {
super();
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 {
//设置传输数据编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String tid=request.getParameter("tid");
JSONObject json=new JSONObject();
if(!"".equals(tid)&&tid!=null){
DbConn db=new DbConn();
ResultSet rs=db.query("select zhandian.* from xianlu,zhandian where xianlu.zdid=zhandian.id and xianlu.tid="+tid);
List<Zhandian> zdlist=FindService.getZhandian(rs);
json.put("msg", 1);
json.put("zdlist", zdlist);
}else{
json.put("msg", 0);
}
PrintWriter out=response.getWriter();
out.print(json.toString());
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
}
PrintWriter out=response.getWriter();
out.print(json.toString());
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
public class Add extends HttpServlet {
/**
* Constructor of the object.
*/
public Add() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
}
public class CusCkSvlt extends HttpServlet {
/**
* Constructor of the object.
*/
public CusCkSvlt() {
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.
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String id=request.getParameter("idi");
String flgs=request.getParameter("flgs");
JSONObject json=new JSONObject();
DbConn db=new DbConn();
if("fk".equals(flgs)){
int bls=db.deleteOrUpdate("update dpiao set lyflag='已付款' where id="+id);
if(bls>0){
json.put("dt", 1);
json.put("msg", "付款成功。");
}else{
json.put("dt", 0);
json.put("msg", "fkerror:01");
}
}else if("gq".equals(flgs)){//改签
int bls=db.deleteOrUpdate("delete from dpiao where id="+id);
if(bls>0){
json.put("dt", 2);
json.put("msg", "改签成功,请重新选票订票。");
}else{
json.put("dt", 0);
json.put("msg", "fkerror:01");
}
}else if("lp".equals(flgs)){
int bls=db.deleteOrUpdate("update dpiao set lyflag='已领票' where id="+id);
if(bls>0){
json.put("dt", 1);
json.put("msg", "领票成功。");
}else{
json.put("dt", 0);
json.put("msg", "lperror:01");
}
}else if("tp".equals(flgs)){
int bls=db.deleteOrUpdate("delete from dpiao where id="+id);
if(bls>0){
json.put("dt", 1);
json.put("msg", "退票成功。");
}else{
json.put("dt", 0);
sql="select * from zhandian";
hp.setValue("sql", sql);
}else{
sql=hp.readValue("sql");
}
ResultSet rs=db.query(sql);
List<Zhandian> alist=FindService.getZhandian(rs);
request.setAttribute("alist", alist);
request.getRequestDispatcher("/admin/mgzhandian.jsp").forward(request, response);
return;
}
//线路信息初始化
if("initxl".equals(tname)){
if("1".equals(flgs)){
sql="select * from traininfo";
hp.setValue("sql", sql);
}else{
sql=hp.readValue("sql");
}
List<Xianlu> xllist=new ArrayList();
DbConn db2=new DbConn();
ResultSet rs=db.query(sql);
List<Traininfo> tlist=FindService.getTraininfo(rs);
Iterator<Traininfo> it=tlist.iterator();
while(it.hasNext()){
Traininfo t=it.next();
Xianlu xl=new Xianlu();
xl.setTid(t.getId());
xl.setTname(t.getTname());
ResultSet rs2=db2.query("select zhandian.*,xh from xianlu,zhandian where xianlu.zdid=zhandian.id and xianlu.tid="+t.getId()+" order by xh");
List<Zhandian> zdlist=FindService.getZhandian(rs2);
xl.setZdlist(zdlist);
xllist.add(xl);
}
request.setAttribute("alist", xllist);
request.getRequestDispatcher("/admin/initxianlu.jsp").forward(request, response);
return;
}
//线路信息管理初始化
if("xianlu".equals(tname)){
String ids=request.getParameter("tid");
if("1".equals(flgs)){
sql="select zhandian.*,xh from zhandian,xianlu where zhandian.id=xianlu.zdid and xianlu.tid="+ids+" order by xh";
hp.setValue("sql", sql);
}else{
sql=hp.readValue("sql");
}
ResultSet rs=db.query(sql);
List<Zhandian> zlist=FindService.getZhandians(rs);
request.setAttribute("alist", zlist);
DbConn db2=new DbConn();
ResultSet rs2=db2.query("select * from zhandian where id not in(select zdid from xianlu where tid="+ids+")");
int bls=db.deleteOrUpdate(sql);
if(bls>0){
json.put("msg", "修改成功。");
}else{
json.put("msg", "修改失败。");
}
}else{
json.put("msg", "旧密码输入有误");
}
}
PrintWriter out = response.getWriter();
out.print(json.toString());
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
public class UpdSvlt extends HttpServlet {
/**
* Constructor of the object.
*/
public UpdSvlt() {
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