Servlet interview questions

Category: JAVA Questions    |    1 views    |    Add a Comment
  1. What is a servlet?
    Servlets are modules that extend request/response-oriented servers,such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database. Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical user interface.

  2. Whats the advantages using servlets over using CGI?
    Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. Servlets also address the problem of doing server-side programming with platform-specific APIs: they are developed with the Java Servlet API, a standard Java extension.
  3. What are the general advantages and selling points of Servlets?
    A servlet can handle multiple requests concurrently, and synchronize requests. This allows servlets to support systems such as online
    real-time conferencing. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.
  4. Which package provides interfaces and classes for writing servlets? javax
  5. What’s the Servlet Interface?
    The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more
    commonly, by extending a class that implements it such as HttpServlet.Servlets > Generic Servlet > HttpServlet > MyServlet.
    The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet.
  6. When a servlet accepts a call from a client, it receives two objects. What are they?
    ServletRequest (which encapsulates the communication from the client to the server) and ServletResponse (which encapsulates the communication from the servlet back to the client). ServletRequest and ServletResponse are interfaces defined inside javax.servlet package.
  7. What information does ServletRequest allow access to?
    Information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names
    of the remote host that made the request and the server that received it. Also the input stream, as ServletInputStream.Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and GET methods.
  8. What type of constraints can ServletResponse interface set on the client?
    It can set the content length and MIME type of the reply. It also provides an output stream, ServletOutputStream and a Writer through
    which the servlet can send the reply data.
  9. Explain servlet lifecycle?
    Each servlet has the same life cycle: first, the server loads and initializes the servlet (init()), then the servlet handles zero or more client requests (service()), after that the server removes the servlet (destroy()). Worth noting that the last step on some servers is done when they shut down.
  10. How does HTTP Servlet handle client requests?
    An HTTP Servlet handles client requests through its service method. The service method supports standard HTTP client requests by dispatching each request to a method designed to handle that request

Share/Save/Bookmark

 

Java applet interview questions

Category: JAVA Questions    |    2 views    |    Add a Comment
  1. What is an Applet? Should applets have constructors?
    - Applets are small programs transferred through Internet, automatically installed and run as part of web-browser. Applets implements functionality of a client. Applet is a dynamic and interactive program that runs inside a Web page displayed by a Java-capable browser. We don’t have the concept of Constructors in Applets. Applets can be invoked either through browser or through Appletviewer utility provided by JDK.
    1. What are the Applet’s Life Cycle methods? Explain them? - Following are methods in the life cycle of an Applet:
    2. init() method - called when an applet is first loaded. This method is called only once in the entire cycle of an applet. This method usually intialize the variables to be used in the applet.
    • start( ) method - called each time an applet is started.
    • paint() method - called when the applet is minimized or refreshed. This method is used for drawing different strings, figures, and images on the applet window.
    • stop( ) method - called when the browser moves off the applet’s page.
    • destroy( ) method - called when the browser is finished with the applet.

    1. What is the sequence for calling the methods by AWT for applets? - When an applet begins, the AWT calls the following methods, in this sequence:
    2. init()
    • start()
    • paint()
    1. When an applet is terminated, the following sequence of method calls takes place :
    2. stop()
    • destroy()
    1. How do Applets differ from Applications? - Following are the main differences: Application: Stand Alone, doesn’t need
      web-browser. Applet: Needs no explicit installation on local machine. Can be transferred through Internet on to the local machine and may run as part of web-browser. Application: Execution starts with main() method. Doesn’t work if main is not there. Applet: Execution starts with init() method. Application: May or may not be a GUI. Applet: Must run within a GUI (Using AWT). This is essential feature of applets.
    1. Can we pass parameters to an applet from HTML page to an applet? How? - We can pass parameters to an applet using <param> tag in the following way:
    2. <param name=”param1? value=”value1?>
    • <param name=”param2? value=”value2?>
    1. Access those parameters inside the applet is done by calling getParameter() method inside the applet. Note that getParameter() method returns String value corresponding to the parameter name.
    2. How do we read number information from my applet’s parameters, given that Applet’s getParameter() method returns a string?
      - Use the parseInt() method in the Integer Class, the Float(String) constructor or parseFloat() method in the Class Float, or the
      Double(String) constructor or parseDoulbl() method in the class Double.
    3. How can I arrange for different applets on a web page to communicate with each other?
      - Name your applets inside the Applet tag and invoke AppletContext’s getApplet() method in your applet code to obtain references to the
      other applets on the page.
    4. Can applets on different pages communicate with each other?
      - No, Not Directly. The applets will exchange the information at one meeting place either on the local file system or at remote system.
    5. How do I determine the width and height of my application?
      - Use the getSize() method, which the Applet class inherits from the Component class in the Java.awt package. The getSize() method returns the size of the applet as a Dimension object, from which you extract separate width, height fields. The following code snippet explains this:

      Dimension dim = getSize();
       int appletwidth = dim.width();
       int appletheight = dim.height();
    6. Which classes and interfaces does Applet class consist? - Applet class consists of a single class, the Applet class and three interfaces: AppletContext, AppletStub, and AudioClip.
    7. What is AppletStub Interface?
      - The applet stub interface provides the means by which an applet and the browser communicate. Your code will not typically implement this interface.
    8. What tags are mandatory when creating HTML to display an applet?
      1. name, height, width
      2. code, name
      3. codebase, height, width
      4. code, height, width

      Correct answer is d.

    9. What are the Applet’s information methods?
      - The following are the Applet’s information methods: getAppletInfo() method: Returns a string describing the applet, its author, copyright information, etc. getParameterInfo( ) method: Returns an array of string describing the applet’s parameters.
    10. What are the steps involved in Applet development? - Following are the steps involved in Applet development:
      • Create/Edit a Java source file. This file must contain a class which extends Applet class.
      • Compile your program using javac
      • Execute the appletviewer, specifying the name of your applet’s source file or html file. In case the applet information is stored in html file then Applet can be invoked using java enabled web browser.
    11. Which method is used to output a string to an applet? Which function is this method included in? - drawString( ) method is used to output a string to an applet. This method is included in the paint method of the Applet.

    Share/Save/Bookmark

     

    Java AWT interview questions

    Category: General Question    |    2 views    |    Add a Comment
    1. What is meant by Controls and what are different types of controls? - Controls are componenets that allow a user to interact with your application. The AWT supports the following types of controls:
      • Labels
      • Push buttons
      • Check boxes
      • Choice lists
      • Lists
      • Scroll bars
      • Text components

      These controls are subclasses of Component.

    2. Which method of the component class is used to set the position and the size of a component? - setBounds(). The following code snippet explains this:
      txtName.setBounds(x,y,width,height);

      places upper left corner of the text field txtName at point (x,y) with the width and height of the text field set as width and height.

    3. Which TextComponent method is used to set a TextComponent to the read-only state? - setEditable()
    4. How can the Checkbox class be used to create a radio button? - By associating Checkbox objects with a CheckboxGroup.
    5. What methods are used to get and set the text label displayed by a Button object? - getLabel( ) and setLabel( )
    6. What is the difference between a Choice and a List? - Choice: A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices. Only one item may be selected from a Choice. List: A List may be displayed in such a way that several List items are visible. A List supports the selection of one or more List items.
    7. What is the difference between a Scollbar and a Scrollpane? - A Scrollbar is a Component, but not a Container. A Scrollpane is a Container and handles its own events and performs its own scrolling.
    8. Which are true about the Container class?
      • The validate( ) method is used to cause a Container to be laid out and redisplayed.
      • The add( ) method is used to add a Component to a Container.
      • The getBorder( ) method returns information about a Container’s insets.
      • getComponent( ) method is used to access a Component that is contained in a Container.

      Answers: a, b and d

    9. Suppose a Panel is added to a Frame and a Button is added to the Panel. If the Frame’s font is set to 12-point TimesRoman, the Panel’s font is set to 10-point TimesRoman, and the Button’s font is not set, what font will be used to display the Button’s label?
      • 12-point TimesRoman
      • 11-point TimesRoman
      • 10-point TimesRoman
      • 9-point TimesRoman

      Answer: c.

    10. What are the subclasses of the Container class? - The Container class has three major subclasses. They are:
      • Window
      • Panel
      • ScrollPane
    11. Which object is needed to group Checkboxes to make them exclusive? - CheckboxGroup.
    12. What are the types of Checkboxes and what is the difference between them? - Java supports two types of Checkboxes:
      • Exclusive
      • Non-exclusive.

      In case of exclusive Checkboxes, only one among a group of items can be selected at a time. I f an item from the group is selected, the checkbox currently checked is deselected and the new selection is highlighted. The exclusive Checkboxes are also called as Radio buttons. The non-exclusive checkboxes are not grouped together and each one can be selected independent of the other.

    13. What is a Layout Manager and what are the different Layout Managers available in java.awt and what is the default Layout manager for the panel and the panel subclasses? - A layout Manager is an object that is used to organize components in a container. The different layouts available in java.awt are:
      • FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to right fashion.
      • BorderLayout: The elements of a BorderLayout are organized at the borders (North, South, East and West) and the center of a container.
      • CardLayout: The elements of a CardLayout are stacked, one on top of the other, like a deck of cards.
      • GridLayout: The elements of a GridLayout are of equal size and are laid out using the square of a grid.
      • GridBagLayout:
        The elements of a GridBagLayout are organized according to a grid.However, the elements are of different sizes and may occupy more
        than one row or column of the grid. In addition, the rows and columns may have different sizes.

      The default Layout Manager of Panel and Panel sub classes is FlowLayout.

    14. Can I add the same component to more than one container? - No. Adding a component to a container automatically removes it from any previous parent (container).
    15. How can we create a borderless window? - Create an instance of the Window class, give it a size, and show it on the screen.
      Frame aFrame = new Frame();
       Window aWindow = new Window(aFrame);
       aWindow.setLayout(new FlowLayout());
       aWindow.add(new Button("Press Me"));
       aWindow.getBounds(50,50,200,200);
       aWindow.show();
    16. Can I create a non-resizable windows? If so, how? - Yes. By using setResizable() method in class Frame.
    17. Which containers use a BorderLayout as their default layout? Which containers use a FlowLayout as their default layout? - The Window, Frame and Dialog classes use a BorderLayout as their default layout. The Panel and the Applet classes use the FlowLayout as their default layout.
    18. How do you change the current layout manager for a container?
      • Use the setLayout method
      • Once created you cannot change the current layout manager of a component
      • Use the setLayoutManager method
      • Use the updateLayout method

      Answer: a.

    19. What is the difference between a MenuItem and a CheckboxMenuItem?- The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.

    Share/Save/Bookmark

     

    Basic Java servlet interview questions

    Category: JAVA Questions    |    4 views    |    Add a Comment
    1. What is the difference between CGI and Servlet?
    2. What is meant by a servlet?
    3. What are the types of servlets? What is the difference between 2 types of Servlets?
    4. What is the type of method for sending request from HTTP server ?
    5. What are the exceptions thrown by Servlets? Why?
    6. What is the life cycle of a servlet?
    7. What is meant by cookies? Why is Cookie used?
    8. What is HTTP Session?
    9. What is the difference between GET and POST methods?
    10. How can you run a Servlet Program?
    11. What is the middleware? What is the functionality of Webserver?
    12. What webserver is used for running the Servlets?
    13. How do you invoke a Servelt? What is the difference in between doPost and doGet methods?
    14. What is the difference in between the HTTPServlet and Generic Servlet? Explain their methods? Tell me their parameter names also?
    15. What are session variable in Servlets?
    16. What is meant by Session? Tell me something about HTTPSession Class?
    17. What is Session Tracking?
    18. Difference between doGet and doPost?
    19. What are the methods in HttpServlet?
    20. What are the types of SessionTracking? Why do you use Session Tracking in HttpServlet?

    Share/Save/Bookmark