• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

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

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.
  • 5
  • 3
  • 3
  • +2
4 Solutions
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.
Madhu_AAuthor Commented:
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.
onClick call javascript,
 read the text box content,
 manuplate it .
assign it to field again.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

then submit form
You can not manuplate on client side and print on console.
Madhu_AAuthor Commented:
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!
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:
Madhu_AAuthor Commented:
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.
>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%>"/>
Madhu_AAuthor Commented:
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.
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"/>
Madhu_AAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now