📄 userservlet.java
字号:
package com.newsIssueSystem.servlet;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.print.attribute.ResolutionSyntax;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.newsIssueSystem.javaBean.User;
import com.newsIssueSystem.javaBean.Role;
public class UserServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UserServlet() {
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 {
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");
String loginName = request.getParameter("txtUserName");
String pwd = request.getParameter("txtPassword");
int role = Integer.parseInt(request.getParameter("radiobutton"));
HttpSession session=request.getSession();
User user = new User();
System.out.println("登陆状态");
boolean isLoginSuccess = user.isLogin(loginName, pwd, role);
System.out.println(isLoginSuccess);
session.setAttribute("isLoginSuccess", isLoginSuccess);
if (isLoginSuccess) {
if (request.getParameter("checkusername") != null) {
System.out.println("已经选择了 记住 用户名");
String isRemenberUserName = request
.getParameter("checkusername");
System.out.println(isRemenberUserName);
if (isRemenberUserName.equals("1")) {
Cookie cookie = new Cookie("cookieusername", loginName);
cookie.setMaxAge(60*1);
response.addCookie(cookie);
}
}
List lstuser = user.getLoginuser(loginName);
User usertest = (User) lstuser.get(0);
String username = usertest.getUserName();
String birthday = usertest.getBirthday();
int roleid = usertest.getRoleid();
int sex = usertest.getSex();
session.setAttribute("username", username);
session.setAttribute("birthday", birthday);
session.setAttribute("roleid", roleid);
session.setAttribute("sex", sex);
session.setAttribute("loginName", loginName);
session.setAttribute("loginTime", new Date().toLocaleString());
System.out.println(username);
System.out.println(birthday);
System.out.println(roleid);
System.out.println(sex);
System.out.println(loginName);
RequestDispatcher rd = request.getRequestDispatcher("UserInfo.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd = request.getRequestDispatcher("left.jsp");
rd.forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -