You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hotelbook-JavaWeb-master/src/main/java/com/inks/hb/login/controller/QueryLoginNameServlet.java

76 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.inks.hb.login.controller;
import com.google.gson.Gson;
import com.inks.hb.common.MD5;
import com.inks.hb.logInfo.pojo.LogInfo;
import com.inks.hb.logInfo.service.LogInfoService;
import com.inks.hb.logInfo.service.LogInfoServiceImpl;
import com.inks.hb.login.pojo.Login;
import com.inks.hb.login.service.LoginService;
import com.inks.hb.login.service.LoginServiceImpl;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
/**
* 此servlet是登录界面使用的根据用户登录名和用户密码进行登录判断。
* 如果登录结果判断成功就在session中写入当前的登录名值
* 通过ajax返回给判断的结果。
*/
@WebServlet(value = "/QueryLoginNameServlet", name = "QueryLoginNameServlet")
public class QueryLoginNameServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
// 创建登录服务对象
LoginService service = new LoginServiceImpl();
// 创建MD5对象
MD5 md5 = new MD5();
// 获取用户登录名和密码
String loginName = request.getParameter("loginName");
String loginPwd = md5.getMD5(request.getParameter("loginPwd")); //转成MD5存储
try {
// 根据用户登录名和密码查询登录结果
int check = service.queryByName(loginName, loginPwd);
if (check == 1) { // 设置session
// 获取session对象
HttpSession session = request.getSession();
// 将登录名存入session
session.setAttribute("LoginName", loginName);
// 根据登录名查询登录信息
Login login = service.queryLogin(loginName);
// 创建日志信息对象
LogInfo logInfo = new LogInfo("登录", login.getLoginId(), login.getLoginName());
// 创建日志服务对象
LogInfoService logInfoService = new LogInfoServiceImpl();
// 插入日志信息
logInfoService.insertLogInfo(logInfo);
}
// 创建Gson对象
Gson gson = new Gson();
// 将登录结果以json格式返回
out.print(gson.toJson(check));
} catch (SQLException e) {
e.printStackTrace();
}
}
}