Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

November 1, 2009

SCWCD related questions and answers

Which of the following statements is true regarding MyServlet?

import javax.servlet.*;
import javax.servlet.http.*:
import java.io.*;

public class MyServlet extends HttpServlet implements SingleThreadModel
{
String myName;

public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = res.getWriter();
myName = req.getParameter("name");
sayHello(out);
out.close();
}

public void sayHello(PrintWriter out) {
out.println("Hello " + myName);
}
}

A MyServlet is thread safe
B MyServlet is not thread safe because myName is an instance variable
C MyServlet is not thread safe because MyServlet implements SingleThreadModel.
D None of the above

Ans : Choice A is correct. An application is thread safe if it always behaves predictably regardless of the number of concurrent threads running in its process space. The simplest way to ensure that a servlet is thread safe is to implement the SingleThreadModel interface. By implementing this interface, the server guarantees that no more than one thread can execute the service(), doGet(), or doPost() method at a time for a particular servlet instance. This makes the servlet thread safe. Thus even if class MyServlet has instance variables, it is thread safe. Thus A is the correct choice and the other choices are incorrect.

http://www.javacertificate.net/scwcd_1.htm

No comments: