• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet not receiving any data

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
From a client code, I am establishing HTTP connection to a servlet.

The servlet has to receive data on HTTP connection and write to InputStream. I am also writing a logs to log.txt
The servlet doPost code is as follows

public class TestServlet extends HttpServlet
{
static int DEBUG = 1;
public void init() { }

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String line = null;
String [] fields;
.
.
.
.
.
FileWriter file = new FileWriter("C:\\log.txt",true);
BufferedWriter bwriter = new BufferedWriter(file);


response.setContentType("text/plain");
PrintWriter out = response.getWriter();

bwriter.write("\n log file created");

InputStream stream = request.getInputStream();
int len_line = 0;
int len_Category = 0;
int len_Parameters = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(stream));

while(br.ready()) {
//out.println("\n inside while loop");
line = br.readLine();
line = line.trim();
len_line = line.length();
}
br.close();



--------------------
The client code is trying to send data to the servlet. The client code is as follows.

connHttp = (HttpConnection)Connector.open("http://localhost:8080/star/TestServlet");
connHttp.setRequestProperty("User-Agent", "Profile/MIDP-1.0, Configuration/CLDC-1.0");
connHttp.setRequestProperty("Content-Language", "en-US");
connHttp.setRequestMethod("POST");
System.out.println(" Http Connection done ");

// Opening output stream to my servlet
try{
osHttp = (DataOutputStream)connHttp.openDataOutputStream();
if (osHttp == null ) {
System.out.println(" osHttp is null ");
}
else {
System.out.println(" osHttp is not null");
}
//osHttp.flush();
}
catch(Exception io) {
System.out.println(" Exception while opening osHttp " + io.getMessage());
}

// Writing to the output stream
try {
osHttp.writeUTF(recvdStr);
osHttp.flush();
}
catch(Exception io )
{
System.out.println(" Exception in io : Message = " + io.getMessage() );
io.printStackTrace();
// io.getCause() ;
}


When I execute this, I see log.txt is created with 0 bytes but nothing is written to it. Somehow data is not written to InputStream.
Can anyone guide me here..

Thanks and Regards,
Vishal
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, I'm trying to look at your code, but the stubborn forum software has made it look all wonky. If you use CODE tags when posting code to the forums (see the FAQ at ⇒ UseCodeTags ⇐) they tell it "Hey! Keep your hands off my formatting!" and your code will look just like when you cut and pasted it into the reply. Cool, no?
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe, writing a log outside the servlet container' directory structure may have been prevented. You may need to find an alternative location to write the logs. May be there is a logs folder within your Web/Application server. Also, you should attempt to use the standard logging frameworks such as Log4J etc...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic