Link to home
Start Free TrialLog in
Avatar of annasfe
annasfe

asked on

Simple question about parameter passing between WML and database

Here I am again, a simple one this time. I send parameters from a wml file to a jsp file in order to enter them in a database but the parameters dont get there correct. What I mean is :
wml file:
<do type="accept" label="SUBMIT">
  <go method="post" href="insert.jsp">
        <postfield name="name" value="$name"/>
        <postfield name="mobile" value="$mobile"/>
 </go>
</do>

jsp file:

String name = request.getParameter("name");
String mobile = request.getParameter("mobile");
PreparedStatement PStmt = conn.prepareStatement("insert into agenda (name, mobile) values (\"" + name + "\",\"" + mobile + "\")");

The values in the database appear like $name and $mobile instead of the real values of the parameters. What is wrong?


Avatar of jimmack
jimmack

Hi again,

  I'm on the boundaries of my knowledge here ;-)...

  I think your problem could be solved by switching from a PreparedStatement to just using a Statement.

  With a PreparedStatement, the idea is that you create a statement that contains "?" symbols.  This allows you to add variables into that statement later.  In this case, you already know everything you need when you create the statement, so you don't really need this functionality.

  For an example of a PreparedStatement (which I haven't used ;-)), look at:

https://www.experts-exchange.com/questions/20799827/Updates-and-images-displayed-on-coordinates.html

  You'll need to scroll down the question about half-way.  Jasbir21 has posted some code that uses a PreparedStatement.
Oops.  Sorry about that.  I read your question (and responded) too quickly :-(

The problem is not with the PreparedStatement, though I would still stick to my original comment about using a Statement instead.

The problem is with the way you have defined the "value" elements in the WML.  I *think* the problem is that you have put inverted commas (") around the $name and $mobile values.  ie. I *think* it should be:

<do type="accept" label="SUBMIT">
  <go method="post" href="insert.jsp">
        <postfield name="name" value=$name/>
        <postfield name="mobile" value=$mobile/>
 </go>
</do>

I'll try to check this now.
It looks like it should be $(name) and $(mobile).

Not inside inverted commas.
Avatar of annasfe

ASKER

First, thanks again for your trying to help :)

I am sorry but this doens work :( When I change it and delete the "" the page doesnt load and I get the server error. Apart from that, I tried in the code as it is (without deleting the "")  to print the data sent to the jsp page (skipping the database preparedstatement stuff) and it prints the correct data. So, I guess that there is sth wrong with the database thing. I have been searching around internet but no luck, only for php code which unfortunatelly I cant understand very well or use. I will try to check your comment for the statement and let you know...

I tried:
"$name"
"$(name)"
$name
$(name)

nothing works :(
>> I tried in the code as it is ... and it prints the correct data

If you've displayed the data successfully from the JSP page, then you can ignore my comments regarding the $name ;-), because, as you say, the problem must lie with the database update.  Just for interest, how are you displaying this data?

I'd still go witht the Statement instead of PreparedStatement, and to simplify the syntax a bit, I'd use ' instead of " to surround the data:

Statement stmt = conn.createStatement();
stmt.executeUpdate("insert into agenda (name, mobile) values ('" + name + "', '" + mobile + "')");

Hope this helps.
Avatar of annasfe

ASKER

Still no luck :(
I changed the prepared statement with the simple one but still the same. I am sure at the end it will be some stupid "" missing or sth but this thing is driving me nuts! And the wierd thing is that the data get to the jsp page correctly since I can print them ( I use the thing you suggested the previous time <p><%=name%></p>)

Any more ideas?
OK ;-)

Let's find out what is being written to the database.  Change the statement code as follows:

Statement stmt = conn.createStatement();
String insertCommand = "insert into agenda (name, mobile) values ('" + name + "', '" + mobile + "')";
out.println("About to do this: " + insertCommand);
stmt.executeUpdate(insertCommand);

That should output the line that is to be executed.

Also (whilst I remember ;-)) are you certain that the faulty data in the database has just been written (eg. are you clearing out the old data before running again)?
Avatar of annasfe

ASKER

Ok..
The ouput I get is the one it should be
"About to do this: insert into agenda (name, mobile) values ('anna','123456789')"
(by the way the out.println was creating a server error if you dont put the <p></p> tag :P)

BUT in the database still the same thing :(
(yes, i delete every time the old entry)
Well now.  That is strange.

Can you post some more of the JSP code.  I suspect that you might have another line in there (perhaps an old test line or something) that is causing the problem.

Excellent catch on the <p> BTW ;-)
Avatar of annasfe

ASKER

Ok, here is the hole jsp code (sorry I was late, I was abit sick these days)

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//OPENWAVE.COM//DTD WML 1.3//EN"
                "http://www.openwave.com/dtd/wml13.dtd" >
<%@ page import="java.util.*, java.sql.*, java.io.*" %>

<%response.setContentType("text/vnd.wap.wml");%>
<wml>
 <card id="inser_contact" title="Insert Contact">

<%
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn4 = DriverManager.getConnection("jdbc:mysql://...", "user", "password");

String name = request.getParameter("name");
String mobile = request.getParameter("mobile");
String fixed = request.getParameter("fixed");
String groupo = request.getParameter("groupo");
int found=0;
String flag="0";
String userid = request.getParameter("userid");

//CheckMobile checkmobile = new CheckMobile();

if (name.equals("") || mobile.equals(""))
{
%>

<jsp:forward page="agenda.jsp">
<jsp:param name="flag" value="1" />
<jsp:param name="userid" value="<%=userid%>" />
</jsp:forward>

<%
}
else
{
  //mobile = checkmobile.change(mobile);
  if(mobile.equals("error"))
  {
   %>

   <jsp:forward page="agenda.jsp">
   <jsp:param name="flag" value="2" />
   <jsp:param name="userid" value="<%=userid%>" />
   </jsp:forward>

   <%

  }
}

if(!fixed.equals(""))
{
  //fixed = checkmobile.change(fixed);
  if(fixed.equals("error"))
  {
   %>

   <jsp:forward page="agenda.jsp">
   <jsp:param name="flag" value="2" />
   <jsp:param name="userid" value="<%=userid%>" />
   </jsp:forward>

   <%

  }

}


PreparedStatement st = conn4.prepareStatement("select name from agenda where id=?");
st.setString(1,userid);
ResultSet rs = st.executeQuery();

while(rs.next()){

  if(name.equals(rs.getString("name")))
      found=1;
}

if(found==1)
{
%>

<jsp:forward page="agenda.jsp">
<jsp:param name="flag" value="3" />
<jsp:param name="userid" value="<%=userid%>" />
</jsp:forward>

<%
}
Statement stmt = conn4.createStatement();
String insertCommand = "insert into agenda (name, mobile) values ('" + name + "', '" + mobile + "')";
out.println("<p>About to do this: " + insertCommand + "</p>");
stmt.executeUpdate(insertCommand);


conn4.close();
%>

</card>
</wml>


Does it help? (dont pay attention in the checkmobile function, is an old one that returns "error" if the mobile doesnt have the correct format)
Avatar of annasfe

ASKER

I will also increase the points cause it appears to be trickier than i thought :)
Hi annasfe, I hope you're feeling better.

>>  I will also increase the points cause it appears to be trickier than i thought :)

;-)

Well your code looks nice and clean to me.  I can't see why it doesn't work (at the moment ;-)).  Can I just clarify?

1) The INSERT is actually happening into the agenda database, but the values being added are "$name" and "$mobile", but the println result is displaying:

2) "About to do this: insert into agenda (name, mobile) values ('anna', '123456789')"

If this is the case, then something very strange is happening with the database write.  If this was my app, I would try the following:

Change the insert command to a single literal test string using slightly different text to ensure the updated page is being used, eg:
String insertCommand = "insert into agenda (name, mobile) values ('annatest', '999999999')";

I would expect that to work.  If it did, I would replace the original line and try again.


One other quick point.  Assuming that your database prevents duplicates (eg. if name and mobile are both primary keys), you can replace:

>> while(rs.next()){
>>
>>  if(name.equals(rs.getString("name")))
>>      found=1;
>> }
>>
>> if(found==1)
>> {

with:

if (rs.next())
{
Avatar of annasfe

ASKER

Ok,I tried your comments, when I put normal values like "annatest" and "999999999" it works fine but when i change it back to the variables still the same :(
BUT i have a new idea! Here is what i thought that causes the problem:

When I print the insert command it looks fine in the WAP browser but this is because in WAP the $name part of the command is automatically translated to "the value of the name variable". This doesnt happen in normal jsp-in my case in the real insert database command- where it takes the value of the parameter name as $name which doesnt mean anything in jsp, so it inserts it as it is.
I mean in jsp it replaces the name variable with its value that is $name in WAP but jsp cannot go any further and change this to the real value...or no? Dont know if you understand this, or if I am right, I still didnt figure anyway to try it, but what do you think? If not I give up!
Hi Anna,

  Sorry for the delay, I've been away for a few days.

  OK.  First I want to say nice one ;-)  Superb catch with the WAP browser interpretation of the returned $ references.

  I have a lot of time for people who learn quick and are genuinely trying, so please don't give up yet.  I'm with you all the way on this until we sort it out :-)

  The second thing is that according to the following reference, the variable references in the WML should definitely appear as $(name) and $(mobile).

http://developer.openwave.com/htmldoc/41/wmlref/intro4.html#575696

  This may or may not make a difference at the moment (hopefully it will).  If this doesn't work, could you confirm whether the database ends up with "$(name)" in it or if it's something else (eg. "$name" without the parentheses).

  If you want to see what the JSP has actually received, you could do one of the following:

  1)  If you have set up the "Logger" inside your "Context" for the web application (in conf/server.xml), you could add the following to the JSP:

<%
    log("name = " + name);
    log("mobile = " + mobile);
%>

      Anywhere after the request.getParameter(...) will do.

  2) If you haven't set up logging, you could do a similar thing with System.out.println(...).  The output from this will go to a file called catalina.out in the Tomcat "logs" directory.

Ooh.  I just found another link with a page similar to yours that shows the formatting of the postfield elements.

Listing 2 on this page:

http://www.wirelessdevnet.com/channels/wap/training/wml6.html
Avatar of annasfe

ASKER

Ok, first, thanks for keep trying :)

I have the variables as they supposed to be, $(name) with parenthesis, in the database it is written also like that $(name) and tried the System.out thing and again in catalina log file it is written $(name). As always in the WAP browser screen I get the correct thing....
So it has to be this thing that I told you with the variable interpretation of jsp. The problem is how do I solve it? It has to be some way....I also thought (although I dont think it will make any difference) to put the insert.jsp file that does the database insert in the same file that sends the data as a different wml card and just pass the data from card to card. Still didtn try it, I will try it and let you know...

I saw the example that you told me, it is exactly like the one I want, the problem is that in asp the Request.parameter seems to work fine :(
>> Still didtn try it, I will try it and let you know...

Hi Anna.  How's it going?
Avatar of annasfe

ASKER

nothing :(
I had some trouble putting both files in the same file-different cards, so I kind of left it there. Anyway, as I saw it I would have had the same problem cause there was not much change really...Maybe I should switch to asp since most of the examples for wml and database stuff that I see is with asp :(
Hold on for a little while.  I'm just creating some test files to see what I get on my web server.  I'll let you know within the next hour whether it has worked or not (and I'll post the complete WML and JSP source if it turns out to be OK ;-))
Avatar of annasfe

ASKER

Whow! That's good news :) I will be away for the weekend but as soon as I come back I ll check to see if you have succeeded -really hope you will!! Thanks and good luck!
I've written the code (wml & jsp), but now my network operator's GPRS isn't working :-(

Probably best for you to check after the weekend.  Have a good one ;-)

Jim.
ASKER CERTIFIED SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of annasfe

ASKER

It doesnt work :'(

I tried all the changes you have in your files but still the same. First I changed my insert statement to  
String insertCommand = "insert into agenda (name, mobile) values ('" + request.getParameter("name") + "', '" + request.getParameter("mobile") + "')";
....but nothing.
Then I changed my first file that sends the data to make it have two cards, but still no difference.

Finally, I skipped the database thing and made my second file exactly like yours but still nothing.

I dont see any more differences, the only thing is that you are not using database transactions but your log is ok while mine is still $(name) $(mobile) so I guess it doesnt matter, when I can make the log file appear correctly I am sure I can make the database transaction ok too.

Here is my code as it is now:

agenda.wml
-------------------
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//OPENWAVE.COM//DTD WML 1.3//EN"
                "http://www.openwave.com/dtd/wml13.dtd" >
<wml>
<card id="agenda" title="Agenda">
 <p align="left">
      NAME: &nbsp;
      <input name="name" value=""/><br/>
 </p>
 <p align="left">
      MOBILE:
      <input name="mobile" value=""/><br/>
 </p>
 <p><a href="#card2">ParamTest</a></p>
  </card>
  <card id="card2" title="ParamTest">
    <p>About to send:</p>
    <p>name = $(name)</p>
    <p>mobile = $(mobile)</p>
    <do type="accept" label="SUBMIT">
      <go method="post" href="insert_contact.jsp">
            <postfield name="name" value="$(name)"/>
            <postfield name="mobile" value="$(mobile)"/>
     </go>
    </do>
  </card>
</wml>



insert_contact.jsp
---------------------
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//OPENWAVE.COM//DTD WML 1.3//EN"
                "http://www.openwave.com/dtd/wml13.dtd" >
<%response.setContentType("text/vnd.wap.wml");%>
<wml>
  <card id="card1" title="Response">
<%
System.out.println("Received: name = " + request.getParameter("name") + ", mobile = " + request.getParameter("mobile"));
%>
    <p>Received:</p>
    <p>Name: <%= request.getParameter("name") %></p>
    <p>Mobile: <%= request.getParameter("mobile") %></p>
  </card>
</wml>


As I told you, all the times in my WAP browser the data appear correctly while in the catalina log it appears $(name) $(mobile)
I dont see any differences with yours, do you?
Aww.  I'm sad now :'(   <sniff>

I thought I'd got it.

Never mind.  We'll keep going.

>>  when I can make the log file appear correctly I am sure I can make the database transaction ok too.

Absolutely right :-)

>> I dont see any differences with yours, do you?

No.  The code at both ends looks OK.

...

(There's a lot of frowning and head-scratching going on at the moment here...)

...

OK.  What do we know?

Well... for one thing, your input fields must be working correctly because your WAP browser is correctly displaying the values that you have entered.

The WML must be well formed, otherwise the WAP browser would reject it.


Mmm.  Alright then.  Things to double check (please don't take these questions the wrong way.  I just want to confirm that I haven't missed something):

When you modified the WML file and accessed it on your handset, did you "reload" the page or did you clear the cache?  - Just confirming that your WAP browser is definitely using the latest page.

Unfortunately, there are no timestamps in catalina.out.  Are you sure that the output that appears in it is from the latest JSP (ie. does it contain the whole text "Received: name = $(name), mobile = $(mobile)")?


If you want to try it, you could access my test page with your WAP browser and I could tell you whether I get the expected data.  http://www.ossmobile.co.uk/testsite/test.wml

I know this can work, so like the terminator, "I absolutely will not stop, ever, until we get the data" ;-)
Avatar of annasfe

ASKER

OK GOOD NEWS! (and some not so good but anyway...)

When I tried the same code that I sent you through a WAP mobile it worked!! I cant believe it, if I finally understand that it was all this time Nokia's simulator fault I will through away my nokia phone !! grrrrr!!!

Anyway, when I say worked, I mean that the catalina log was fine! Then I tried to put my old code with the database transaction but I got a server 500 error...well, still working on it but I have much more positive thinking now. :)

Now about your questions. I am pretty sure that in the Nokia simulator the page was reloaded ok cause when I was making small changes I could see them so it was not the cache one.Then in the catalina log I am also sure that it was writing the latest output cause I was not deleting the previous and I could see every time one new line, and at the end for example with the new format "Received :..."

So I will try to make the database work and keep you informed....I still cant believe it was the simulator's fault....
Never trust a simluator/emulator or any other ..ator ;-)  LOL.

Real devices always give you the actually functionality (and bugs ;-))
Avatar of annasfe

ASKER

You are totally right, I am still very mad with the stupid emulator.....
well, I give you the points although still I dont have it working totally but I am sure I am in the right way now (probably you deserve more for all this trouble you got into ;))

Thanks again and wait for my next question! (I am sure there are a lot to come :D)
I would have been happy to carry on here if I could have earned an 'A' ;-)

As I'm sure I've said before.  It's always a pleasure to help someone who is really interested and wants to know "how" and "why" ;-)

I await your questions :-)
Just for info:

It makes no difference to you (as a questioner) what grade you give.  In this case, for example, you spent 200 points, that's it and that's all ;-)

The grade does, however, make a difference to the person that answered the question.  A 'C' grade gives 2x points, a 'B' gives 3x points and an 'A' give 4x points.

(That's why I would have liked to have kept going for the 'A' grade ;-))
Avatar of annasfe

ASKER

Sorry, you are right, I also thought about it (but it was too late cause I had submited it and then I saw I cannot change it). And also I didnt know it makes this kind of difference what I put :(  (but I increased the points, I hope this makes it up a bit ;))

I promise next time will be definately an A :)

Thanks again a lot, I will post a final note when I have it working completely, just so that you know it worked out fine (or if not I will post a new question ;)
No problem really ;-)

;-)  Good luck.