Solved

J2ME MIDlet & JSP question

Posted on 2006-06-19
20
531 Views
Last Modified: 2013-11-23
i'm following an old tutorial that shows the parsing of inputted data from a mobile device to a jsp, however, I have 2 problems and I'd appreciate if anyone can shed some light: Here's the code.

public void invokeJSP(String url) throws IOException {
            HttpConnection c = null;
            InputStream is = null;
            OutputStream os = null;
            OutputStream os2 = null;
            StringBuffer b = new StringBuffer();
            TextBox t = null;
            try {
                  c = (HttpConnection)Connector.open(url);
                  c.setRequestMethod(HttpConnection.POST);
                  c.setRequestProperty("IF-Modified-Since", "25 Nov 2001 15:17:19 GMT");
                  c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
                  c.setRequestProperty("Content-Language", "en-CA");
                  c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                  
                  os = c.openOutputStream();
                  os.write(("name="+myname).getBytes());
                  os.write(("password="+mypassword).getBytes());

                                                 /*this seems a problem so i'm commenting it out.
                    need to find out what it actually does and what the implications are of
                    not flushing.*/
                  //os.flush();
                  
                  is = c.openDataInputStream();
                  int ch;
                  while ((ch = is.read()) != -1) {
                        b.append((char)ch);
                        System.out.print((char)ch);
                  }
                  t = new TextBox("Date", b.toString(), 1024, 0);
                  t.setCommandListener(this);
            } finally {
                  if (is != null){
                        is.close();
                  }
                  if (os != null) {
                        os.close();
                  }
                  
                  if (c != null) {
                        c.close();
                  }
            }
                  display.setCurrent(t);
            }
1. if i do the os.flush() - i get null in my jsp, if i don't flush it, the variable gets returned.
2. if i do a second variable, i.e password, it appends it to the first variable

my jsp looks as follows.
<%! String name; String password; %>
<%
name = request.getParameter("name");
password = request.getParameter("password");
out.println("Got: " + name);
out.println("Password: " + password);

%>

In the emulator, the 2 variables return as
Got: superpassword=null
Password: null

any ideas?
thanks.
blu.
0
Comment
Question by:blue-genie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 3
20 Comments
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933067
The first thing I notice is that there is no separator in the parameters:

               os.write(("name="+myname).getBytes());
               os.write(("password="+mypassword).getBytes());

This would result in a byte stream like:
name=xxxpassword=yyy

I suspect there should be a \n after each variable.


0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933084
Ah, no a "&" must be present:

name=xxx&password=yyy
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16933285
what would the exact syntax be?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Author Comment

by:blue-genie
ID: 16933303
ok, sorted,
os.write(("name="+myname).getBytes());
os.write(("&password="+mypassword).getBytes());
did the trick.
Any ideas about the flush()?
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933330
flushing is good
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16933335
Make sure instead of the platform default encoding you use the encoding which the web-server/ servlet container hosting the JSP uses.
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933344
flushing should not have any differences on the output, does it still occur?
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16933350
tbeernot, yes, i'm aware flushing is good, but if i flush i get a null returned.
mayankeagle, i have not clue what u're referring to .... :-(
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933361
instead of flushing try closing the stream, let's see what that does
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16933401
i am closing the stream. as long as i don't flush() it's fine.
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933416
an alternative would be to place the parameters in the URL and use GET. The JSP container will interprete that. This approach has a limit on the length of the URL, but with a few parameters this should work ok:

 ?name=xxx&password=yyy
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933440
I meant closing instead of flushing. BTW, I found the article which you probably used as a base (code is very very similar ;-) but I can't see what the problem with that code in this case would be. The stream really needs to be flushed or closed, otherwise the behaviour is unpredictable.

Can you capture the network traffic so we can see what the MIDP is actually sending (and the difference between flush and no-flush)? (You could use the very handy PlugProxy, http://www.cs.nyu.edu/brw215/plugproxy/).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16934148
>>  i have not clue what u're referring to

getBytes () uses the platform default encoding to convert the string to a byte-array. If you use getBytes ( "ASCII" ), it uses ASCII encoding. getBytes ( "UTF-8" ) uses UTF-8 encoding. You should use the encoding which the JSP server supports.
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16934553
hmm, ok...
i think i'm using the right thing.
any idea why if my application is parsing 2 varibles back, and they can be written out, but i can't use them in a prepared statement?

out.println("Username:" + name); // these write out - parsed from textfield in application on device
out.println("Password:" + password); // these write out - parsed from textfield in application on device

synchronized(pstmVerifyUser) {
      pstmVerifyUser.setString(1, (String)name); //have also tried toString();
      pstmVerifyUser.setString(2, (String)password);
      rsUserID = pstmVerifyUser.executeQuery();
}

blu
0
 
LVL 2

Accepted Solution

by:
tbeernot earned 500 total points
ID: 16934632
Try passing
    "" + name
    "" + password
to the setString

Does the flush work now?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16934698
>> i think i'm using the right thing.

No, you're using the platform default and it will cause problems if the default encoding is different on both machines :)
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16935268
ah ok. i figured out why my variables weren't right. for some reason the emulator puts a space before the cursor when you start typing.
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 17121784
There are two questions in this question:

1. if i do the os.flush() - i get null in my jsp, if i don't flush it, the variable gets returned.
2. if i do a second variable, i.e password, it appends it to the first variable

My post from 06/19/2006 03:09AM PDT and 06/19/2006 03:12AM PDT answers question 2.
As to question 1; I have never gotten an answer to my post from 06/19/2006 07:24AM PDT and it is unclear weather that still is an issue.
0
 
LVL 39

Author Comment

by:blue-genie
ID: 17121822
hi sorry - i was so busy trying to get it to work.
in terms of solutions, 1. i am still not flushing the thing, can't get it to work with flushing, but i am closing the stream.
2. the second solution was viable and worked.

thanks peeps.
sorry i took so long to get back to you.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question