<form action="<%= request.getContextPath()%>/login?method=login" method="get">
jsp页面采用这个语句 提交表单后method=login无法被servlet中的method.getparameter("method")方法获取到
求解决
这是jsp代码
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="<%= request.getContextPath()%>/login?method=login" method="get">
<input name="username" required="用户名">
<input name="password" required="密码" type="password">
<button type="submit">提交</button>
</form>
<div>${UsernameError}</div>
<div>${PasswordError}</div>
</body>
</html>
这是servlet代码 控制台打印method为空 也就是拿不到method的值
@WebServlet("/login")
public class Adminloginservlet extends HttpServlet {
private AdminserviceImp adminserviceImp = new AdminserviceImp();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
System.out.println(method);
switch (method){
case "login":
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
adminlogindto adminlogindto = this.adminserviceImp.login(username, password);
if (adminlogindto.getCode() == -1){
req.setAttribute("UsernameError","用户名不存在");
req.getRequestDispatcher("login.jsp").forward(req,resp);
} else if (adminlogindto.getCode() == 0) {
req.setAttribute("PasswordError","密码错误");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}else {
req.setAttribute("Success","欢迎进入");
HttpSession session = req.getSession();
session.setAttribute("Admin", adminlogindto.getAdmin());
resp.sendRedirect("admin.jsp");
}
break;
case "logout":
req.getSession().invalidate();
resp.sendRedirect("/login.jsp");
break;
}
}
}