1.登陆页面
[sourcecode language=”html” title=”login.jsp”]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginCheckServlet" method="post">
请输入用户名:<input type="text" name="username"/><br/>
请输入密码:<input type="password" name="userpwd"/><br/>
<input type="submit" value="登录"/>
<input type="reset"/>
</form>
</body>
</html>
[/sourcecode]
2.登陆验证
[sourcecode language=”html” title=”com.servlet/LoginCheckServlet”]</pre>
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 登陆验证
*/
@WebServlet("/LoginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("username"); //获取表单传来的用户名
String pwd=request.getParameter("userpwd"); //获取表单传来的密码
String into="";
//验证
if(name.equals("abc") && pwd.equals("123"))
{
into=name +" 用户 登陆成功,欢迎光临!";
HttpSession session = request.getSession();
// 2.将用户名保存在session中
session.setAttribute("u_name", name);
}
else
{
into=name +" 用户名 登陆失败!请注册";
}
//将into设置 为request范围内对象的属性
request.setAttribute("showinto", into);
//将into设置 为session范围内对象的属性
//跳转到显示页面
request.getRequestDispatcher("yanz/demo_show.jsp").forward(request, response); //转向
//response.sendRedirect("demo_show.jsp");//重定向
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
[/sourcecode]
2.过滤器页面
[sourcecode language=”java” title=”com.filter/LoginFilter.java”]
</pre></pre>
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet Filter implementation class LoginFilter
*/
@WebFilter("/loginFilter")
public class LoginFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain) throws IOException, ServletException {
HttpServletRequest requ = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
HttpSession session = requ.getSession(true);
if(session.getAttribute("u_name")==null)
{
resp.sendRedirect("/benzhu/login.jsp");}
else{filterchain.doFilter(request, response);}
}
public void init(FilterConfig arg0) throws ServletException {
}
}
[/sourcecode]
3.登陆成功页面
[sourcecode language=”html” title=”yanz/demo_show.jsp”]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示结果页面</title>
</head>
<body>
<%=request.getAttribute("showinto") %>
</body>
</html>
[/sourcecode]
4.配置xml
[sourcecode language=”xml” title=”web.xml”]
<filter>
<filter-name>loginfilter</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginfilter</filter-name>
<url-pattern>/yanz/*</url-pattern>
</filter-mapping>
[/sourcecode]