package action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import dao.*;
import bean.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class MainServlet extends HttpServlet {
PageBean pb = null;// 此为分页类
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String oper = request.getParameter("oper");// 连接分发唯一标示
if ("login".equals(oper)) {// 用户登陆
this.login(pb, request, response);
return;
}
if ("register".equals(oper)) {// 新用户注册
this.register(request, response);
return;
}
if ("start".equals(oper)) {// 进入BBS页面
pb = this.start(pb, request, response);
return;
}
if ("addbbs".equals(oper)) {// 添加BBS
this.addbbs(request, response);
return;
}
if ("message".equals(oper)) {// 分页
String ye = request.getParameter("command");
pb = this.message(pb, ye, request, response, "right.jsp");
return;
}
if ("view".equals(oper)) {// 更新左侧BBS标题
String bid = request.getParameter("bid");
pb = this.view(pb, bid, request, response);
return;
}
if ("addmsg".equals(oper)) {// 帖子回复
pb = this.addmsg(pb, request, response);
return;
}
if ("goedit".equals(oper)) {// 跳转到编辑之前的业务处理
this.goedit(request, response);
return;
}
if ("edit".equals(oper)) {// 编辑帖子
pb = this.edit(pb, request, response);
return;
}
if ("dele".equals(oper)) {// 删除帖子
pb = this.dele(pb, request, response);
return;
}
if ("refresh".equals(oper)) {// 刷新
this.login(pb, request, response);
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
// 用户登陆
private void login(PageBean pb, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String name = request.getParameter("uname");
String pass = request.getParameter("upass");
String checkcode = request.getParameter("checkcode");
String rand = (String) session.getAttribute("rand");
ConDB db = new ConDB();
int grade = db.testUser(name, pass);
if (checkcode.equals(rand)) {
if (grade == 1 || grade == 0) {
List tlist = db.view();
session.setAttribute("tlist", tlist);
List ulist=db.showuser();
session.setAttribute("user",ulist);
response.sendRedirect("main.html");
} else {
// !!!!!!!!!!!登陆错误!!!!!!!!!!!!
this.error(request, response, "无此用户或者密码错误", "./index.jsp");
}
} else {
this.error(request, response, "验证码错误", "./index.jsp");
}
db.close();
}
// 新用户注册
private void register(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 得到客户端页面录入信息
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
String uname = request.getParameter("uname").trim();
String upass = request.getParameter("upass").trim();
String checkcode = request.getParameter("checkcode");
int grade=0;
int grade1=1;
String phone=request.getParameter("phone").trim();
String idcard=request.getParameter("idcard").trim();
String rand = (String) session.getAttribute("rand");
ConDB judge = new ConDB();
ConDB add = new ConDB();
// 获得新用户的注册信息
boolean beo= (phone.length() > 0) && (idcard.length() > 0);
boolean boo = (uname.length() > 0) && (upass.length() > 0);
if (checkcode.equals(rand)) {
if(beo==true)
if (judge.judge(uname) && boo == true) {
add.add(uname, upass, grade,phone,idcard,0);
request.getRequestDispatcher("/succeed.jsp").forward(request,
response);
} else {
this.error(request, response, "用户名重复--无法使用", "./register.jsp");
}
else
{
if (judge.judge(uname) && boo == true) {
add.add(uname, upass, grade1,null,null,0);
request.getRequestDispatcher("/succeed.jsp").forward(request,
response);
} else {
this.error(request, response, "用户名重复--无法使用", "./register.jsp");
}
}
} else {
this.error(request, response, "验证码错误", "./register.jsp");
}
}
// 进入BBS页面
private PageBean start(PageBean pb, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ConDB db = new ConDB();
List tlist = db.view();
db.close();
HttpSession session = request.getSession();
session.setAttribute("tlist", tlist);
if (tlist.size() == 0 || tlist == null) {
pb = new PageBean(4, "");
} else {
pb = new PageBean(4, "where bid=" + ((Bbs) tlist.get(0)).getBid());
}
pb = this.message(pb, "frist", request, response, "right.jsp");
return pb;
}
// 添加bbs
private void addbbs(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// ------------业务操作对象的创建------------
User user = (User) session.getAttribute("user");
String title = request.getParameter("title");
String bdetail = request.getParameter("bdetail");
String author = ((User) request.getSession().getAttribute("user"))
.getUname();
int uid=((User) request.getSession().getAttribute("user"))
.getUid();
Timestamp bdate = new Timestamp(new java.util.Date().getTime());
String jinghua=request.getParameter("jinghua");
Bbs bbs = new Bbs(0, title, author, bdate,jinghua);
Bdetail detail = new Bdetail(0, 0, bdetail, user.getUname(), bdate);
// ---------------------------------------
ConDB db = new ConDB();
if (db.addMessge(bbs, detail)) {
session.setAttribute("tlist", db.view());
response.sendRedirect("right.jsp");
} else {
// !!!!!!!!!!!!发帖错误!!!!!!!!!!!
this.error(request, response, "系统错误--发帖发生错误", "./right.jsp");
}
db.close();
}
// 分页
private PageBean message(PageBean pb, String fenye,
HttpServletRequest request, HttpServletResponse response,
String path) throws ServletException, IOException {
HttpSession session = request.getSession();
// ------------------分页业务处理-------------------
int num = pb.getNum();
int ye = pb.getYe();
int yeshu = pb.getYeShu();
Message mess = null;
if ("frist".equals(fenye)) {
List list = this.isBoos(pb, session);
mess = new Message(num, ye, yeshu, list, pb.isBool());
} else if ("next".equals(fenye)) {
mess = new Message(num, ye, yeshu, pb.xiaYiYe(), pb.isBool());
} else if ("back".equals(fenye)) {
mess = new Message(num, ye, yeshu, pb.shangYiYe(), pb.isBool());
} else if ("last".equals(fenye)) {
mess = new Message(num, ye, yeshu, pb.weiYe(), pb.isBool());
}
// ----------分楼层----------
mess.setStart(pb.getStart() + 1);
// ------------------------------------------------
session.setAttribute("messages", mess);
System.out.print("grade");
response.sendRedirect(path);
return pb;
}
// 分页转换bbs
private PageBean view(PageBean pb, String id, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
pb = new PageBean(4, "where bid=" + id);
pb = this.message(pb, "frist", request, response, "right.jsp");
return pb;
}
// 回帖
private PageBean addmsg(PageBean pb, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// ------------业务操作对象的创建-- ----------
String bid = request.getParameter("bid");
String resp = reque