AJP Question and Answer Only
AJP Question and Answer Only
Answer: Servlet
Answer: init()
Answer: doGet()
Answer: HttpServletRequest
Answer: To process data from an HTML form using the POST method
Answer: ServletOutputStream
Answer: @WebServlet
12. What is the default HTTP method that a servlet handles if no specific
method is defined?
Answer: GET
Answer: destroy()
18. What method would you use to retrieve a parameter from a request in
a servlet?
Answer: getParameter()
19. Which of the following can be used to set the content type of the
response in a servlet?
Answer: response.setContentType()
Answer: web.xml
22. Which lifecycle method is called when the servlet is first loaded into
memory?
Answer: init()
25. Which interface provides methods for reading client data in servlets?
Answer: HttpServletRequest
26. How can servlets maintain state across multiple requests from the
same client?
Answer: response.sendRedirect()
31. Which of the following is the correct way to retrieve an attribute from
the request object?
Answer: request.getAttribute()
Answer: request.getSession()
36. How can you include the response of another resource in your
servlet’s response?
Answer: doDelete()
Answer: GET
45. Which method should be used to read form data submitted via POST in
a servlet?
Answer: request.getParameter()
46. What is a servlet filter primarily used for?
47. How do you retrieve a list of all the parameter names sent in a
request?
Answer: request.getParameterNames()
48. Which of the following describes the relationship between servlets and
JSP?
Answer: response.addCookie()
Answer: session.invalidate()
51. What type of data can be sent in the body of a POST request?
Answer: request.getHeader()
Answer: service()
Answer: doPut()
Answer: HttpSession
Answer: request.getCookies()
73. What is the behavior of a servlet when it is called for the first time?
Answer: It is loaded and initialized.
74. Which of the following can be used to create a custom error page in a
servlet application?
76. Which of the following can be used to forward a request to a JSP page
from a servlet?
Answer: session.getAttributeNames()
79. How do you specify an encoding type for a form submission in HTML?
**Answer:** service()
**Answer:** The HTTP method used for the request (GET, POST, etc.)
86. What is the default location for the `web.xml` file in a Java web
application?
**Answer:** /WEB-INF/web.xml
**Answer:** @WebServlet
92. Which interface allows you to read data sent by the client in a servlet?
**Answer:** HttpServletRequest
93. What type of data can be sent in the body of an HTTP POST request?
**Answer:** 200
97. Which method can be used to retrieve the context path of a servlet?
**Answer:** getContextPath()
**Answer:** service()
100. What happens if you call `response.sendError(404)` in a servlet?
**Answer:** response.addCookie(cookie)
**Answer:** session.invalidate()
**Answer:** request.getCookies()
**Answer:** HttpSession
110. How can you set the maximum age of a cookie in seconds?
**Answer:** cookie.setMaxAge(seconds)
111. What method do you use to retrieve the value of a specific cookie in
a servlet?
**Answer:** getCookie(name)
**Answer:** request.getSession(true)
116. Which method can be used to set the domain for a cookie?
**Answer:** cookie.setDomain(domain)
119. How can you ensure a cookie is only sent over secure connections?
**Answer:** cookie.setSecure(true)
**Answer:** Until the user explicitly logs out or the session is invalidated
**Answer:** cookie.setMaxAge(seconds)
122. Which attribute of a cookie defines the URL path for which the cookie
is valid?
**Answer:** Path
124. How can you prevent a cookie from being accessed through
JavaScript?
126. Which method would you use to obtain the session ID of a session?
**Answer:** session.getId()
127. What is the result of calling `response.sendRedirect("url")` after
adding a cookie?
129. What happens to cookies when the browser is closed, assuming they
are session cookies?
**Answer:** cookie.setSecure(true)
132. Which method is used to remove a cookie from the client's browser?
**Answer:** cookie.setMaxAge(0)
136. What HTTP status code is used to indicate that a resource has been
permanently moved?
**Answer:** 301
**Answer:** Filter
**Answer:** request.getRemoteAddr()
**Answer:** @WebInitParam
**Answer:** session.getAttributeNames()
**Answer:** request.getRequestDispatcher("/path").forward(request,
response)
**Answer:** Session
**Answer:** POST
**Answer:** servletContext.getInitParameter(name)
**Answer:** response.sendRedirect(url)
157. How can you add a custom error page for a specific error code in
web.xml?
**Answer:**
<error-page><error-code>404</error-code><location>/error.html</locati
on></error-page>
method return?
**Answer:** request.getAttribute(name)