Solved

How to Print Contents of a text box in a JSP?

Posted on 2008-06-19
13
377 Views
Last Modified: 2013-11-24
This is a very very simple question and for some reason I am not able to solve it.

All I want for testing purpose if to provide a input textbox and then manipulate the input (say trim) and display it on the console using System.out.println and also assign to some bean in the back end.

I want something like this:
<input type="text" name="details" size="10">
<%=details.trim();%>
<dsp:setValue bean="Abean.details" paramValue="details"/>

DSP tag is a custom tag library so don;t worry about it. All I want is to print the input box contents and store it in request so that I can pass it to the bean without passing the whole form.

Thanks! Seek an immediate answer.
0
Comment
Question by:Madhu_A
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 9

Assisted Solution

by:sanjooz
sanjooz earned 50 total points
ID: 21827562
I am not sure what  exactly you are trying to accomplish.

However, do you realize that JSP is server side language? When your user types something in the text box, the only way to assign it to the bean is to send that text to the server. Either by submitting the form, or via an AJAX request.

Once the content comes to the server, you can assign it to a bean or print it to console.
0
 

Author Comment

by:Madhu_A
ID: 21828241
sanjooz, yes I know that JSP is server side.

I want to manipulate that text in the text box before sending to the server (assigning to the bean). Just give me an example of manipulating the contents of a textbox when the user submits a button (onclick --?), print it on console and then assign it to a bean (not sending the entire form)

I could tie a textbox to a bean variable directly but I wont be able to edit the contents of the text box.

By the way I am try to trim the text box contents (i.e. minus space ) before assigning it to the bean - because I do not want to touch the backend code to trim it.
0
 
LVL 11

Expert Comment

by:Manish
ID: 21831065
onClick call javascript,
 read the text box content,
 manuplate it .
assign it to field again.
document.formName.fieldName.value=youNewContentvalueAfterTrim;
0
 
LVL 11

Assisted Solution

by:Manish
Manish earned 100 total points
ID: 21831073
then submit form
document.formName.submit();
0
 
LVL 11

Expert Comment

by:Manish
ID: 21831085
You can not manuplate on client side and print on console.
0
 
LVL 27

Expert Comment

by:rrz
ID: 21831221
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!

 

Author Comment

by:Madhu_A
ID: 21833248
karanw, thanks for your input but there is a problem. The backend code does not take in the entire form, just uses the value assigned to the variables. So in javascript I cannot do form.submit, and I know in Javascript I cannot assign the trimmed value to the bean variable again!
0
 
LVL 5

Assisted Solution

by:gksinghiet
gksinghiet earned 100 total points
ID: 21833469
If you are using ATG's DSP tag library you can easily do by using dsp:input tag
<dsp:input type="text" id="TestInput" bean="/atg/userprofiling/ProfileFormHandler.name" paramvalue="testParam"/>

and at the form's onSubmit event or submit buttons onClick event do something like:
document.getElementById("TestInput").value=document.getElementById("TestInput").value.trim();
0
 

Author Comment

by:Madhu_A
ID: 21835482
gksinghiet:, I like your idea of calling onclick to trim the value. But are you sure the updated value (trimmed) will be sent to the bean variable..if it does my problem would be solved - I am gonna check it out.

Is there a way to do it in JSP code instead of calling onclick of JavaScript?

Thanks for the trim functions rrz@871311 and the JS help karanw.
0
 
LVL 27

Expert Comment

by:rrz
ID: 21836934
>Is there a way to do it in JSP code instead of calling onclick of JavaScript?
Yes, something like
<%
      String details = request.getParameter("details");
      if(details == null)details = "none sent";
      details.trim();
%>
<dsp:setValue bean="Abean.details" paramValue="<%=details%>"/>
0
 

Author Comment

by:Madhu_A
ID: 21838314
rrz@871311, how can we use JSP code before the submit button is pressed? setValue tag is a nice idea but before the submit button is pressed, would we able to trim the variable? If we can get that code to execute after the submit button is pressed then it would for sure! Thanks.
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 21839111
As sanjooz posted; JSP is at server.  My last code post would on the page that receives the request from your form.  
>how can we use JSP code before the submit button is pressed?  
You can't, unless you wanted to use AJAX to request the trimed string from the server. But that doesn't make sense.  
>but before the submit button is pressed, would we able to trim the variable?  
Yes, you can use  RegEx in Javascript. See the links I posted.  You could do something like the following. I didn't test this code. Ask us if you have troubles.
<script>
            function stringTrimmer(){
                              var str = document.myForm.details.value;
                             document.myForm.trimmedDetails.value =  str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
                             return true;
            }
</script
<form  name="myForm" onsubmit="return stringTrimmer();"  action="pageAtServer">
<input type="text" name="details" size="10"/>
<input type="hidden" name="trimmedDetails"/>
<input type="submit"/>
</form>
0
 

Author Closing Comment

by:Madhu_A
ID: 31468984
From your answers I confirmed that unless I use JavaScript or Ajax there is no way I can trim the variable (in the same page) unless i redirect and trim the variable in a second page.

Thanks for all your input.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

20 Experts available now in Live!

Get 1:1 Help Now