wood burning stoves 2.0*
The moose likes JSP and the fly likes If anybody can answer these two questions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "If anybody can answer these two questions?" Watch "If anybody can answer these two questions?" New topic
Author

If anybody can answer these two questions?

Sam Wang
Ranch Hand

Joined: Jul 17, 2001
Posts: 95
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" />


SCJP(2001)
jawwad ahmed
Ranch Hand

Joined: Dec 01, 2001
Posts: 179
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

Joined: Jul 17, 2001
Posts: 95
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

Joined: Aug 23, 2001
Posts: 356
Where are you setting your attributes ???


Shubhrajit
Sam Wang
Ranch Hand

Joined: Jul 17, 2001
Posts: 95
[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

Joined: Aug 23, 2001
Posts: 356
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

Joined: Aug 23, 2001
Posts: 356
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

Joined: Jul 17, 2001
Posts: 95
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

Joined: Jul 17, 2001
Posts: 95
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

Joined: Feb 20, 2001
Posts: 580
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, President<br />Carman Consulting, Inc.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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

Joined: Jul 17, 2001
Posts: 95
I know the reason.Thanks a lot.
I found ranch is very helpful for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If anybody can answer these two questions?