• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass A String and Write Out Its Value in JSP

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My servlet has a String called 'category'. I want to pass the String to a JSP. Therefore, I have the code shown below in the servlet:

And, I do the following in my JSP:

How do I write out keyValue in my JSP in Struts?
 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JiaPei Jen:


How do I write out keyValue in my JSP in Struts?[/QB]



JiaPei,
In stead of above scriptlet in your JSP , you can do this.
1) <bean efine id="keyValue" name="c" scope="request" type="java.lang.String" />
whiis is equivalent to
<%String keyValue=(String)request.getAttribute("c");%>
2) then , u can display KeyValue like this..
<bean:write name="keyValue/>
HTH
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Manasa, for showing me how to write out a String, a property in a bean, a drop-down list, etc. Your instructions have always been very clear.
Could you help with another simple question? Say, I have a String in JSP. The String is not passed from a Servlet. For example, the String is created in the JSP this way:

How do I write out 'username' in JSP within the Struts framework?
[ December 08, 2003: Message edited by: JiaPei Jen ]
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do I write out 'username' in JSP within the Struts framework?
[ December 08, 2003: Message edited by: JiaPei Jen ][/qb]<hr></blockquote>
JiaPei,
Glad to help you out.

Even in this case also, you can do the same thing ...
<bean:write name="username" />
just remember this.
whenevr you want to write something in JSP
u should use
1) bean:write tag

if u want to display a property from a bean
u should use-> <bean:write name="BEAN NAME" property="PROPERTY NAME" />
2) if u want to write display some string variables or something lik ethat
just <bean:write name="VARIABLE NAME" /> is enough

3) you want to display something from ApplicationResources.properties,
u shld use
<bean:message key="KEY FROM APPRESOURCES.PROPERTIES"/>
Most of the time, genreally we use above three cases..
Hope that helps!!
[ December 08, 2003: Message edited by: manasa teja ]
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Manasa, I tried in my JSP:

However, I got this message in the browser: Cannot find bean username in any scope
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JiaPei Jen:
Hi, Manasa, I tried in my JSP:

However, I got this message in the browser: Cannot find bean username in any scope


try this once..
<bean efine id="username" type="java.lang.String" value=""/>
<%username=request.getRemoteUser();%>
<bean:write name="username" />
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried:

Now, the browser no longer gives a message indicating problems. However, nothing is written out; i.e. I printed out a blank.
There must be a value for 'username' because I use container-managed authentication. Is it because I have spaces in those <bean:write ... > and <% .... %> tags?
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JiaPei Jen:
I tried:

Now, the browser no longer gives a message indicating problems. However, nothing is written out; i.e. I printed out a blank.
There must be a value for 'username' because I use container-managed authentication. Is it because I have spaces in those <bean:write ... > and <% .... %> tags?


I am not sure, why you are not getting the value.
instead of above three lines of code, try this..
The name of the user is: <%=request.getRemoteUser();%>
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Manasa. Thanks for your continuing help for several days. I am afraid that I have to try another way to print the String username = request.getRemoteUser(); in the JSP. I tried

but got the following in the browser:
^ An error occurred at line: 119 in the jsp file: /article/content/menu.jsp Generated servlet error: C:\jakarta-tomcat-4.1.18-LE-jdk14\work\Standalone\localhost\PracticeVersion\article\content\menu_jsp.java:239: illegal start of expression out.print(request.getRemoteUser() ;) ;
I tried something else - manually created a String in the JSP and print it out:

I got a blank from both <bean:write .....> tag. The browser shows:


The user name is:.
The role is:.


Well, how do I print a String in a JSP? It has taken a lot of effort from people around just to resolve this one littlel problem.
By the way, I was able to print the value of 'username' without problem if I do not use Struts tags:


[ December 09, 2003: Message edited by: JiaPei Jen ]
[ December 09, 2003: Message edited by: JiaPei Jen ]
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic