Hi,
I have a servlet which acts like a proxy. First when servlet gets request, It creats a socket to a reporting server (run's on 80 port) and sends a request. After getting a file from that server this servlet tries to set status and the header in HttpServletResponse. Latter it writes the data to a ServletOutputStream.
My problem is, the page is not displayed on the browser when I try to access it from webserver. If I run the application directly on test box,it is displaying the page. When I check the webserver logs it is sending a request as below.
/ActuateReportServlet?report=TransactionHistory HTTP/1.0" - -
and I am thinking it is not setting the status and so server is not displaying the page.
Please help me with this problem.
Here is the code in servlet which does
Socket socket = new Socket(host, port);
boolean b1 = socket.isConnected();
System.out.println("Socket Connection = "+b1);
PrintStream ps = new PrintStream(socket.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String file = url.getFile();
String request = new StringBuffer("GET ").append(file).append(" HTTP/1.0\r\n\r\n").toString();
ps.print(request);
System.out.println("Print Stream = "+request);
java.util.StringTokenizer st = new java.util.StringTokenizer(br.readLine());
String resProtocol = st.nextToken();
String resStatus = st.nextToken();
int status = 500;
if (resStatus != null && !resStatus.equalsIgnoreCase("")) {
status = Integer.parseInt(resStatus);
System.out.println("resStatus int = "+status);
}
String line = null, name = null, value = null;
if ( status == res.SC_MOVED_TEMPORARILY ) {
while( !(line = br.readLine()).equalsIgnoreCase("") ) {
name = line.substring(0, line.indexOf(":"));
value = line.substring(line.indexOf(":") + 1, line.length());
if ( name != null && name.equalsIgnoreCase("Location") ) {
break;
}
}
if ( MLSFormatter.isValidString( value )) {
String location = (String) System.getProperty("imaging.server");
downloadNetworkResource( req, res, location, value.trim(), null );
status = 200;
}
} else {
ServletOutputStream sos = res.getOutputStream();
res.setStatus(res.SC_OK);
while(!(line = br.readLine()).equalsIgnoreCase("")) {
name = line.substring(0, line.indexOf(":"));
value = line.substring(line.indexOf(":") + 1, line.length());
res.setHeader(name, value);
System.out.println("Name = "+name);
System.out.println("Value = "+value);
}
while ((b = bis.read(buf)) != -1)
{
System.out.println("bytes b = "+b);
/*char c;
System.out.println("b = "+b);
for(int i=0; i<buf.length; i++){
c = (char)buf[i];
System.out.print(c);
}*/
sos.write(buf, 0, b);
}
sos.flush();
sos.close();
/*bis.close();
br.close();
ps.close();*/
}
bis.close();
br.close();
ps.close();