栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

jsp使用filter登录状态验证

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jsp使用filter登录状态验证

 

一, 编写jsp登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



账户名: 密码: <%@ include file="c.jsp" %>
二,编写后端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 {
	//设置编码

	//获取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("");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
 三,配置servlet程序web.xml

哈哈哈上编使用session和cookie做登录状态判断小辉没有配置哈哈 希望大家都能自己配置哦



  jsp-04
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
  	建议填写该配置的类名
  	筛选完整路径(包名+类名)
  
  
  	建议填写该配置的类名
  	/建议填写该配置的类名.do(=可以通过类名+.do来访问页面)
  
四,编写筛选filter登录状态判断 

 小编也同时利用了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("");
		}
		//放行
		arg2.doFilter(arg0, arg1);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		//筛选被创建
	}

}
五,配置filter筛选web.xml 


  jsp-04
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
  	建议填写该配置的类名
  	筛选完整路径(包名+类名)
  
  
  	建议填写该配置的类名
  	/*(/*=输入什么都可以执行筛选)
  

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873806.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号