login.jsp
<body>
<!-- 登录页面 -->
<form action="loginServlet" method = "post">
<input name = "username" placeholder = "请输入用户名.." />
<input name = "password" placeholder = "请输入密码.." />
<input type = "submit" value = "提交" />
</form>
<%= request.getAttribute("msg") == null ? "": request.getAttribute("msg") %>
</body>
index.jsp 登陆成功后进入的页面
<body>
<h1>欢迎登陆</h1>
</body>
loginSeverlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("username");
String password = request.getParameter("password");
if( "admin".equals(user)&&"321".equals(password)) { //如果用户名和密码
request.getSession().setAttribute("currentuser", user);
response.sendRedirect("index.jsp");
} else {
request.setAttribute("msg", "error");
request.getRequestDispatcher("login.jsp").forward(request, response); //转发
}
}
Sessionfilter
public class Sessionfilter implements Filter {
private String[] pages;
@Override
public void doFilter(ServletRequest req, ServletResponse resp , FilterChain chain)
throws IOException, ServletException {
//ServletRequest 是serclet 里面的HttpServletRequest的父类
}
@Override
public void init(FilterConfig config) throws ServletException {
String mis = config.getInitParameter("misspages"); //从web.xml里面获取的 <param-name>
String [] ms = mis.split(";"); //把其转换成数组
}
public String[] getPages() {
return pages;
}
public void setPages(String[] pages) {
this.pages = pages;
}
}