Solved

J2ME MIDlet & JSP question

Posted on 2006-06-19
20
526 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
  • 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
 
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:mayankeagle
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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:mayankeagle
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:mayankeagle
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 41
json format text only 4 65
wordappend challenge 8 85
Checkbox and ListView in Android Layout 4 39
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now