communication between application and jsp

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

  1. sagarshivam macrumors member

    May 24, 2011
    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:
    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:@""]]];
    	[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,
    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


    Dec 16, 2010
    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

    May 24, 2011
    I added further to my 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.

  4. jiminaus macrumors 65816


    Dec 16, 2010
    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

    May 24, 2011
    Thanks a lot!

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

Share This Page