一, 编写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="logonServlet.do" method="post">
<div>
账户名:<input type='text' name="oname"/>
</div>
<div>
密码:<input type="password" name="opwd"/>
</div>
<button>登录</button>
<!--引入版权 -->
<%@ include file="c.jsp" %>
</form>
</body>
</html>
二,编写后端servlet登录程序
package com.jmh.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
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.jmh.dao.Tb_LogonDao;
import com.jmh.entity.Tb_Logon;
/**
* 登录
*
*/
public class LogonServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
/* req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");*/
//获取out、session对象
PrintWriter out = resp.getWriter();
HttpSession session = req.getSession();
//处理业务逻辑代码
//获取传入过来的值
String oname = req.getParameter("oname");
String opwd = req.getParameter("opwd");
//实例化实体类
Tb_Logon lo=new Tb_Logon(oname, opwd);
//实例化方法类
Tb_LogonDao tbl=new Tb_LogonDao();
try {
boolean logon = tbl.Logon(lo);
if(logon) {//登录成功
//使用session
//登录状态:登录成功后把用户名传入给session作用域
session.setAttribute("oname", oname);
req.getRequestDispatcher("跳转登录成功后的页面").forward(req, resp);
}else {//登录失败
out.print("<script>alert('登录失败!');location.href='跳转登录页面'</script>");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
三,配置servlet程序web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>jsp-04</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--登录 -->
<servlet>
<servlet-name>建议填写该配置的类名</servlet-name>
<servlet-class>筛选完整路径(包名+类名)</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>建议填写该配置的类名</servlet-name>
<url-pattern>/建议填写该配置的类名.do(=可以通过类名+.do来访问页面)</url-pattern>
</servlet-mapping>
</web-app>
四,编写筛选filter登录状态判断
package com.jmh.filter;
import java.io.IOException;
import java.io.PrintWriter;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 筛选
*
*/
public class SetEcodingLogon implements Filter{
@Override
public void destroy() {
//筛选被摧毁
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
//筛选器被执行/处理
//设置编码
HttpServletRequest req=(HttpServletRequest)arg0;
HttpServletResponse resp=(HttpServletResponse)arg1;
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取out对象
PrintWriter out = resp.getWriter();
//获取session对象
HttpSession session = req.getSession();
//获取请求地址
String uri = req.getRequestURI();
if(uri.indexOf("logon.jsp")>-1||uri.indexOf("logonServlet.do")>-1) {
//如果是登录页面或者登录servlet就放行
arg2.doFilter(arg0, arg1);
return;
}
//获取session里面的值
String s = (String)session.getAttribute("oname");
//如果跳转指定页面判断session里面没有值的话就跳转登录界面
if(null == s) {
//使用js方式跳转页面登录页面
out.print("<script>alert('请先登录');location.href='跳转登录页面';</script>");
}
//放行
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
//筛选被创建
}
}
五,配置filter筛选web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>jsp-04</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置筛选 -->
<filter>
<filter-name>建议填写该配置的类名</filter-name>
<filter-class>筛选完整路径(包名+类名)</filter-class>
</filter>
<filter-mapping>
<filter-name>建议填写该配置的类名</filter-name>
<url-pattern>/*(/*=输入什么都可以执行筛选)</url-pattern>
</filter-mapping>
</web-app>