Servlet Filter: Socket need to be referenced in doFilter()

Posted by Craig m on Stack Overflow See other posts from Stack Overflow or by Craig m
Published on 2009-03-27T05:07:15Z Indexed on 2010/05/30 13:02 UTC
Read the original article Hit count: 427

Filed under:
|
|
|

Right now I have a filter that has the sockets opened in the init and for some reason when I open them in doFilter() it doesn't work with the server app right so I have no choice but to put it in the init

I need to be able to reference the outSide.println("test"); in doFilter() so I can send that to my server app every time the if statement it in is is tripped.

Heres my 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();}

  }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about servlets