Important Web Design and Development Short Questions
Chapter: 31
Question 1:
What steps are involved in creating and sending a cookie to a browser?
Answer:
To create and send a cookie, first, a Cookie object is created with a name and value. Next, its attributes, such as expiration time, are set. Finally, the cookie is added to the HTTP response to be sent to the browser.
Chapter: 31
Question 2:
Describe the process of reading cookies from a client.
Answer:
To read cookies, the server retrieves them from the HttpServletRequest object using request.getCookies(). It then iterates through the array of cookies to find specific cookies by their name and use their values as needed.
Chapter: 31
Question 3:
What happens if a cookie with a specific name is not found in a client’s request?
Answer:
If a cookie with a specific name is not found in the client's request, it indicates either the client has not visited the site before or the cookie has expired. The server may then create a new cookie and send it to the client.
Chapter: 31
Question 4:
How does the ShoppingCartServlet manage user sessions and cookies?
Answer:
The ShoppingCartServlet manages sessions by creating a unique session ID stored in a cookie named "JSESSIONID". It uses this session ID to retrieve and store user-specific data, like selected books, in a global map on the server.
Chapter: 31
Question 5:
What is the role of the findCookie method in the ShoppingCartServlet?
Answer:
The findCookie method is used to check if a cookie named "JSESSIONID" exists in the client's request. It returns the cookie if found, allowing the servlet to identify and manage the user’s session.
Chapter: 31
Question 6:
How does the RepeatVisitorServlet differentiate between new and returning users?
Answer:
The RepeatVisitorServlet checks for a cookie named "repeat". If the cookie is found and its value is "yes", the servlet greets the user with "Welcome Back". If the cookie is not present, it creates a new cookie and greets the user with "Welcome Aboard".
Chapter: 32
Question 7:
What is URL rewriting and why is it used?
Answer:
URL rewriting is a technique used to maintain state and track sessions by including session information in the URL. It is used when cookies are not supported or disabled in the client’s browser.
Chapter: 32
Question 8:
How does URL rewriting work in session tracking?
Answer:
URL rewriting involves appending a unique session ID to the URL of each request. This session ID is used by the server to identify and retrieve session data associated with the client.
Chapter: 32
Question 9:
What is the main advantage of URL rewriting over cookies for session tracking?
Answer:
The main advantage of URL rewriting is that it does not rely on client-side storage (like cookies), making it useful for clients with cookies disabled or for tracking state in environments where cookies are not supported.
Chapter: 32
Question 10:
What are the typical steps involved in URL rewriting?
Answer:
The typical steps are: (1) Generate a unique session ID, (2) Append the session ID to URLs as a query parameter, (3) On each request, extract the session ID from the URL and use it to retrieve session data.