servlets interview questions

1. What's the difference between applets and servlets?
A: There are many fundamental differences between Applet and Servlet classes, the Java API documentation for the two types will show you they have little in common. Applets are essentially graphical user interface (GUI) applications that run on the client side in a network environment, typically embedded in an HTML page. Applets are normally based on Abstract Windowing Toolkit components to maintain backward-compatibility with the widest range of browsers' Java implementations. The application classes are downloaded to the client and run in a Java Virtual Machine provided by the browser, in a restrictive security environment called a "sandbox". Servlets are used to dynamically generate HTTP responses and return HTML content to Web browsers on the server side. Servlets are often used to validate and process HTML form submissions and control a series of user interactions in what is known as a Web application. Servest can be used to control all aspects of the request and response exchange between a Web browser and the server, called a servlet container.

2. Do we open servlet classes directly instead of HTML?
A: Servlets are used to deliver HTML to Web browsers, but they are not like static HTML documents. When you set up a servlet in a Web application it has a URL like a static HTML document, so you can link to it, bookmark it or send the URL by email, just as you would with an standard Web page. The main difference is that the HTML sent to the Web browser is composed dynamically by the servlet and its contents can be customized based on the details of the request sent by the Web browser.
When you open a servlet URL the browser does not display content of the servlet class, but a dynamic HTML document created by the servlet. The servlet class is written as a standard Java class that extends the HttpServlet class. In its most basic form, the HTML output can be created by a series of print() statements on a PrintWriter. The method that handles simple Web requests is called doGet(), as below.
public final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter output = response.getWriter();
output.println("<html>");
output.println(" <head>");
output.println(" <title>");
// Other HTML output
output.flush();
output.close();
}

1 comment: