Results 1 to 6 of 6
  1. #1
    vishal21 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Data not received by the servlet

    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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    First please use code tags.
    Second, this is your problem:
    Java Code:
    while([B][U]br.ready()[/U][/B]) {
        //out.println("\n inside while loop");
        line = br.readLine();
        line = line.trim();
        len_line = line.length();
    }
    ready() doesn't do what you think it does. It merely tells you whether the next read() (or readLine()) will block or not. In your case it's almost certain that it will block waiting for the first bit of data, meaning you never actually read anything.

    The usual technique is:
    Java Code:
    while((line = br.readLine()) != null) {
        //out.println("\n inside while loop");
        line = line.trim();
        len_line = line.length();
    }
    Oh, and you don't write to an input stream. This is a comment on your post, not your code since you aren't writing to an input stream in your code...

  3. #3
    vishal21 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Hi Tolls,
    Thanks for such a quick reply..
    But I did the change which you had mentioned.

    But it did not solve the problem.
    Even now I am not able to write to servlet.

    Thanks and Regards,
    Vishal

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    So, what does your code look like now?
    What problems are you seeing?

  5. #5
    vishal21 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Servlet Code :
    -----------------

    {
    static int DEBUG = 1;
    public void init() { }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String line = null;
    String [] fields;
    String ch_dollar = "$", ch_colon = ":";
    String name = null, fathersname = null, votersid = null, sex = null, address = null;
    String db="janadb";
    String username="root";
    String password="vishal";
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    String sendString = null;
    int count = 0;
    String strCount = null;
    String Category = null;
    String Parameters = null;
    String StringD = "D";



    FileWriter file = new FileWriter("C:\\log.txt");
    BufferedWriter bwriter = new BufferedWriter(file);





    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()) {
    while( (line = br.readLine()) != null ) {
    //out.println("\n inside while loop");
    //line = br.readLine();
    line = line.trim();
    len_line = line.length();
    }
    br.close();
    if (DEBUG == 1) bwriter.write("\n send String from SERVER = " + line );
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();

    //if (DEBUG == 1) bwriter.write("\n inside doPost = ");
    if (DEBUG == 1) bwriter.write("\n send String from SERVER = " + line );
    fields = split(line, ch_dollar);
    Category = fields[0];
    Category = Category.trim();
    len_Category = Category.length();
    Parameters = fields[1].trim();
    Parameters = Parameters.trim();
    len_Parameters = Parameters.length();

    =====================
    client code ..which is trying to connect to servlet.
    This client is J2ME client.

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

    Here also log.txt is getting created on the servlet side. But its size is 0.
    That means servlet is getting invoked but its not able to receive any data.
    I am still try to figure out the problem.
    osHttp.writeUTF(recvdStr) is writing data to the output stream but it is not getting received on servlet side by br.readLine();

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Can you please use code tags.

    ANyway, I just noticed:
    Java Code:
    while( (line = br.readLine()) != null ) {
        //out.println("\n inside while loop");
        //line = br.readLine();
        line = line.trim();
        len_line = line.length();
    }
    You're supposed to be writing this out at the same time you know.
    Otherwise you'll simply end up with line = null.

Similar Threads

  1. detecting data received is malicious or not
    By prashant in forum Networking
    Replies: 2
    Last Post: 02-02-2009, 03:58 PM
  2. get data from servlet to html
    By lema in forum Java Servlet
    Replies: 7
    Last Post: 05-22-2008, 04:00 PM
  3. Save data from Excel sheet to servlet
    By chandu.v in forum Java Servlet
    Replies: 2
    Last Post: 04-10-2008, 01:51 PM
  4. get data from html to servlet
    By lema in forum Java Servlet
    Replies: 66
    Last Post: 04-09-2008, 02:43 PM
  5. How to retrieve data from servlet
    By valery in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2007, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •