简体   繁体   中英

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

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

  }
}

Another possibility would be to use log4j and have it handle the socket handling. log4j can be configured to keep the log messages from this filter in a separate file from any other logging. That would remove the need for sockets in your code entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM