Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help on passing string  RSS feed

 
john can
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a.jsp
-------------------------------------------------------------------
<%! String aaa; %> //declaration
<%! String bbb; %> //declaration


Enter Text: <input type="TEXT" name="input" value="<%=aaa%>"> //text box for user to key in text
<input type=submit value="Submit" > //submits
<br>
You entered : <%=aaa%> //after submit been pressed, the same page is loaded, but result will be printed here

<% String ccc;
ccc = aaa;
out.println(ccc); //result will be also be printed here, but been casts to another param
%>

--------------------------------------------------------------------------

Explanation...
1) i want to create a jsp form, a simple one, where the page it-self have a input text box, can read the value, and print it out on the normal text.
Eg; it has a text box, reads from user to key in in the text box, and when the user press "Submit buttom" it'll print on the screen within the same page.

2) please help.

thanks
john
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, make sure you have the <form></form> tags in your code, and you define its method="POST" (POST is probably what you need) and action="a.jsp" (or whatever the jsp file name is).

Then, use the "request" variable and call its "getParameter( String parameter )" method, which takes the name of the HTML parameter/field that you are interested in.
In your case you want to get the value of your input field, which you named "input".

And then you can assign the value of ccc to be not aaa, but the value returned by that request.getParameter( "input" ) method.

So, now when user opens the page for the first time, "input" field will be given the value of String aaa (which in your case is "null" - you might want to change it to an empty string).
When "submit" is clicked, the value of the "input" field will be stored in the "request" object. When the page is loaded again "getParameter()" method will return that value - which is what the user had entered.

Andris
 
john can
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it something like ...


<% String showID = request.getParameter("input"); %>
<% String ccc;
ccc = aaa;
out.println(ccc);
%>

<form action="Host.jsp" method=post>
Enter Text: <input type="TEXT" name="show" value="">
<input type=submit value="Submit" >
<br>
You entered : <%= results.getString("aaa") %>
<br>You entered1: <%=aaa%>
</form>
 
john can
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.. above coding is wrong

is it something like ...
---------------------------------------------------------

<% String aaa = request.getParameter("show"); %>
<% String ccc;
ccc = aaa;
out.println(ccc);
%>

<form action="Host.jsp" method=post>
Enter Text: <input type="TEXT" name="show" value="">
<input type=submit value="Submit" >
<br>
You entered : <%= results.getString("aaa") %>
<br>You entered1: <%=aaa%>
</form>

.... i'm stuck.. help help
thanks
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
Did you get it working?
If not, what is "results.getString( "aaa" ) ?
Did you mean "request.getParameter( "show" )" instead?
It should work this way.

This is how it works. "request" is a somewhat "pre-defined" object of type HttpRequest for jsp pages. (As you study more about JSPs and servlets, you'll learn where it comes from.) This is the API for the HttpServletRequest. Its superclass ServletRequest has a "getParameter( String name )" method, which gets the value of the parameter (which is an HTML "object"?, like input text box, or any other HTML element) with the specified name from your request.

In your example, you have an input text box with name "show".
"getRequest( "show" )" will get whatever that box contains as you request more info from the server (i.e., when you press "submit" button).
The first time you access the page and call "String aaa = request.getParameter( "show" );" , the "show" box hasn't been created yet, so you get "null" for "aaa".
When you enter smth in that text field, and press "submit", the "getParameter( "show" )" returns whatever was entered in that field upon pressing "submit". This will be displayed as the page is accessed the second time, etc.

Does it make sense?

Andris
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YUGH.
Just use JSTL and ditch all that messy scriptlet code.
And while you're at it use a servlet to handle all that, doing only display code in JSP.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!