现在我有一个过滤器,在init中打开了套接字,出于某种原因,当我在doFilter()中打开它时,它无法正常使用服务器应用程序,所以我别无选择,只能把它放在初始化
我需要能够引用outSide.println(“test”);在doFilter()中,每次if语句被触发时,我都可以将它发送到我的服务器应用程序。
继承我的代码:
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public final class IEFilter implements Filter
{
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
String browser = "";
String blockInfo;
String address = request.getRemoteAddr();
if(((HttpServletRequest)request).getHeader ("User-Agent").indexOf("MSIE") >= 0)
{
browser = "Internet Explorer";
}
if(browser.equals("Internet Explorer")) {
BufferedWriter fW = new BufferedWriter(new FileWriter("C://logs//IElog.rtf"));
blockInfo = "Blocked IE user from:" + address;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>");
out.println("This page is not available - JNetProtect");
out.println("</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<center><H1>Error 403</H1>");
out.println("<br>");
out.println("<br>");
out.println("<H1>Access Denied</H1>");
out.println("<br>");
out.println("Sorry, that resource may not be accessed now.");
out.println("<br>");
out.println("<br>");
out.println("<hr />");
out.println("<i>Page Filtered By JNetProtect</i>");
out.println("</BODY>");
out.println("</HTML>");
//init.outSide.println("Blocked and Internet Explorer user");
fW.write(blockInfo);
fW.newLine();
fW.close();
} else {
chain.doFilter(request, response);
}
}
public void destroy()
{
outsocket.close();
outSide.close();
}
public void init(FilterConfig filterConfig)
{
try
{
ServerSocket fs;
Socket outsocket;
PrintWriter outSide ;
outsocket = new Socket("Localhost", 1337);
outSide = new PrintWriter(outsocket.getOutputStream(), true);
}catch (Exception e){ System.out.println("error with this connection"); e.printStackTrace();}
}
}
答案 0 :(得分:0)
另一种可能性是使用log4j并让它处理套接字处理。 log4j可以配置为将来自此过滤器的日志消息保存在与任何其他日志记录不同的文件中。 这样就完全不需要代码中的套接字了。