aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFSJ Final mock Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HFSJ Final mock Q" Watch "HFSJ Final mock Q" New topic
Author

HFSJ Final mock Q

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider the following:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
String url = res.encodeRedirectURL("/redirectme"); //Line 14
boolean test = "/redirectme". equals(url); //Line 15
res.sendRedirect(url);
}

Given answers:
1) Line 14 demonstrates a session management mechanism called URL rewriting.
2) After line 15, test could be either true or false.

I can't understand how test could be false after line 15.
S.L.Narayanan
Ranch Hand

Joined: Apr 01, 2005
Posts: 431
Hi

If u take response.encodeURL or response.encodeRedirectURl functions, they will append the session id to the URL only if Cookies are disabled. So before doing the actual rewriting, they will check whether Cookies are supported. So the boolean value in the given code can be true or false depending on whether Cookies are supported or not.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, NEXT--->IBM XML
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by S.L.Narayanan:
Hi

If u take response.encodeURL or response.encodeRedirectURl functions, they will append the session id to the URL only if Cookies are disabled. So before doing the actual rewriting, they will check whether Cookies are supported. So the boolean value in the given code can be true or false depending on whether Cookies are supported or not.


if the request for this servlet from the client is firt time then the value will be false becuase first time url-rewrite is turned on but if the request to this servlet is not the first in that session then it will be true if coockies are on...

if i am wrong pls correct me


Thanks and Regards, Amit Taneja
Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
Amit,

I want to agree with you because that is what HFSJ book says. But I tried the code sample in my Tomcat and it didn't add the jsessionid on the URL. I'm confused now.

Who knows this?

Thanks,
Jenny
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
The first time, a client requests for this servlet, the Container will use both Cookie and URL rewriting ( as you have used encodeRedirectURL for rewriting and cookie is the default for session management). i.e the container will send a cookie containing session id in the response header and also, it will append session id at the end of the url.

Now if the client has enabled cookies, then the cookie containing session id will come back in the request header and the Container will not do any URL rewriting for other requests as the Container knows that client supports cookies.

But, if the client has disables the cookies, then the request header in the next request will not contain any cookie header and the container now knows that it has to use URL rewriting, so wherever encodeURL or encodeRedirectURL is used it will append sessionid to those urls.

also, in case of sendRedirect, a second request is made by the client browser for the url specified in the redirect. so, if browser supports cookies, then you will not see session id appended at the end.
Sam Sun
Ranch Hand

Joined: Feb 01, 2005
Posts: 60
I just tested the code. My result is "true" only.

I disabled Cookie, but it still print "true". The result I expected is it should always be "true" when Cookie enabled, and "false" when Cookie disabled.
Please update me.
Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
Bruce,

How did you turn off the cookies exactly? I think it matters.
Sam Sun
Ranch Hand

Joined: Feb 01, 2005
Posts: 60
I did it like this:
open internet browser -> Tools -> internet options -> privacy -> Advanced ->override automatic cookie handling -> Block Block -> OK.

Hope that helps.
Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
Hi,

I did what you said block block and still getting the JSESSIONID cookie. I used the following code snippet to check it.


Is there anyway that we can really block the cookies?

I guess the block block is for some other kind of cookies, not the one being set in response???

Thanks,
Jenny
Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
Sorry about wierd spelling of cookie in my code snippet because of javaranch.

I have a simpler way to get that session cookie:

${cookie.JSESSIONID.value}

I tried to block block the cookie in my browser but ${cookie.JSESSIONID.value} still returns a session ID.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
I tried to block block the cookie in my browser but ${cookie.JSESSIONID.value} still returns a session ID.

But is it the same value if you refresh the page? If the JSP just created the session, as it always does when there's no session unless you disable sessions in the page directive or the DD, then there will always be some session ID.
Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
Yuriy,

I guess you are right.

Here is more findings:

If I use dispatcher forward to my 'view.jsp' page from servlet, I don't get the session id;

If I use redirect to 'view.jsp' page from servlet, I get the session id.

Any explanations?

Thanks!
Jenny
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFSJ Final mock Q