java solution 2016 Batch pre board Answers

5th Semester

 2016 Batch


2.Write a program passing parameter in applet.Write complete HTML file to include applet.

3.Explain two key swing features.

Swing offers two key features:

Swing components are lightweight and don’t rely on peers. Swing supports a pluggable look and feel. The three PLAFs available to all users are Metal (default), Windows, and Motif.

4.Write a swing program demonstrating GridLayout.

Long Answer Questions:

7.Write a program to insert data using JDBC in MYSql database called contact_db.





Source Code




Group C

Ans: Delegation model is the modern approach of handling the event that defines standard and consistent mechanism to create the event and control the event.
Source Code:


Ans: Servlet is the java program that generates dynamic web pages and extends the functionality of server.

The lifecycle methods of servlet are:

  1. init()
    The init() methods run once in a servlet that initialize the servlet object and variables.
  2. service()
    The service() methods deals with dynamic request of client and generate the dynamic web pages.
  3. destroy()
    The destroy method() is used before deleting the servlet from memory.

Deployment Descriptor
Web container uses XML based file to read the information about servlet and name the file as web.xml which is known as deployment descriptor.
//Structure of Deployment Descriptor


St. Xaviers

Group B

  1. Create a HTML document that contain the header information of page and include this HTML as a header file in JSP.

// index.html


<% @include file=”index.html” %>

  1. What is key event? How is it handled in java? Explain with suitable java source code.

On entering the character the Key event is generated. There are three types of key events which are represented by the integer constants. These key events are following



  1. How can you maintain session in servlet? Explain with example.

In such case, container creates a session id for each user. The container uses this id to identify the particular user. An object of HttpSession can be used to perform two tasks:

    • bind objects
    • view and manipulate information about a session, such as the session identifier, creation time, and last accessed time.





  1. Write a servlet program to find the prime number between 20 and 100.

  2. Write a swing program to insert data in table using prepared statement. (You can assume your own database, table, and number of field.

Group C

  1. How exception can be handled in JSP and forward to the “error.jsp”? Explain with suitable JSP script.

There are two ways of handling exceptions in JSP. They are:

    • By errorPage and isErrorPage attributes of page directive.
    • By <error-page> element in web.xml file.


<% @page errorPage = “error.jsp” %>

< %

String num1 = request.getParameter(“first”);

String num2 = request.getParameter(“second”);

// extracting numbers from request

int x = Integer.parseInt(num1);

int y = Integer.parseInt(num2);

int z = x / y; // dividing the numbers

out.print(“division of numbers is: ” + z); // result

% >


<% @page isErrorPage = “true” %>

<h1> Exception caught</ h1>

The exception is : <%= exception %>

  1. What are the advantages of JSP over servlet? Explain the use of web.xml with example.

There are many advantages of JSP over servlet. They are as follows:

  • Extension to Servlet

JSP technology is the extension to servlet technology. We can use all the features of servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP that makes JSP development easy.

  • Easy to maintain

JSP can be easily managed because we can easily separate our business logic with presentation logic. In servlet technology, we mix our business logic with the presentation logic.

  • Fast Development: No need to recompile and redeploy

If JSP page is modified, we don’t need to recompile and redeploy the project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application.

  • Less code than Servlet

In JSP, we can use a lot of tags such as action tags, jstl, custom tags etc. that reduces the code. Moreover, we can use EL, implicit objects etc.


web.xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method. For example: the doGet() method for HTTP GET requests.










XML Schema overcame all the limitations of DTD and had now become the standard for validating XML documents. The good thing about XML Schema is that it is closely associated with Object Oriented data models.

  1. What is web server?

Web server is a computer where the web content is stored. Basically web server is used to host the web sites but there exists other web servers also such as gaming, storage, FTP, email etc. Web server respond to the client request in either of the following two ways:

Sending the file to the client associated with the requested URL.

    • Generating response by invoking a script and communicating with database

Group B

2. Write a swing program to compute sum, difference and product of two numbers.

3.write an applet to check whether given word by user is of length 5 or not.

The HTML file for above applet is:

4.write a JSP program to compute sum of two numbers.

5. Write a program to set cookie in servlet.
Cookie.html file

6. Write a program to read the employee(eid,name,address,salary) from file and display.

Comprehensive Questions:

  1. Differentiate between FlowLayout and BorderLayout. Make user form with two textfield and two buttons.

FlowLayout: Flow layout manager is a default layout for applet and panel.  A flow layout arranges components in a left to right, much like lines of text in a paragraph. Components are laid out from the upper left corner, left to right and top to down. When no more components fit on a line, the next one appears on next line.

BorderLayout: The BorderLayout is used to arrange the components in five regions: north, south, east, west and center. Each region may contain one component only. It is the default of frame or window. The four sides are referenced to as north, south, east and west. The middle area is called the center. North, south, east and west are narrow fixed width regions whereas center is large region.

  1. Write a servlet program to compute factorial of a given positive number.



Short Answers

  1. Write a program which creates a menu (File->open, save, close) in frame.
  2. Explain the delegation event model with example.
  • Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism has the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events.


  1. Create a frame which contains a swing table with some data.
  2. What do you understand by Request Dispatching? Explain with example.
  • Request Dispatcheris an interface whose implementation defines an object which can dispatch the requestto any resources on the server. javax.servlet.RequestDispatcher interface is used to forward or include the response of a resource in a Servlet.


  1. Explain component and Container with example program.
  • A componentis an object having a graphical representation that can be displayed on the screen and that can interact with the user. Examplesof components are the buttons, checkboxes, and scrollbars of a typical graphical user interface.


  • A containeris a component which can contain other components inside itself


  1. Create an Application using swing package which will search the student in the database and display the record in the respective fields. [Database schema is as- Student (Sid, sname, address)].

  2. Create a login form (web) and validate the user from database. Assume your own database parameters.
  1. Create a Jsp simple calculator. The result should be processed by a servlet.



Short Answer Questions:

2.Write a program to insert 3 records into a table “book” having columns(id,name,author,publishor and price).Assume database name yourself.

3.Create a jsp that takes an integer from HTML and display its reverse.



4.Create an applet to draw a circle and a rectangle filled with different colors.

5.Create a swing program with menu bar,menu and a menu item.When menu item is clicked,display “hello” in a message dialog.

6.Create a servlet to read cookie from client and display the name and value associated with the cookie.

Long Answer Questions:

8.What is a java bean?Explain.Write a program to use java bean in jsp.

A Java Bean is a java class that should follow following conventions:

-It should have a no-argument constructor.

-It should be Serializable.

-It should provide methods to set and get the values of the properties, known as getter and                  setter methods.




Short Answer Questions

2.Create a swing program to add two numbers.

3.Write a program to update price of book to 1200 whose name is “Head First Java” into a table “book”.

4.Create an applet to draw a circle and a rectangle filled with different colors.

5.Create a jsp to find the length of given string provided from HTML.


6.Create a servlet to write cookie to user computer and store it for 24 hours.

Long Answer Questions:

  1. What is a java bean?Explain.Write a program to use java bean in jsp.

A Java Bean is a java class that should follow following conventions:
-It should have a no-argument constructor.
-It should be Serializable.
-It should provide methods to set and get the values of the properties, known as getter and                  setter methods.



Group B

  1. Make an user interface to take name and address of user?
  2. Make a user interface to take name and address of user?
  3. Make an applet to display name of your college?
  4. Write a jsp program to display multiplication table to 2 (from 1 To 10).

Group C

  1. Differentiate between JSP and Servlet?



A servlet is a server-side program and written purely on Java. JSP is an interface on top of Servlets. In another way, we can say that JSPs are extension of servlets to minimize the effort of developers to write User Interfaces using Java programming.
Servlets run faster than JSP JSP runs slower because it has the transition phase for converting from JSP page to a Servlet file. Once it is converted to a Servlet then it will start the compilation
Executes inside a Web server, such as Tomcat A JSP program is compiled into a Java servlet before execution. Once it is compiled into a servlet, it’s life cycle will be same as of servlet. But, JSP has it’s own API for the lifecycle.
Receives HTTP requests from users and provides HTTP responses Easier to write than servlets as it is similar to HTML.
We can not build any custom tags One of the key advantage is we can build custom tags using JSP API (there is a separate package available for writing the custom tags) which can be available as the re-usable components with lot of flexibility
Servlet has the life cycle methods init(), service() and destroy() JSP has the life cycle methods of jspInit(), _jspService() and jspDestroy()
Written in Java, with a few additional APIs specific to this kind of processing. Since it is written in Java, it follows all the Object Oriented programming techniques. JSPs can make use of the Javabeans inside the web pages
  1. Explain layout manager?
    • The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes of layout managers. There are following classes that represents the layout managers:
    • java.awt.BorderLayout
    • java.awt.FlowLayout
    • java.awt.GridLayout
    • java.awt.CardLayout
    • java.awt.GridBagLayout
    • javax.swing.BoxLayout
    • javax.swing.GroupLayout
    • javax.swing.ScrollPaneLayout
    • javax.swing.SpringLayout, etc.


Every content pane is initialized to use a BorderLayout. (As Using Top-Level Containers explains, the content pane is the main container in all frames, applets, and dialogs.) A BorderLayoutplaces components in up to five areas: top, bottom, left, right, and center. All extra space is placed in the center area. Tool bars that are created using JToolBar must be created within a BorderLayout container, if you want to be able to drag and drop the bars away from their starting positions


The CardLayout class lets you implement an area that contains different components at different times. A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays. An alternative to using CardLayout is using a tabbed pane, which provides similar functionality but with a pre-defined GUI.


FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row, starting a new row if its container is not sufficiently wide. Both panels in CardLayoutDemo, shown previously, use FlowLayout.



Set B

Q.N. 2

Q.N. 3


Q.N. 4

Q.N. 6