communication between application and jsp

Discussion in 'iOS Programming' started by sagarshivam, Sep 22, 2011.

  1. sagarshivam macrumors member

    Joined:
    May 24, 2011
    #1
    Dear All,

    I am trying to pass some values (like username and password) from my application to some jsp file lying on some server. And code for this is:
    Code:
    NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",username, password];	
    	NSData *postdata = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    	request= [[[NSMutableURLRequest alloc] init] autorelease];
    	[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://128.9.45.182:8080/DV_DEMO/jsp/test.jsp"]]];
    	[request setHTTPMethod:@"POST"];
    	[request setHTTPBody:postdata];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    I am also using all the NSURLConnection delegate in the program
    And in the jsp file,
    Code:
    <% 
    String username = request.getParameter("username") ;
    String password = request.getParameter("password");
    out.println(username + password);
    %>
    
    And I am getting proper value in the jsp.
    But now I want to return back some string to the application.Say after username and password verification, some string should be returned back to the application. How this can be done?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Have a look at the overview in the documentation for NSURLConnection. You'll need to implement connection:didReceiveResponse: and connection:didReceiveData: in your NSConnection's delegate.

    Alternatively use sendSynchronousRequest:returningResponse:error: on a secondary thread. Grand Central Dispatch is a good mechanism for this.
     
  3. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #3
    I added further to my code:
    Code:
    NSURLResponse *response;
    	NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    	NSString *serveroutput= [[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
    	NSLog(@"serve response is %@", serveroutput);
    
    But in this case I am getting the whole jsp code.
    I want some variable's value (say String var defined in jsp).
    How is that possible.May ne using httpheaderfield or anything else, I am not aware of it.


    Thanks
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    If you're getting the JSP code back, then that's what your server is sending back. You can confirm by trying to go the same URL with a browser. This sounds like a configuration error on your server.
     
  5. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #5
    Thanks a lot!

    Actually it was the data, server is sending back. Now I am getting what really I want.
     

Share This Page