Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-19
13
Medium Priority
?
413 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
[X]
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
13 Comments
 
LVL 9

Assisted Solution

by:sanjooz
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.
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
Independent Software Vendors: 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!

 
LVL 11

Assisted Solution

by:Manish
Manish earned 300 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
 

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 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:
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 28

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 28

Accepted Solution

by:
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.
<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

Industry Leaders: 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

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…
A solution for Fortify Path Manipulation.
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.

604 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