Questions and Answers
Q1) How would servlet code from a service method (e.g., doPost() ) retrieve the value of the “User-Agent” header from the request? Ans: String userAgent = request.getHeader(“User-Agent”); Q2) Which HTTP methods are used to show the client what the server is receiving? Ans: TRACE ( This method is typically used for troubleshooting, not for production) Q3) Which method of HttpServletResponse is used to redirect an HTTP request to another URL? Ans: sendRedirect() Q4) Which HTTP method are NOT considered idempotent? Ans: POST Q5) Given req is a HttpServletRequest, which gets a binary input stream? Ans: ServletInputStream inputStream = req.getInputStream() Q6) How would you set a header named “CONTENT-LENGTH” in the HttpServletResponse object? Ans: response.setHeader(“CONTENT-LENGTH”,”1024”); Q7) Write servlet code fragment that gets a binary stream for writing an image or other binary type to the HttpServlet...