Im Moment habe ich einen Filter, der die Sockets in der init geöffnet hat und aus irgendeinem Grund, wenn ich sie in doFilter() öffnen, funktioniert es nicht mit der Server-App richtig, so habe ich keine andere Wahl, als es in die init setzenServlet Filter: Socket muss in doFilter() referenziert werden
Ich muss in der Lage sein, die outSide.println verweisen ("test"); in doFilter(), also kann ich das an meine Server-App jedes Mal senden, wenn die if-Anweisung in es ausgelöst wird.
Heres mein Code:
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();}
}
}