使用Java匹配过滤<script>标签,不区分大小写

本文介绍了一种使用JAVA自带方法过滤HTML中JS代码的有效方案。通过使用Pattern类的CASE_INSENSITIVE标志,可以忽略标签的大小写,从而实现对各种形式JS标签的全面过滤。

为了过滤一些内容当中包含有JS代码,因此加了对字符的过滤设置。刚开始并没有想到会有大小写的问题。

如果用户直接输入<script>JS代码</script>这样是可以进行过滤,但没考虑到如果用户输入的是大写标签或即有大写也有小小写的标签。

最开始想到一种比较笨的方法就是一一列举,然后一一匹配。着实太笨!

 

翻看API,发现原来JAVA已自带有这方面的方法。汗颜!!!

代码如下:

 /**
     * 对字符串当中的JS代码 进行过滤,全部替换为"非法字符"
     *
     * @param str 要过滤的字符串
     * @return 过滤后的字符串
     */
    public static String doFilter(String str) {
       str = Pattern.compile("<script.*?>.*?</script>", Pattern.CASE_INSENSITIVE).matcher(str).replaceAll("****");
        return str;
    }
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="zh-CN" class="bootstrap-admin-vertical-centered"> <head> <meta charset="UTF-8"> <title>图书馆管理系统</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="static/css/bootstrap.min.css"> <link rel="stylesheet" href="static/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="static/css/bootstrap-admin-theme.css"> <link rel="stylesheet" href="static/css/bootstrap-admin-theme.css"> <script src="static/js/bootstrap.min.js"></script> <script src="static/jQuery/jquery-3.1.1.min.js"></script> <script src="static/ajax-lib/ajaxutils.js"></script> <script src="static/js/login.js"></script> </head> <style type="text/css"> .alert{ margin: 0 auto 20px; text-align: center; } </style> <script src="static/js/jquery.min.js"></script> <script src="static/js/bootstrap.min.js"></script> <body class="bootstrap-admin-without-padding"> 池州学院 230312242 杨洪博 课设 <div style="background-image: url('image/2.png'); background-size: cover; background-repeat: no-repeat; background-position: center;"></div> <div class="col-lg-12"> <div class="alert alert-info"> <a class="close" data-dismiss="alert" href="#">×</a> 欢迎登录图书馆管理系统 </div> <form class="bootstrap-admin-login-form" method="post" action="/books/LoginServlet"> <% String state = (String)session.getAttribute("state"); session.removeAttribute("state"); if(state!=null){ %> <label class="control-label" for="username">密码错误</label> <%}%> <div class="form-group"> <label class="control-label" for="username">账 号</label> <input type="text" class="form-control" id="username" name="username" required="required" placeholder="学号"/> <label class="control-label" for="username" style="display:none;"></label> </div> <div class="form-group"> <label class="control-label" for="password">密 码</label> <input type="password" class="form-control" id="password" name="password" required="required" placeholder="密码"/> <label class="control-label" for="username" style="display:none;"></label> </div> <label class="control-label" for="password">没有账号请<a href="/books/register.jsp" style="color:blue;">注册</a></label> <br> <input type="submit" class="btn btn-lg btn-primary" value="登    录"/> </form> </div> </div> </div> <div class="modal fade" id="modal_info" tabindex="-1" role="dialog" aria-labelledby="addModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="infoModalLabel">提示</h4> </div> <div class="modal-body"> <div class="row"> <div class="col-lg-12" id="div_info"></div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" id="btn_info_close" data-dismiss="modal">关闭</button> </div> </div> </div> </div> </body> </html>文件路径没问题 但图片显示出来
06-22
<template> <div style="text-align: center; padding: 20px;"> <router-link to="/addPro"> <el-button type="primary">添加商品</el-button> </router-link> <ul style="list-style: none; padding: 0; margin: 0;"> <li v-for="pro in products" :key="pro.pid" style="display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid #eee;" > <span> {{ pro.pid }} | {{ pro.pname }} | {{ pro.catalog }} | {{ pro.catalog_name }} | ¥{{ pro.price }} | 库存: {{ pro.number }} </span> <span> <router-link :to="`/updatePro/${pro.pid}`"><button>修改</button></router-link> <button @click="deletePro(pro.pid)" style="margin-left: 8px;">删除</button> </span> </li> </ul> <el-pagination :current-page="currentPage" :page-size="pageSize" :total="total" layout="prev, pager, next, jumper" @current-change="handleCurrentChange"> </el-pagination> </div> </template> <script> import axios from 'axios' export default { data() { return { products: [], currentPage: 1, pageSize: 5, total:0 } }, created() { this.loadPro() }, methods: { async loadPro() { try { const res = await axios.get('/product/proList', { params: { page: this.currentPage, size: this.pageSize } }) this.products = res.data.list this.total = res.data.total console.log('返回数据:', res.data) } catch (err) { console.error('加载失败:', err) this.$message?.error('加载商品列表失败') } }, handleCurrentChange(newPage) { this.currentPage = newPage this.loadPro() }, async deletePro(pid) { if (!confirm('确定删除该商品?')) return try { await axios.get(`/product/deleteProById/${pid}`) this.$message?.success('删除成功') this.loadPro() } catch (err) { console.error('删除失败:', err) this.$message?.error('删除失败') } } } } </script>public List<Product> proList(int page, int size) { Sort sort = Sort.by(Sort.Direction.DESC, "pid"); Pageable pageable = PageRequest.of(page - 1, size,sort); return repository.findAll(pageable).getContent(); }@RequestMapping("proList") public PageResult<Product> proList( @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) { Pageable pageable = PageRequest.of(page - 1, size); Page<Product> productPage = repository.findAll(pageable); return new PageResult<>( productPage.getTotalElements(), // 总数 productPage.getContent() // 当前页数据 ); }怎么实现高亮分页模糊查询
最新发布
11-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值