📄 loginproc.java
字号:
package com.mypack;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mypack.bo.LoginBO;
public class LoginProc extends HttpServlet {
public LoginProc() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得登录表单的输入信息
String inputName = request.getParameter("userName");
System.out.println(inputName);
String inputPassword = request.getParameter("password");
System.out.println(inputPassword);
String inputValidCode = request.getParameter("validCode");
System.out.println(inputValidCode);
// 获得正确的表单中的信息
// 从session中获得验证码
HttpSession session = request.getSession();
String rightValidCode = (String) session.getAttribute("strRand");
System.out.println(rightValidCode);
// 从获得正确的用户名与密码
String rightPassword = "";
String role = "";
ServletContext sc = getServletContext();
HashMap<String, User> map = (HashMap<String, User>) sc
.getAttribute("userInfo");
// 用户名若存在,则取出密码与角色
if (map.containsKey(inputName)) {
User user = (User) map.get(inputName);
rightPassword = user.getPassword();
System.out.println(rightPassword);
role = user.getRole();
System.out.println(role);
}
// 判断密码与验证码是否正确
LoginBO lbo = new LoginBO();
boolean bCheckPassword = lbo.checkUserPassword(rightPassword,
inputPassword);
System.out.println(bCheckPassword);
boolean bCheckCode = lbo.checkValidCode(rightValidCode, inputValidCode);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>后台验证界面</title></head");
out.println("<body>");
if (!bCheckPassword) {
out.println("用户名或密码错误");
} else if (!bCheckCode) {
out.println("验证码错误");
} else {
out.println("登录成功<br>");
// 将用户信息放进session
request.getSession().setAttribute(inputName,
new User(inputName, inputPassword, role));
request.getSession().setAttribute("role", role);
// 动态读取文件列表
File f = new File(sc.getRealPath(sc.getInitParameter("downDirc")));
File[] fList = f.listFiles();
for (int i = 0; i < fList.length; i++) {
String fName = fList[i].getName();
out.print("<a href=DownProc?id=" + fName + ">");
out.print(fName);
System.out.println(fName);
out.print(" </a><br>");
}
out.println("<a href='Logout' >退出</a><br>");
}
out.println("</body>");
out.println("</html>");
session.removeAttribute("strRand");
out.flush();
out.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -