📄 loginservlet.java
字号:
package com.yiboit.cfss.login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
/**
* 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 {
String username = request.getParameter("username");
String password = request.getParameter("password");
String errmsg = "";
// 服务器端验证
if(username == null || "".equals(username)) {
errmsg = "User name is required.\n";
}
if (password == null || "".equals(password)) {
errmsg += "Password is required.\n";
}
if (username != null && username.length() > 30) {
errmsg += "User name is too long.\n";
}
if (password != null && password.length() > 30) {
errmsg += "Password is too long.\n";
}
HttpSession session = request.getSession(true);
if (!"".equals(errmsg)) {
session.setAttribute("errmsg", errmsg);
response.sendRedirect("index.jsp");
return;
}
LoginDAO dao = new LoginDAO();
try {
ShopUser user = dao.getShopUserById(username, password);
if (user == null) {
// 用户名或密码错误
session.setAttribute("errmsg", "User name or password is invalid.");
response.sendRedirect("index.jsp");
} else {
// 登陆成功
String flag = user.getFlag();
session.setAttribute("logonUsername", username);
session.setAttribute("logonPassword", password);
session.setAttribute("logonFlag", flag);
response.sendRedirect(flag + "/description.jsp");
}
} catch (Exception e) {
// 系统错误
e.printStackTrace();
response.sendRedirect(request.getContextPath() + "/error.jsp");
}
}
/**
* 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 {
this.doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -