/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.javaee7.servlet.filters;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
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;
/**
* @author Arun Gupta
*/
@WebFilter(filterName = "LoggingFilter", urlPatterns = {"/*"})
public class LoggingFilter implements Filter {
private static final boolean debug = true;
// The filter configuration object we are associated with. If
// this value is null, this filter instance is not currently
// configured.
private FilterConfig filterConfig = null;
public LoggingFilter() {
}
private void doBeforeProcessing(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
if (debug) {
log("LoggingFilter:DoBeforeProcessing");
}
// Write code here to process the request and/or response before
// the rest of the filter chain is invoked.
// For example, a logging filter might log items on the request object,
// such as the parameters.
for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) {
String name = (String) en.nextElement();
String values[] = request.getParameterValues(name);
int n = values.length;
StringBuilder buf = new StringBuilder();
buf.append(name);
buf.append("=");
for (int i = 0; i < n; i++) {
buf.append(values[i]);
if (i < n - 1) {
buf.append(",");
}
}
log(buf.toString());
}
}
private void doAfterProcessing(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
if (debug) {
log("LoggingFilter:DoAfterProcessing");
}
// Write code here to process the request and/or response after
// the rest of the filter chain is invoked.
// For example, a logging filter might log the attributes on the
// request object after the request has been processed.
for (Enumeration en = request.getAttributeNames(); en.hasMoreElements();) {
String name = (String) en.nextElement();
Object value = request.getAttribute(name);
log("attribute: " + name + "=" + value.toString());
}
// For example, a filter might append something to the response.
PrintWriter respOut = new PrintWriter(response.getWriter());
respOut.println("<P><B>This has been appended by an intrusive filter.</B>");
}
/**
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (debug) {
log("LoggingFilter:doFilter()");
}
doBeforeProcessing(request, response);
Throwable problem = null;
try {
chain.doFilter(request, response);
} catch (IOException | ServletException t) {
// If an exception is thrown somewhere down the filter chain,
// we still want to execute our after processing, and then
// rethrow the problem after that.
problem = t;
t.printStackTrace();
}
doAfterProcessing(request, response);
// If there was a problem, we want to rethrow it if it is
// a known type, otherwise log it.
if (problem != null) {
if (problem instanceof ServletException) {
throw (ServletException) problem;
}
if (problem instanceof IOException) {
throw (IOException) problem;
}
sendProcessingError(problem, response);
}
}
/**
* Return the filter configuration object for this filter.
*/
public FilterConfig getFilterConfig() {
return (this.filterConfig);
}
/**
* Set the filter configuration object for this filter.
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
/**
* Destroy method for this filter
*/
@Override
public void destroy() {
}
/**
* Init method for this filter
*/
@Override
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
if (filterConfig != null) {
if (debug) {
log("LoggingFilter:Initializing filter");
}
}
}
/**
* Return a String representation of this object.
*/
@Override
public String toString() {
if (filterConfig == null) {
return ("LoggingFilter()");
}
StringBuilder sb = new StringBuilder("LoggingFilter(");
sb.append(filterConfig);
sb.append(")");
return (sb.toString());
}
private void sendProcessingError(Throwable t, ServletResponse response) {
String stackTrace = getStackTrace(t);
if (stackTrace != null && !stackTrace.equals("")) {
try {
response.setContentType("text/html");
try (PrintStream ps = new PrintStream(response.getOutputStream());
PrintWriter pw = new PrintWriter(ps)) {
pw.
没有合适的资源?快使用搜索试试~ 我知道了~
javaee7-samples 是一个项目用来演示 Java EE 7 各种新特性的示例,有超过 160 个示例程序
共1308个文件
java:549个
xml:439个
jsp:141个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 61 浏览量
2024-03-26
09:36:57
上传
评论
收藏 1.95MB ZIP 举报
温馨提示
javaee7-samples 是一个项目用来演示 Java EE 7 各种新特性的示例,有超过 160 个示例程序
资源推荐
资源详情
资源评论
收起资源包目录
javaee7-samples 是一个项目用来演示 Java EE 7 各种新特性的示例,有超过 160 个示例程序 (1308个子文件)
default.css 3KB
default.css 3KB
default.css 3KB
default.css 3KB
main.css 2KB
cssLayout.css 2KB
cssLayout.css 2KB
cssLayout.css 2KB
cssLayout.css 2KB
cssLayout.css 940B
cssLayout.css 940B
cssLayout.css 940B
cssLayout.css 940B
cssLayout.css 940B
default.css 651B
default.css 651B
default.css 651B
default.css 651B
default.css 651B
main.css 189B
main.css 189B
main.css 189B
main.css 189B
stylesheet.css 131B
mydata.csv 183B
mydata.csv 183B
.DS_Store 6KB
.gitignore 45B
contracts-library-impl-1.0-SNAPSHOT.jar 10KB
my-web-fragment.jar 4KB
myResources.jar 816B
LoggingFilter.java 9KB
JsonParserFromReader.java 9KB
TestServlet.java 8KB
TestClient.java 8KB
JsonParserFromStream.java 8KB
TestServlet.java 7KB
TestServlet.java 7KB
DOMGeneratorServlet.java 7KB
StreamingGeneratorServlet.java 7KB
TestServlet.java 7KB
TestServlet.java 6KB
TestServlet.java 6KB
TestScheduleServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
JsonReaderFromReader.java 6KB
MyAsyncServlet.java 6KB
TestClient.java 6KB
TestClient.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestJNDIServlet.java 6KB
TestServlet.java 6KB
TestInvokeAllServlet.java 6KB
JsonReaderFromStream.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestJNDIServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServletSendAsync.java 6KB
TestInvokeAnyServlet.java 6KB
TestServlet.java 6KB
TestMultipleInterfaceServlet.java 6KB
TestJNDIServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestServlet.java 6KB
TestCDIServlet.java 6KB
TestServletSendSync.java 6KB
TestClient.java 6KB
TestResourceServlet.java 6KB
TestServlet.java 6KB
TestScheduleFixedRateServlet.java 6KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestClient.java 5KB
TestServlet.java 5KB
TestServlet.java 5KB
TestServletJNDI.java 5KB
TestRemoteClient.java 5KB
TestClient.java 5KB
TestLocalClient.java 5KB
共 1308 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6718
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Code for the complete guide to tkinter tutorial
- 关于百货中心供应链管理系统.zip
- SimpleFolderIcon-master 修改Unity的Project下的文件夹图标
- A python Tkinter widget to display tile based maps
- A pure Python library for adding tables to a Tkinter application
- Vector资源文件.zip
- MobaXterm-Installer
- MicroMsg.xlsx
- 88-520告白(520气球).zip
- HTML+CSS+JS精品网页模板H126.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功