package 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 entity.MsgUserInfo;
import service.impl.MsgUserInfoServiceDaoImpl;
public class MsgUserInfoServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public MsgUserInfoServlet() {
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 {
request.setCharacterEncoding("UTF-8");
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;charset=UTF-8");
PrintWriter out = response.getWriter();
//获取操作类型
String opr=request.getParameter("opr");
//创建MsgUserInfoServiceDaoImpl对象
MsgUserInfoServiceDaoImpl msgInfosdi=new MsgUserInfoServiceDaoImpl();
if(opr.equals("login")){
//获取用户名和密码
String username=request.getParameter("username");
String password=request.getParameter("password");
//非空验证,防止js禁用
if(username==null||username.trim().equals("")||password==null||password.trim().equals("")){
request.setAttribute("error", "用户名和密码不能为空");
request.getRequestDispatcher("../index.jsp").forward(request,response);
return;
}
//将数据传递到数据库中进行检索
MsgUserInfo mui=msgInfosdi.login(username, password);
//处理检索结果,有数据登录成功,没数据则登录失败
if(mui==null){
request.setAttribute("error", "用户名或密码错误");
request.getRequestDispatcher("../index.jsp").forward(request,response);
}else{
//登录成功
request.getSession().setAttribute("user",mui );
//查询该用户的所有短信息,跳转到查询短信息的处理Servlet
response.sendRedirect("MsgServlet?opr=list");
//response.sendRedirect("../main.jsp");
}
}else if(opr.equals("loginout")){
//实现退出功能
request.getSession().removeAttribute("user");
response.sendRedirect("../index.jsp");
}else if(opr.equals("reg")){
//实现注册功能
//获取注册输入的信息
String username=request.getParameter("username");
String password=request.getParameter("password");
String email=request.getParameter("email");
String rePassword=request.getParameter("affirm");
//做输入信息验证
if(username==null||username.trim().equals("")||password==null||password.trim().equals("")||email==null||email.trim().equals("")||rePassword==null||rePassword.trim().equals("")){
request.setAttribute("error", "请将信息填写完整,不能为空");
request.getRequestDispatcher("../regist.jsp").forward(request,response);
return;
}
//将信息的数据保存到数据库
MsgUserInfo userInfo=new MsgUserInfo(username,password,email);
int isRegist=msgInfosdi.regist(userInfo);
//保存结果进行处理
if(isRegist==0){
//注册成功
request.getSession().setAttribute("error", "已经注册成功");
response.sendRedirect("../index.jsp");
}else if(isRegist==1){
//注册失败
request.setAttribute("msg", "注册失败");
request.getRequestDispatcher("../regist.jsp").forward(request, response);
}else if(isRegist==-1){
//用户已经存在
request.setAttribute("msg", "用户已经存在");
request.getRequestDispatcher("../regist.jsp").forward(request, response);
}
}else if(opr.equals("findUsers")){
//将用出当前用户所有信息全部查询并显示
//获得当前登录用户
MsgUserInfo uInfo=(MsgUserInfo)request.getSession().getAttribute("user");
//到数据库查询用户信息
List<MsgUserInfo> toUser=msgInfosdi.findUser(uInfo.getUsername());
request.setAttribute("toUser", toUser);
request.getRequestDispatcher("../send.jsp").forward(request, response);
}
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
论坛短信息
需积分: 9 115 浏览量
2017-12-05
14:33:05
上传
评论 1
收藏 970KB RAR 举报
qq_40285032
- 粉丝: 0
- 资源: 3
最新资源
- MI4803-VB一款SOP8封装2个P-Channel场效应MOS管
- unity快速入门-最快速的入门
- MI4803A-VB一款SOP8封装2个P-Channel场效应MOS管
- MI4801-VB一款SOP8封装2个P-Channel场效应MOS管
- 多变量线性回归的简单表示机代码实现
- 羊驼家族Ollama,window版本,本地大模型
- Java面试题+Java并发编程(J.U.C)+Java8实战+Redis+kafka
- 源码工程文件-libmodbus-test
- wx036校园快递平台系统-springboot+vue+uniapp-小程序.zip(可运行源码+sql文件+文档)
- Kotlin基础.pdf(39页,包含基础篇和进阶篇)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈