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

Posted on 2008-06-19
Medium Priority
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">
<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.
Question by:Madhu_A
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
  • 5
  • 3
  • 3
  • +2

Assisted Solution

sanjooz earned 150 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.

Author Comment

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.
LVL 11

Expert Comment

ID: 21831065
onClick call javascript,
 read the text box content,
 manuplate it .
assign it to field again.
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

LVL 11

Assisted Solution

Manish earned 300 total points
ID: 21831073
then submit form
LVL 11

Expert Comment

ID: 21831085
You can not manuplate on client side and print on console.

Author Comment

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!

Assisted Solution

gksinghiet earned 300 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:

Author Comment

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.
LVL 28

Expert Comment

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";
<dsp:setValue bean="Abean.details" paramValue="<%=details%>"/>

Author Comment

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.
LVL 28

Accepted Solution

rrz earned 750 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.
            function stringTrimmer(){
                              var str = document.myForm.details.value;
                             document.myForm.trimmedDetails.value =  str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
                             return true;
<form  name="myForm" onsubmit="return stringTrimmer();"  action="pageAtServer">
<input type="text" name="details" size="10"/>
<input type="hidden" name="trimmedDetails"/>
<input type="submit"/>

Author Closing Comment

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.

Featured Post

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!

Question has a verified solution.

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

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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Suggested Courses

770 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