GeeCON Prague 2014*
The moose likes Struts and the fly likes Pass A String and Write Out Its Value in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Pass A String and Write Out Its Value in JSP" Watch "Pass A String and Write Out Its Value in JSP" New topic
Author

Pass A String and Write Out Its Value in JSP

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
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?
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
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


MT
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
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

Joined: May 27, 2002
Posts: 325

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

Joined: Nov 19, 2000
Posts: 1309
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

Joined: May 27, 2002
Posts: 325
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

Joined: Nov 19, 2000
Posts: 1309
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

Joined: May 27, 2002
Posts: 325
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

Joined: Nov 19, 2000
Posts: 1309
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass A String and Write Out Its Value in JSP