File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet not setting status in Response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet not setting status in Response" Watch "Servlet not setting status in Response" New topic
Author

Servlet not setting status in Response

Bindu Kantipudi
Greenhorn

Joined: Nov 30, 2005
Posts: 3
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();
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Smita
Welcome to JavaRanch!

We're pleased to have you here with us in the Servlets forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here

Note:
This was the 2nd time you've been asked.
[ December 29, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet not setting status in Response
 
Similar Threads
IO problem
problem in simple chat application.
how to exchange binary data between applet and server socket
please help with tokenizing
program to extract the full string based on requirement