写一个登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>登录</h1>
<form action="login" method="post">
username:<input type="text" name="username"><br />
password:<input type="password" name="password"><br />
<input type="submit" value="登录">
</form>
</body>
</html>
然后在web.xml中添加(这个是设置启动页面的)
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
添加两个包和两个类
Account代码如下(idea alt + insert 可以快速添加get和set)
public class Account {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
CheckAc代码如下
public class CheckAc extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
Account account = new Account();
account.setUsername(username);
account.setPassword(password);
HttpSession session = request.getSession();
//trim去掉开头和结尾的空格
if (username.trim().equals("admin")&&password.trim().equals("admin")){
session.setAttribute("account",account);
//sendRedirect页面跳转到指定页面
response.sendRedirect("success.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
然后是成功页面(要导入Account类哦)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.model.Account" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<%
Account account = (Account) session.getAttribute("account");
%>
<%=account.getUsername()%>
恭喜你登录成功!
</body>
</html>
最后就可以了