aspose file tools*
The moose likes Servlets and the fly likes Using the value of a String variable for a text input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using the value of a String variable for a text input" Watch "Using the value of a String variable for a text input" New topic
Author

Using the value of a String variable for a text input

Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Here's what I presume is a very basic question. I want to put the value of a String variable into text input. I know that if I wanted a literal value there
I could just use " value='XYZZY' " or the equivalent, but I don't know how to do this from a variable, and I have tried all manner of <%=output > variants without success.
Here's a fragment:


String output = frizzen( frazzen );
out.println( "<form method='post' action='/servlet/myServlet'>" );
out.println( " Text: <input type='text' name='text' size='50'><br><br>" );
out.println( " <input type='submit' value='submit'>" );

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

<%=output > -----change to----> *<%= output %>*

<edit>putting out.println in servlet and scriptlet(<%= %>) in jsp are poor practice!</edit>

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I just wanted to add a little more to this.

As previously eluded to, you should have your Servlet just set variables in the appropriate scope (in your case, probably Page or Request scope). Then place your HTML content in a JSP page and reference the scoped variables using the Expression Language (EL). Your servlet would then just do it's processing, setting scoped variables, then forward to the JSP page.

In the JSP page (which will basically be HTML with some tags sprinkled in) you can reference the variables using the EL. For example:


You can also place your variables in a JavaBean (Google to see what those are). Then one "bean" can hold all your variables, and you access them in a similar manner to that shown above: ${bean.myVar}.

Also, as your JSP page should never be accessed directly (it should only be forwarded-to by the servlet) you can place it under the WEB-INF directory. The Servlet will still be able to forward to it, but the page is not directly accessible from the outside.

I hope this helps.
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Thank you both.

My very next step after this will be my very first attempt at JSP, so you will have helped me with that step.

Since that HTML fragment comes directly from an assignment...

You will need to create a web page that contains the following HTML fragment:

<form method='post' action='/servlet/ReverseServlet'>
Text: <input type='text' name='text' size='50'><br><br>
<input type='submit' value='submit'>
</form>


...I won't take...

your way of programming is mess!


...personally.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Ed Connery wrote:Thank you both.

My very next step after this will be my very first attempt at JSP, so you will have helped me with that step.

Since that HTML fragment comes directly from an assignment...

You will need to create a web page that contains the following HTML fragment:

<form method='post' action='/servlet/ReverseServlet'>
Text: <input type='text' name='text' size='50'><br><br>
<input type='submit' value='submit'>
</form>
Woops, we're not supposed to do your homework for you. Perhaps you should forget what I said?
Ed Connery wrote:
...I won't take...

your way of programming is mess!


...personally.


Yeah, everyone here just wants to help you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Ed Connery wrote:I won't take.....personally.

Yeah, that could've been put better. I'd have said "You're code does not adhere to best practices".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Mark E Hansen wrote: Woops, we're not supposed to do your homework for you. Perhaps you should forget what I said?

Don't worry, you haven't done my homework for me. My servlet already does everything it was assigned to do. However, instead of putting the output on the line below the submit button I want to put it back in the input text field. I've looked at two books and numerous websites and I don't see any examples which don't simply use a literal for the value.

As I'm sure all of you know, the following code just assigns " <%output%> " as the value, it doesn't assign the contents of "output".



While some variant may be the answer, the following just displays " ${output} ".



The next assignment will use JSP and will put the output back in the input field. Perhaps this can't be done without JSP, but that seems unlikely.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
You don't use the expression language in the HTML content you spit out of a Servlet. You use the expression language in the JSP page.

Don't use the Servlet to spit out HTML at all. Use the JSP page for that. The Servlet should do your application logic and just set scoped variable which the JSP will access (using the EL).

Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
OK, I'll give up on this goal. It seems that either it can't be done or (more specifically) shouldn't be done.

The next assignment introduces JSP, and that's obviously the way to go.

Thanks for the feedback!
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Ed Connery wrote:OK, I'll give up on this goal. It seems that either it can't be done or (more specifically) shouldn't be done.

The next assignment introduces JSP, and that's obviously the way to go.

Thanks for the feedback!

Perhaps if you stated what the actual goal is? It seemed the goal was to have the servlet spit out HTML code, which shouldn't be done (in general). If you want to do that anyway (because the instructor just wants to see you do it before showing you a better way), then do it.

In the Servlet, you know the value you want to use for the input field, so just code it in there, like this:

... just for example. Note how we're just concatenating different string values to make the final HTML snippet to send to the client.

It is much better to do this in the JSP page. But, like I said, if you want to do it in the Servet, you can.

Also, note that any string values you send to the client are going to be interpreted as HTML (well, XML really) and so should have any XML-special characters escaped. There are tutorials on doing this too. In fact, the JSTL tags used in the JSP pages will handle this sort of thing for you automatically.

Good luck.
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Ah, the goal was stated all the way back in the first message, putting the value of output into that text field.

Your fragment works perfectly and I just needed to add "size" as follows:



So thanks again, and now I'll move along to learning how to do this in JSP.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ed Connery wrote:

So thanks again, and now I'll move along to learning how to do this in JSP.


If an example would help, we have one in our codebarn that is all bundled up as a war file and ready to drop into your servlet/JSP container.

See:
http://faq.javaranch.com/java/CodeBarnSimpleMvc


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Ben, I meant to thank you for the example. As it happens I have to do my JSP using ActionServlet and with the M and the C in one servlet, with the V in the JSP of course. Thanks to you folks I solved my problem on the HTML side (and today went back and modified it to handle double quotes), so I've moved on to smashing my forehead on the keyboard trying to solve the same problem with JSP. Ah, computing, it feels so good when we stop!
 
Don't get me started about those stupid light bulbs.
 
subject: Using the value of a String variable for a text input