java solution 2016 Batch pre board Answers

5th Semester

 2016 Batch



Asian





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.


 
8.





NCC

Group-B

Qno.2.



Source Code

Qno.3

Qno.4

Qno.5





Group C

Qno.6.
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:

 
Qno.7.

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

//header.jsp

<% @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

  • KEY_PRESSED
  • KEY_RELASED
  • KEY_TYPED

 

  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.

//index.html

//FirstServlet.java

//SecondServlet.java

 

  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.

//A.jsp

<% @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

% >

//error.jsp

<% @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.

<servlet-mapping>

<servlet-name>redteam</servlet-name>

<url-pattern>/red/*</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>blueteam</servlet-name>

<url-pattern>/blue/*</url-pattern>

</servlet-mapping>

Morgan

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

Cookie.java

ReadServlet.java

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.

Factorial.html

FactorialServlet.java

Hetauda

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.

Example

  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.

Example.
Index.html

Login.java

  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.

Example:

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

Example:

  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.

Calculator.jsp

CalculatorDemo.java

Orchid

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.





reverse.jsp

—————————-
reverse.html

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.

java_bean.jsp

————————————

SET A

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.
length.jsp

—————————-
length.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.
java_bean.jsp

————————————

IMS

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?

Servlet

JSP

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.

BorderLayout

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

CardLayout

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

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.

 

NCCS

Set B

 
Q.N. 2
Fact.html

Fact.java

Q.N. 3
Q3.html

Q3.java





Web.xml

Q.N. 4

Q.N. 6

Q.N. 7
Q7.jsp

Q7disp.jsp

Q7.java

Q.N. 8

Set A

Q.N. 2

Q.N. 3

Prime.java

Q.N. 4

Q.N. 6
Q6.html

Q6.java

Web.xml

Q.N. 7

Q7disp.jsp

Q7.java

Q.N. 8

KIST

Group B

2) Write a swing program to add two numbers given by user as input.

3) Write a program to display record from user_table that contains (id, username, password and role) inside a database kist_db.

4) Write a jsp to display  “Hello from jsp” 10 times.

5) Write a  servlet to find the factorial of a positive values.

 
6) Create an applet that contain three text field and two buttons (add and subtract). Perform add and subtract.

Group ‘c’
7)What layout manager? Explain border layout and grid bag layout.

  • The Layout Managers are used to arrange components in a particular manner. Layout Manager is an interface that is implemented by all the classes of layout managers.
    1. Grid bag layout
      The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle.
      Example:

  • Border layout

The Border Layout is used to arrange the components in five regions: north, south, east, west and center. Each region (area) may contain one component only.
Example:

8) What do you understand by event delegation model? How swing support  look and feel ?

  • 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.
    The Delegation Event Model has the following key participants namely:
  • Source– The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it’s handler. Java provide as with classes for source object.
  • Listener– It is also known as event handler. Listener is responsible for generating response to an event. From java implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is received,  the listener process the event an then returns.

“Look” refers to the appearance of GUI widgets and “feel” refers to the way the widgets behave.
Sun’s JRE provides the following L&Fs:

  1. CrossPlatformLookAndFeel:this is the “Java L&F” also known as “Metal” that looks the same on all platforms. It is part of the Java API (javax.swing.plaf.metal) and is the default.
  2. SystemLookAndFeel:here, the application uses the L&F that is default to the system it         is running on. The System L&F is determined at runtime, where the application asks the system to return the name of the appropriate L&F.
    For Linux and Solaris, the System L&Fs are “GTK+” if GTK+ 2.2 or later is installed, “Motif” otherwise. For Windows, the System L&F is “Windows”.
  3. Synth:the basis for creating your own look and feel with an XML file.
  4. Multiplexing:a way to have the UI methods delegate to a number of different look and feel implementations at the same time.
    For example:

SET-A

Group B

  1. Write a swing program in which user can select one of the option and display that selected option.

3) Write a program to set cookie.

4) Create jsp page to display all the odd numbers between 10 and 50.

5) Create applet having <para> tag.

Group C

7) Describe any two layout.
1) Flow layout: The Flow Layout is used to arrange the components in a line, one after another (in a -flow).  Flow Layout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel.
2)  Box layout:The Box Layout is used to arrange the components either vertically or horizontally. For this purpose, Box Layout provides four constants. 
8) Difference between JSP and servlet. Explain the use of web.xml with an example.

SERVLET JSP
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

 

Thames

Qn.1) Write a program to illustrate how to use session state in servlet.

Qn2) Write a program where user can seleclt one of the option and display the selected option.

Qn3) Repeated from past qn of 2016
Qn. 4) What is adapter class? Explain the advantages and disadvantages of adapter class over listener interfaces with suitable examples.

Adapter class is a simple java class that implements an interface with only EMPTY implementation Instead of implementing interface if we extends Adapter class ,we provide implementation only for require method. An adapter class provides the default implementation of all methods in an event listener interface. If you inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces. So it saves code. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.
Advantages:
• Assists unrelated classes to work together.
• Provides a way to use classes in multiple ways.
• Increases the transparency of classes.
• Its provides a way to include related patterns in a class.

Disadvantages:
• All requests are forwarded, so there is a slight increase in the overhead.
• Sometimes many adaptations are required along an adapter chain to reach the type which is required.
• No new functionality can be added in adapter class.

Examples
The following examples contain the following Adapter classes:
• ContainerAdapter class.
• KeyAdapter class.
• FocusAdapter class.
• WindowAdapter class.
• MouseAdapter class.
• ComponentAdapter class.
• MouseMotionAdapter class.

Java MouseAdapter Example

Group C

1) Differentiate between jsp and servlet.explain the use of web.xml file with an example.

  • Writing code for servlet is harder than JSP as it is html in java. 1)JSP is easy to code as it is java in html.
  • Servlet plays a controller role in MVC approach. 2)JSP is the view in MVC approach for showing output.
  • Servlet is faster than JSP. 3) JSP is slower than Servlet because the first step in JSP lifecycle is the translation of JSP to java code and then compile.
  • Servlet can accept all protocol requests. 4) JSP only accept http requests.
  • In Servlet, we can override the service() method. 5) In JSP, we cannot override its service() method.
  • In Servlet by default session management is not enabled, user have to enable it explicitly. 6)In JSP session management is automatically enabled
  • Servlet is a java code.
  • JSP is a html based code.

Web.xml file is the configuration file of web applications in java.
It instructs the servlet container which classes to load, what parameters to set in the context,
and how to intercept requests coming from browsers.

2) Differentiate between applet and swing. explain the border layout manager.

  • Swing is light weight component while applet is not.
  • Applet have no main method, but swing have.
  • Swing have have look and feel while Applet Does not provide this facility.
  • Swing uses for stand lone Applications while Applet need HTML code for Run the Applet.
  • Swing have its own Layout while Applet uses Awt Layout.

The border layout is used to arrange the component in five regions:
north,south,east,west&center. eacch region may contain one component only.border layout creates a border layout but no gap between components. It is the default layout of frame or window.

NCIT

SET A

3.

  1. Keyboard event handling by applet

     

5.

6.

7.

Set B

2.
Table.jsp

Table2.jsp

3.

4.

5.

6.

7.
INDEX.JSP:

Servlet:

 

Leave a Reply

Your email address will not be published. Required fields are marked *