本文主要是介绍Javaweb中过滤器的使用,并实现留言板的功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前端编写:index.jsp message.jsp
index.jsp编写:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理界面</title>
<link href="form.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<%String flag = request.getParameter("flag");
%><script type="text/javascript">var flag = <%= flag%>;if(flag == "404"){alert("请先登录后操作!");}</script><form action="login.jsp" method="post" class="smart-green"><h1>系统管理</h1><label><span>用户名:</span><input type="text" placeholder="请输入用户名" name="username"/></label><label><span>密码:</span><input type="password" placeholder="请输入密码" name="password"/></label><input value="点击登录" type="submit" /></form></body>
</html>
基础验证这边不实现验证功能,只判断是否为空:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%//进行账号密码的验证String username = request.getParameter("username");String password = request.getParameter("password");request.getSession().setAttribute("loginName", username);response.sendRedirect(request.getContextPath()+"/message.jsp");//进行页面的跳转,request.getContextPath()获取本地地址;
%>
message.jsp编写:
<%@page import="model.message"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言板使用</title>
<link href="form.css" rel="stylesheet" type="text/css" />
<link href="table.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<%String userName = (String)session.getAttribute("loginName");String subFlag = request.getParameter("subFlag");List<message> messages = (List<message>)session.getAttribute("message");
%>
<script type="text/javascript">var flag = '<%= subFlag%>';if(flag == "1"){alert("留言成功!");}
</script><form action="/FilterDemo/messageServlet" method="get" class="smart-green"><h1>留言板的编写</h1><label><span>留言人:</span><input type="text" placeholder="请输入标题" value="<%=userName %>" name="name"/></label><label><span>标题:</span><input type="text" placeholder="请输入标题" name="title"/></label><label><span>内容:</span><textarea name="content"></textarea></label><input type="submit" value="提交留言"/></form><br/><table id="table-3" width="85%" align="center"><tr><th width="15%">留言人</th><th width="15%">标题</th><th width="70%">内容</th></tr><%if(messages != null){for(message ms : messages){%><tr><td><%=userName %></td><td><%=ms.getTitle() %></td><td><%=ms.getContent() %></td></tr><%}}%></table>
</body>
</html>
模块化数据,进行调用:
package model;public class message {//用来封装数据private String title;private String content;public message(String title, String content) {super();this.title = title;this.content = content;}public message() {}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}
留言板进行基本的数据处理:
package cn.java.servlet;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;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 model.message;/*** Servlet implementation class messageServlet*/
@WebServlet("/messageServlet")
public class messageServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String title = request.getParameter("title");String content = request.getParameter("content");message ms = new message();ms.setTitle(title);ms.setContent(content);List<message> list = (List<message>)request.getSession().getAttribute("message");if(list == null){list = new ArrayList<message>();request.getSession().setAttribute("message", list);}list.add(ms);response.sendRedirect(request.getContextPath()+"/message.jsp?subFlag=1");//重定向跳转地址;}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
css编写:省略;
关于过滤器的编写:进行全局过滤器测试:
Fileter编写
package cn.java.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;@WebFilter("/Fileter")
//实例化接口
public class Fileter implements Filter {private FilterConfig config;public Fileter() {}public void destroy() {//过滤器使用操作,最后一次使用,最后面做清除操作 System.out.println("characterEncodigFilter destroy....");}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {//过滤器的方法,进行过滤器的操作//获取对象的时候做一些基本操作System.out.println("characterEncodigFilter doFilter..");//获取对象时候被使用,第二次操作,每次刷新都执行//1.注册过滤器需要在web.xml文件里面注册String sysName = config.getInitParameter("systemName");String version = config.getInitParameter("version");System.out.println(sysName);System.out.println(version);//这边可以设置网页的编码格式request.setCharacterEncoding(config.getInitParameter("encoding"));//2.拦截映射配置chain.doFilter(request, response);}public void init(FilterConfig Config) throws ServletException {//获取配置对象;System.out.println("characterEncodigFilter init...");//只有第一次调用的时候被使用,启动时第一步操作this.config = Config;}public FilterConfig getConfig() {return config;}public void setConfig(FilterConfig config) {this.config = config;}}
进行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>FilterDemo</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 注册过滤器 --><filter><filter-name>FileterEncodig</filter-name><filter-class>cn.java.filter.Fileter</filter-class><init-param><param-name>systemName</param-name><param-value>filterEncodig</param-value></init-param><init-param><param-name>version</param-name><param-value>1.0</param-value></init-param><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><!-- 做本地过滤器映射 --><filter-mapping><filter-name>FileterEncodig</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>
可以获取版本,编码格式,等操作;<url-pattern>/*</url-pattern>
用来做那个jsp页面会被调用;
进行基本的是否登录验证:利用过滤器:
package cn.java.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;@WebFilter("/SessionFileter")
public class SessionFileter implements Filter {public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {//进行是否登录验证,不一定需要过滤器来验证,也可以用其他方式HttpServletRequest hrequest = (HttpServletRequest)request;HttpServletResponse hresponse = (HttpServletResponse)response;String userLogin = (String)hrequest.getSession().getAttribute("loginName");if(userLogin == null){hresponse.sendRedirect(hrequest.getContextPath()+"/index.jsp?flag=404");return;}else {chain.doFilter(request, response);return;}}public void init(FilterConfig fConfig) throws ServletException {//这里面用来初始化配置}}
也需要进行web.xml配置:
<!-- 用于做用户是否登录的过滤器验证 --><filter><filter-name>sessionFilter</filter-name><filter-class>cn.java.filter.SessionFileter</filter-class></filter><filter-mapping><filter-name>sessionFilter</filter-name><url-pattern>/message.jsp</url-pattern><!-- 用于配置是否使用在哪个页面 --></filter-mapping>
最后实现效果:
就可以实现这些功能了,过滤器可以进行一些基本的过滤操作,基本验证,后面讲解监听器,可以实现目前在线用户的数量,访问量统计,防止用户重复登录等操作!
这篇关于Javaweb中过滤器的使用,并实现留言板的功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!