How to assign a string to an input field's content?

704 Views Asked by At

The string value is being requested using script tags but I'm not sure how to add the value of fibNum to the input field's content.

Does anyone know how to achieve this?

For example in java, the string variable's value can be added to a println statement as follows:

system.out.println("The value is: " + fibNum);

This is how I'm requesting the value, its then stored in a string variable called fibNum

<% String fibNum = request.getParameter("fibNum"); %>   
<input type="text" name="fibNum" size="40px" style="font-size:30pt;height:60px">
3

There are 3 best solutions below

0
On BEST ANSWER

If your are using it in general (with no association of any BEAN) then scriplets wiht solve your query, as given by @Roberto

<% String fibNum = request.getParameter("fibNum"); %>   
<input type="text" name="fibNum" value="<%=fibNum%>" size="40px" style="font-size:30pt;height:60px">

But if you are using it with some bean and want to associate it under some session, it's always better use EL and JSTL.

<input type="text" name="fibNum" value="#{managedBean.funcReturningParam}" size="40px" style="font-size:30pt;height:60px">

funcReturningParam is the method which processes the values, both from frontend and backend, and then sets the parameters.

managedBean used should preferably be the one with least security and debugging issues.

PS: Try to make a common function in any managed bean, which is to be used to get(set) values of most used parameters(variables) to be used and which are not associated with the DB Entities.

0
On

If what you want is to show the fibNum value on the input text, you can do that with scriplets (what you are already using to declare and initialize the variable):

<% String fibNum = request.getParameter("fibNum"); %>   
<input type="text" name="fibNum" value="<%=fibNum%>" size="40px" style="font-size:30pt;height:60px">

Although, it is always better to use a templating solution like JSTL or EL instead of Java scriplets.

1
On

I'm not really used to webforms or java but in ASP.NET MVC with C# you could do this

<input type="text" name="fibNum" size="40px" style="font-size:30pt;height:60px" value="@fibNum">

I guess it's the same aproach, just adapt it to the language you're using.