tags:

views:

455

answers:

2

Hello everyone and thanks in advance.

This is a last ditch effort to figure out what the problem is or find a better solution.

I am using JSP filter to filter web access to a tomcat web server.

I have a client, a server and the filter.

The client and the filter open up sockets the the server receives them.

I heard that opening up a server socket in a JSP files is a no-no but I cannot think of a better way to make it send a string to the server software, if you know any please do tell

But the problem on hand is that when the page is filtered it only send the string initially and not anytime after that

I have the socket opened in the filter and the receiver in the server program is in a thread so it should be taking and printing the string when it is received.

All of my code is zipped in here, you will need tomcat to run.

http://www.easy-share.com/1904209945/JNetProtect.zip

I'm really sorry for the length and complexity of this question, please if there is any better way to do this do speak up,

+1  A: 

From your explanation it looks to me, that you are lacking some significant concepts. Please excuse me if it sounds offensive to you.

JSP page is processed on the server, which means if you are opening up a socket in your JSP it doesn't mean client is opening the socket.

However, can't you use command pattern, probably using Servlet Filter, to direct your request to a particular Command object and then do your socket stuff there.

Adeel Ansari
Yeah sorry about that, I'm using a Serverlet Filter and doing the socket stuff under DoFilter method, but it seems like it it leaving the connection in mid air after a string gets sent once.
Well, I have just went through your filter code. So, you mean the first request to the page make the filter work, and after that subsequent requests doesn't work? Not too sure why this is happening. Try to open the socket in the init() and close it in destroy(). And look if there are any Exceptions.
Adeel Ansari
Hey and you are not closing the socket anywhere, I just noticed.
Adeel Ansari
Yeah, but it wasn't a problem I was just looking at this to see what was working and what wasn't and saw this didn't work.Thanks for the help I'm trying your suggestion
A: 

Well I didn't get any exceptions,

But what would you suggest I do with this filter so I can do an outSide.println() in the DoFilter()?

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
  {
    ServerSocket fs;
    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>");

       // 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
    {

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

  }
}
I guess the filter not having a main messed up the formatting, sorry about that.
You defined ServerSocket fs, for no reason. Further, you did open the socket in your init(), thats right, but its local, how you can use it in doFilter()? I hope you are getting my point.
Adeel Ansari
I noticed I couldn't use it, I really don't know what to do from here.