Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If anybody can answer these two questions?

 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One:
In jsp,if I set the attribute of the request,I will always get null at the
commit-target page.
[commit.jsp]
<form method="post" action="target_page.jsp">
<input type="hidden" name="test" value="<%=new Object()%>">
<input type="submit" name="Submit" value="Submit">
</form>
[target_page.jsp]
<%
Object obj = request.getAttribute("test"); //obj is always null!
%>
Two:
Also in jsp,what's different between
<jsp:useBean id="someBean" class="com.someClass" />
and
<jsp:useBean id="someBean" type="com.someClass" />
 
jawwad ahmed
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hI;
Well For no 1
ANS 1)
Use <%=requset.getParameter("test");%>
ANS 2)
The class attribute is mantatory.If u specify also type attriute in <jsp:usebean> tag then it will the class type will be type cast to that type.
Thanks
Jawwad Ahmed
[ August 22, 2002: Message edited by: jawwad ahmed ]
 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In
Thanks.
Another question:I write a filter(some code below).
In my log file,I find the attributeName is
not null,but all the attribute is null.The
same as the parameters and session's attribute.
Why?
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
HttpServletRequest req = (HttpServletRequest)request;
//get attributes
Enumeration enuAttr = req.getAttributeNames();
for(int i = 0;enuAttr != null && enuAttr.hasMoreElements();i++){
String attributeName = (String)enuAttr.nextElement();
Object attribute = req.getAttribute(attributeName);
//log the attribute in log file
}
}
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you setting your attributes ???
 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[commit.html]
<%
request.setAttribute("attr",new Object());
%>
[ August 22, 2002: Message edited by: Stub Wang ]
[ August 22, 2002: Message edited by: Stub Wang ]
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code should work fine .. it worked fine in the test region simulated by me


However I find that there are lots of strange things ....

1. Why are you initiatiating a new blank object and doing a toString to set an HTML element.
2. No where in the codes you have posted I saw attributes being set in request/session . So how come you are still getting some parameter names?
3. I did not get any idea what are you trying to accomplish
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My previous post looks funny ... since you have edited the coded you had posted earlier ....
Anyway ...

1. I guess it should be commit.jsp
2. When commit.jsp sends response to the browser, the scope of its request has ended and in the next page ( to which you submit the form ) a new request object will be formed. Hence you will not find either the attribute name/ or the attribute value which you had saved in the request of commit.jsp
3. Still I have no idea what r u trying to do
Originally posted by Stub Wang:
[commit.html]
<%
request.setAttribute("attr",new Object());
%>
[ August 22, 2002: Message edited by: Stub Wang ]
[ August 22, 2002: Message edited by: Stub Wang ]
 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First,I'm only study the request and session in jsp.It's for test!
Second,My question is in my log filter,I can get
the attribute or parameter name,but they are all
null.But in the target.jsp,I can get the parameter's value(can't get the attribute's value)
[commit.jsp]
<%request.setAttribute("test","ATTRIBUTE TEST");%>
<form method="post" action="target.jsp">
<input type="text" name="username" value="">
<input type="submit" name="Submit" value="Submit">
</form>
[target.jsp]
<%out.print(request.getParameter("username"));%>
//can get the parameter "username" 's value
<%out.print(request.getAttribute("test"));%>
//only get null
and in my log file
username = null
test = null
I don't know why
 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something wrong with my log filter.
Now I can get parameter's value!
But I can't get request's attribute.
How can I set the request's attribute??
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stub Wang:
[commit.html]
<%
request.setAttribute("attr",new Object());
%>
[ August 22, 2002: Message edited by: Stub Wang ]
[ August 22, 2002: Message edited by: Stub Wang ]

Are you putting JSP scriptlet tags in an HTML page? Shouldn't they be in a JSP page?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stub Wang:
First,I'm only study the request and session in jsp.It's for test!
Second,My question is in my log filter,I can get
the attribute or parameter name,but they are all
null.But in the target.jsp,I can get the parameter's value(can't get the attribute's value)
[commit.jsp]
<%request.setAttribute("test","ATTRIBUTE TEST");%>
<form method="post" action="target.jsp">
<input type="text" name="username" value="">
<input type="submit" name="Submit" value="Submit">
</form>
[target.jsp]
<%out.print(request.getParameter("username"));%>
//can get the parameter "username" 's value
<%out.print(request.getAttribute("test"));%>
//only get null
and in my log file
username = null
test = null
I don't know why


The reason that you cannot retrieve the value of the "test" attribute is because of its scope. You are setting the attribute in request scope. However, when you post back to target.jsp, that's a completely different request. If you want the value to persist between requests, use session scope.
 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the reason.Thanks a lot.
I found ranch is very helpful for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic