Tag: architecture
Questions Related to architecture
What are the support functions that the Container provide ____________?
-
Communication Support
-
Life-cycle Management
-
Multithreading support
-
All of the above
Which of the below statements are true considering MVC design pattern?
-
Model – Java Class, JSP, View – HTML, Controller – Servlet
-
Model – Java Class, View – JSP, Controller – Servlet
-
Model – Java Class, View – JSP, Container – Servlet
-
Model – Java Beans, View – JSP, HTML, Controller – Servlet, EJB
When you have non-idempotent request which Http method satisfy the requirement?
-
GET
-
POST
-
PUT
-
HEAD
To answer this question, the user needs to have a basic understanding of HTTP methods.
An HTTP method is a type of request that a client sends to a server to perform a specific action. HTTP defines several standard methods, each of which is designed to be used for different purposes.
A non-idempotent request is a request that changes the state of the server. This means that if the same request is made multiple times, the result may be different each time.
OPTIONS:
A) GET: This option is not correct. The GET method is used to retrieve data from the server and is considered idempotent. This means that multiple identical requests will return the same response every time.
B) POST: This option is correct. The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server. POST requests are non-idempotent, meaning that multiple identical requests will not produce the same response.
C) PUT: This option is not correct. The PUT method is used to replace all current representations of a target resource with the request payload. Like the POST method, PUT requests are non-idempotent.
D) HEAD: This option is not correct. The HEAD method is used to retrieve the headers that would be returned if the HEAD request's URL was instead requested with the GET method. The HEAD request is considered idempotent because it does not change the state of the server.
Therefore, the correct answer is:
The Answer is: B. POST
Which of the following statements are true?
-
HttpServletRequest getParameter(...) retrieves the parameter value from the request
-
HttpServletResponse getAttribute(...) retrieves the attribute value from the response
-
HttpServletResponse getParameter(...) retrieves the parameter value from the response
-
HttpServletRequest setParameter(...) sets the parameter value to the request
-
sendURL()
-
redirectURL()
-
sendRedirect()
-
getRequestDispatcher()
What is the recommended way to deal with servlets and thread safety?
-
Write servlet code to extend ThreadSafeServlet.
-
Have the servlet implement SingleThreadModel
-
Synchronize the service method of the servlet
-
Use local variables exclusively, and if you have to use instance variables, synchronize access to them.
AI Explanation
To answer this question, it is important to understand the concept of thread safety in the context of servlets.
Servlets in Java are designed to handle multiple requests concurrently. This means that multiple threads can access and execute the same servlet code simultaneously. Thread safety refers to ensuring that the servlet behaves correctly and consistently when accessed by multiple threads concurrently.
Now let's go through each option to understand why it is correct or incorrect:
Option A) Write servlet code to extend ThreadSafeServlet. This option is incorrect because there is no such class called "ThreadSafeServlet" in the Java Servlet API. Extending this hypothetical class would not provide any inherent thread safety to the servlet.
Option B) Have the servlet implement SingleThreadModel. This option is incorrect. The SingleThreadModel interface was introduced in earlier versions of the Servlet API to specify that a servlet can handle only one request at a time. However, it was deprecated in Servlet API version 2.4 and removed in version 3.0. Using SingleThreadModel is not recommended as it can cause performance issues and does not guarantee thread safety.
Option C) Synchronize the service method of the servlet. This option is incorrect. Synchronizing the service method of the servlet would ensure that only one thread can execute the service method at a time, but it would not guarantee thread safety for the entire servlet. Other servlet methods, such as doGet(), doPost(), etc., can still be executed concurrently.
Option D) Use local variables exclusively, and if you have to use instance variables, synchronize access to them. This option is correct. The recommended way to deal with servlets and thread safety is to use local variables exclusively. Local variables are thread-safe because each thread has its own stack frame, and local variables are stored on the stack. However, if you need to use instance variables, you should synchronize access to them. This can be done using the synchronized keyword or other thread-safe mechanisms, such as using atomic variables or locks.
The correct answer is option D. This option is correct because it promotes the use of local variables, which are inherently thread-safe, and suggests synchronizing access to instance variables to ensure thread safety.
Which statements about HttpSession objects are true?
-
A session whose timeout period has been set to -1 will never expire.
-
A session will become invalid as soon as the user closes all the browser windows.
-
A session will become invalid after a timeout period defined by the servlet container.
-
A session may be explicitly invalidated by calling HttpSession.invalidateSession().
AI Explanation
To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) A session whose timeout period has been set to -1 will never expire. This option is incorrect because a session whose timeout period has been set to -1 will never expire. By default, the timeout period for a session is defined by the servlet container and can be configured in the deployment descriptor (web.xml) or programmatically. A timeout period of -1 means that the session will never expire.
Option B) A session will become invalid as soon as the user closes all the browser windows. This option is incorrect because a session does not become invalid as soon as the user closes all the browser windows. The session may still be active and available on the server even if the browser windows are closed. The session will only become invalid after a timeout period defined by the servlet container.
Option C) A session will become invalid after a timeout period defined by the servlet container.
This option is correct. A session will become invalid after a timeout period defined by the servlet container. The timeout period can be configured in the deployment descriptor (web.xml) or programmatically using the setMaxInactiveInterval()
method of the HttpSession
object. Once the timeout period is reached without any activity from the user, the session will be invalidated.
Option D) A session may be explicitly invalidated by calling HttpSession.invalidateSession().
This option is incorrect because there is no invalidateSession()
method in the HttpSession
class. To explicitly invalidate a session, you can use the invalidate()
method of the HttpSession
object. For example, session.invalidate()
will invalidate the current session.
The correct answer is Option C. A session will become invalid after a timeout period defined by the servlet container.
-
URL rewriting may be used by a server as the basis for session tracking.
-
SSL has a built-in mechanism that a servlet container could use to obtain data used to define a session.
-
When using cookies for session tracking, the name of the session tacking cookie must be JSESSIONID.
-
IF a user has cookies disabled in the browser, the container may choose to use a javax.servlet.httpCookielessHttpSession object to track the user's session.
-
True
-
False
-
req.getSession(false);
-
req.getSession(true);
-
both the above
-
none of the above