『壹』 java web 過濾器跟攔截器的區別和使用
java web 過濾器跟攔截器的區別和使用分別介紹如下:
1、過濾器的使用
Filter主要對客戶端的請求和伺服器的響應進行過濾,使用場景:
客戶端的請求到達伺服器,伺服器真正開始處理這個請求之前,要經過Filter的過濾
伺服器真正的處理完這個請求,生成響應之後,要經過Filter的過濾,才能將響應發送給客戶端
作用:可以通過Filter技術,對web伺服器管理的所有web資源,例如JSP、Servlet、靜態圖片文件或靜態 html文件等進行攔截,從而實現一些特殊的功能。例如實現URL級別的許可權訪問控制、過濾敏感詞彙、壓縮響應信息等一些高級功能。
配置Filter
同開發Servlet一樣,寫完了類,接下來就是配置了,我們需要在web.xml文件中配置Filter。具體的配置和Servlet配置如出一轍。
<filter>
<filter-name>log</filter-name>
<filter-class>com.jellythink.practise.LogFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
上面配置中比較重要的就是url-pattern和dispatcher了。
過濾類:
public class LogFilter implements Filter
{
private FilterConfig config;
public void init(FilterConfig config)
{
this.config = config;
}
public void destroy()
{
this.config = null;
}
// 這個方法是Filter的核心方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
// 對用戶的請求進行處理
ServletContext context = this.config.getServletContext();
long begin = System.currentTimeMillis();
// 輸出過濾信息
System.out.println("開始過濾...");
HttpServletRequest hRequest = (HttpServletRequest)request;
System.out.println("Filter已經截獲到用戶請求的地址:" + hRequest.getServletPath());
// 處理完以後,將請求交給下一個Filter或者Servlet處理
chain.doFilter(request, response);
// 對伺服器的響應進行處理
long end = System.currentTimeMillis();
System.out.println("過濾結束");
System.out.println("請求被定為到:" + hRequest.getRequestURI() + "; 所花費的時間為:" + (end - begin));
}
}
2、攔截器的使用:
攔截器的主要作用是攔截用戶的請求並進行相應的處理。比如通過它來進行許可權驗證,或者是來判斷用戶是否登陸,或者是像12306那樣子判斷當前時間是否是購票時間。
1.在SpringMVC的配置文件中加上支持MVC的schema
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
下面是聲明示例:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
這樣在SpringMVC的配置文件中就可以使用mvc標簽了,mvc標簽中有一個mvc:interceptors是用於聲明SpringMVC的攔截器的。
『貳』 java 頁面怎麼傳值給過濾器fileder
在web.xml中添加一個過濾器,該過濾器要集成OncePerRequestFilter。
添加過濾器的代碼是:
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
『叄』 java過濾器怎麼不過濾一個頁面里包含的多個請求
abstract public class FilterPerRequest implements Filter {
private static final Boolean FILTERED = true;
private ThreadLocal<Boolean> statusLocal = new ThreadLocal<Boolean>();
@Override
final public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
if (hasNotFiltering()) {
doFilterAndDispatchRequest(req, resp, chain);
return;
}
//dispatch request only
chain.doFilter(req, resp);
}
private boolean hasNotFiltering() {
Boolean status = statusLocal.get();
if (status == null) {
return true;
}
return false;
}
private void doFilterAndDispatchRequest(ServletRequest req,
ServletResponse resp, FilterChain chain) throws IOException,
ServletException {
try {
doFilterPerRequest(req, resp);
setFiltered();
chain.doFilter(req, resp);
} finally {
cleanFilterStatus();
}
}
abstract protected void doFilterPerRequest(ServletRequest req,
ServletResponse resp);
private void setFiltered() {
statusLocal.set(FILTERED);
}
private void cleanFilterStatus() {
statusLocal.set(null);
}
}
那要就情況而定了。。。
如果是request范圍內的話,就可以使用上面的過濾器。
如果是session范圍內的話,就需要在session內存儲一個標志變數,方式和request差不多。
如果是限制用戶操作的話,那就必須將用戶操作持久化。
由於你的問題不明確,我也不好多說...
『肆』 java html字元過濾器
這是我看到一個不錯的,自己看看吧
import java.util.regex.Pattern;
public class Test
{
public static void main(String[] args)
{
String ww="<html>sss<body>ss</body>ssss</html>";
String ff=html2Text(ww);
System.out.println(ff);
}
public static String html2Text(String inputString) {
String htmlStr = inputString; // 含html標簽的字元串
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定義script的正則表達式{或<script>]*?>[\s\S]*?<\/script>
// }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定義style的正則表達式{或<style>]*?>[\s\S]*?<\/style>
// }
String regEx_html = "<[^>]+>"; // 定義HTML標簽的正則表達式
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 過濾script標簽
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 過濾style標簽
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 過濾html標簽
textStr = htmlStr;
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}
return textStr;
}
}
『伍』 在java中,使用過濾器編碼過濾亂碼時,過濾器的編碼怎麼寫
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {
if (null != encoding) {
HttpServletRequest request = (HttpServletRequest)srequest;
HttpServletResponse response = (HttpServletResponse)sresponse;
// 對請求進行編碼設置
request.setCharacterEncoding(encoding); //把請求用指定的方法編碼
response.setContentType("text/html");
response.setCharacterEncoding(encoding); //把返回響應指定 編碼方式
System.out.println("------------過濾編碼-------------" + encoding);
}
// 把處理許可權交給下一個過濾器
chain.doFilter(srequest, sresponse);
}//這是核心方法
正好項目中有用到
『陸』 javaweb,給HTML文件加上過濾器以後,HTML顯示亂碼。
那是你的瀏覽器不識別Content-Type,採用了默認的GBK/GB2312編碼顯示,這個可以在打開頁面「右鍵-編碼」看到;meta最好給一個結束符,即:<meta http-equiv="content-type" content="text/html; charset=utf-8" />
『柒』 關於sevlet、過濾器、jsp、html、xml等這些javaweb的相關知識和用途,請各位大大們給個詳細的解釋和區別~
說簡單點:servlet就是接抄收頁面請求然後轉發的。
過濾器就是在servlet之前可以做驗證、亂碼方面的。
jsp就是java語言中的動態頁面.
html就是靜態頁面。
太詳細的話這里1W個字都說不完的.
『捌』 在HTML頁面寫一個過濾器,實現傳進SERVLET的CLASS文件裡面的編碼機制統一
在頁面的請求裡面傳遞中文時候,會變成亂碼,所以每次請求都需要 request.setCharacterEncoding("utf-8");
周睿羊做很麻煩,在web.xml裡面寫一個添加一個過濾器
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>cn.zcq100.util.CharacterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
然後建立一個類,實現import javax.servlet.Filter;介面
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chian) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chian.doFilter(request, response);
}
這樣每次請求的時候都會先通過這個過濾器過濾。
『玖』 filter的使用 java 過濾器的幾種使用方法
過濾器來
過濾器是處於客戶端自與伺服器資源文件之間的一道過濾網,在訪問資源文件之前,通過一系列的過濾器對請求進行修改、判斷等,把不符合規則的請求在中途攔截或修改。也可以對響應進行過濾,攔截或修改響應。
過濾器一般用於登錄許可權驗證、資源訪問許可權控制、敏感詞彙過濾、字元編碼轉換等等操作,便於代碼重用,不必每個servlet中還要進行相應的操作。
『拾』 jsp\java如何編寫過濾器過濾特殊字元
正則表達式來校驗:過濾器就網路一大堆,怎麼寫正則表達式,也可以網路,不知你說的特殊字元是什麼字元,所以只能給方法