Computer Knowledge
Java Enterprise and Web Technologies
2,183 Questions
Java enterprise and web technologies questions focus on J2EE architecture, web services like SOAP, and servlet functionalities. These topics frequently appear in IT officer and specialist scale examinations. Regular practice ensures familiarity with enterprise application components.
HttpServlet methodsSOAP and web servicesEJB architecture rolesJ2EE componentsJSP servlet callingUDDI concepts
Java Enterprise and Web Technologies Questions
-
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
A
Correct answer
Explanation
HttpServletRequest.getParameter() retrieves request parameter values sent by the client (from query string or POST body). Option B is wrong because HttpServletResponse has no getAttribute() method - attributes are on requests, not responses. Option C is wrong because HttpServletResponse has no getParameter() - parameters belong to requests only. Option D is wrong because HttpServletRequest has no setParameter() method - request parameters are read-only once set by the client.
-
sendURL()
-
redirectURL()
-
sendRedirect()
-
getRequestDispatcher()
C
Correct answer
Explanation
HttpServletResponse.sendRedirect(String URL) is the standard method to redirect the client to a different URL. This sends a 302 temporary redirect response to the browser, which then makes a new request to the target URL. Options A and B are non-existent methods. Option D (getRequestDispatcher) is for server-side forwarding/dispatching, not client-side redirection - it forwards the request without the client knowing.
-
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.
D
Correct answer
Explanation
Servlets are inherently multi-threaded - one instance serves multiple requests concurrently. The recommended approach is to use local variables (thread-safe by design since each thread has its own stack) and avoid instance variables. If instance variables are necessary, synchronize access to them. Option A is wrong - no such class exists. Option B (SingleThreadModel) is deprecated and inefficient - it creates problems without solving thread safety. Option C (synchronizing service()) serializes ALL requests, causing severe performance bottlenecks.
-
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().
C
Correct answer
Explanation
HttpSession invalidation is managed by the servlet container based on a configurable timeout period (default typically 30 minutes). The container checks session inactivity and invalidates sessions that exceed the timeout. Option A is wrong - timeout of -1 means no timeout limit, but session can still be invalidated when server restarts or explicitly. Option B is wrong - closing browser doesn't immediately invalidate session; the session cookie persists until it expires or is cleared. Option D has the wrong method name - the correct method is HttpSession.invalidate() not invalidateSession().
-
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.
D
Correct answer
Explanation
Option D is FALSE (making it the correct answer to 'which is NOT true') because there is no such class as javax.servlet.http.CookielessHttpSession in the Servlet API. When cookies are disabled, containers use URL rewriting (appending the session ID to URLs) - they don't switch to a different HttpSession implementation class. Options A, B, and C are all TRUE statements: URL rewriting is a valid session tracking mechanism; SSL sessions can be used; and JSESSIONID is the standard cookie name (though containers can configure a different name).
A
Correct answer
Explanation
Session attributes are shared across all servlets within the same ServletContext (web application). When you store an attribute in an HttpSession using setAttribute(), any other servlet in the same application can retrieve it using getAttribute() on the same session (identified by the same session ID). This sharing is a key purpose of session attributes - to maintain user-specific state across multiple servlets/JSPs in the application. Different ServletContexts have separate session scopes.
-
req.getSession(false);
-
req.getSession(true);
-
both the above
-
none of the above
B
Correct answer
Explanation
HttpServletRequest.getSession(true) returns the current session or creates a new one if none exists. The boolean parameter 'create' controls this behavior: true means create if needed, false means return null if no session exists. Option A (getSession(false)) would return null instead of creating a session. The method getSession() with no arguments is equivalent to getSession(true) - it also creates sessions.
-
They should be used for data that changes rarely.
-
They can be accessed using ServletContext.getInitParameter()
-
They can be accessed using ServletContext.getParameter()
-
They should be used for data that is applicable to an entire web application.
C
Correct answer
Explanation
Option C is FALSE (making it the correct answer to 'which is NOT true') because ServletContext has no getParameter() method - the correct method is getInitParameter(). Options A, B, and D are all TRUE: ServletContext init parameters are configured in web.xml, meant for data that rarely changes (like database connection strings), are application-wide (shared by all servlets), and are accessed using getInitParameter(String name).getParameter() methods exist on HttpServletRequest, not ServletContext.
-
getMethod()
-
getHeader()
-
getCookies()
-
getInputStream()
D
Correct answer
Explanation
ServletRequest.getInputStream() is declared in the ServletRequest interface and returns a ServletInputStream for reading the request body as binary data. Options A, B, and C are wrong because getMethod(), getHeader(), and getCookies() are declared in HttpServletRequest (which extends ServletRequest), not in ServletRequest itself. ServletRequest contains fundamental methods like getParameter(), getAttribute(), getInputStream(), and getReader(), while HttpServletRequest adds HTTP-specific methods.
-
getServletInfo()
-
getInitParameters()
-
getServletConfig()
-
None
C
Correct answer
Explanation
In the init(ServletConfig) method, the ServletConfig object is passed as a parameter and can be accessed directly. The getServletConfig() method returns this stored config object, which is useful in other methods throughout the servlet lifecycle. getServletInfo() returns servlet information (not config), and getInitParameters() is not a standard method.
-
singleton
-
Prototype
-
request
-
response
D
Correct answer
Explanation
Spring defines five bean scopes: singleton (default), prototype, request, session, and global session. 'response' is NOT a valid scope in Spring - the option list includes singleton, prototype, and request, which ARE all valid Spring scopes, making 'response' the correct answer for what is NOT defined.
-
Application scope
-
Application Integration
-
Application Context
-
None
C
Correct answer
Explanation
The ApplicationContext is Spring's bootstrap mechanism that loads all high-level entry-point objects required by your application. It provides configuration for the application, reads bean definitions, and assembles them at runtime. Application scope is a bean scope, and Application Integration is not a standard Spring term.
-
Constructor Injection
-
Setter Injection
-
interface Injection
-
None
C
Correct answer
Explanation
Spring supports constructor injection (via dependencies in constructor), setter injection (via setter methods), and field injection (via annotations). Interface injection is NOT supported by Spring - it was part of early DI frameworks but Spring chose not to implement it, preferring the other three approaches.
-
Desktop & web Application
-
Desktop
-
Web Application
-
Other
A
Correct answer
Explanation
Java is widely used for both desktop applications (Swing, JavaFX) and web applications (servlets, JSP, Spring). The most complete answer includes both domains.
-
Data Access Object.
-
MVC
-
Business Delegate
-
Service Locator.