Silo - Tips - Summer 13 Examination Model Answer Subject Name Advanced Java Programming
Silo - Tips - Summer 13 Examination Model Answer Subject Name Advanced Java Programming
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
Page 1 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
Page 2 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
TCP UDP
It is reliable It is unreliable
It is use to transfer for large amont of data It is use to transfer for small amont of data
Page 3 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
2) Connection:
(Explanation-2 Marks)
Connection is a interface. It allows to establish connection between data source and application i.e
program written by the user. Connection interface helps by providing the different methods for
transactions and error handling. Different approaches can be used to establish a connection with
the database.
Page 4 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
ii) Write a servlet to store password accepted from client and display it.
(HTML code - 2 Marks, Servlet Program - 4 Marks)
HTML program:
<html>
<body>
<center>
<form name="Form1" method="post"
action="http://localhost:8080/examples/servlet/PostParametersServlet">
<B>Password</B>
<input type=textbox name="pass" size="25" value="">
<input type=submit value="Submit">
</body>
</html>
Servlet Program
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParametersServlet extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException
{
PrintWriter pw = response.getWriter();
String pvalue = request.getParameter(pass);
Page 5 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
pw.println(“Password is :” + pvalue);
}
pw.close();
}
Page 6 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
{
}
public void mousePressed(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void paint(Graphics g)
{
f = new Font("Times New Roman", Font.PLAIN, size);
setFont(f);
g.drawString(msg, 10, 60);
}
}
b) Write a program to demonstrate the use of Border layout which shows four buttons at four
sides of an applet with caption as East, West, North and South.
(Ccorrect logical steps - 6 Marks, syntax - 2 Marks)
// Demonstrate BorderLayout.
import java.awt.*;
import java.applet.*;
import java.util.*;
/* <applet code="BorderLayoutDemo" width=400 height=200>
</applet> */
public class BorderLayoutDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("NORTH"),BorderLayout.NORTH);
Page 7 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("EAST"), BorderLayout.EAST);
add(new Button("WEST"), BorderLayout.WEST);
}
}
ii) Protocol
(Explanation-2 Marks)
Protocol: it is set of rules that governance the communication between computers on a network.
Thease rule include guidelines that regulate the feture of a network like access method, allow
physical topologies, types of cabling and speed of data transfer etc.
Protocols like TCP, UDP, HTTP, IP
iii) HTTP
(Explanation-2 Marks)
HTTP: The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed,
collaborative, hypermedia information systems. HTTP functions as a request-response protocol in
the client-server computing model. A web browser, for example, may be the client and an
application running on a computer hosting a web site may be the server. The client submits an
HTTP request message to the server. The server, which provides resources such as HTML files
Page 8 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
and other content, or performs other functions on behalf of the client, returns a response message
to the client. The response contains completion status information about the request and may also
contain requested content in its message body.
Page 9 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
Page 10 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
Example.
try
{
String sql=”Select * from temployee” + “where Firstname=?” + “and Lastname=?”;
PreparedStatement p=connection.preparedStatement(sql);
}
Catch(SQLException e)
{
}
Page 11 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
ii) Tree
(Explanation-2 Marks)
A tree is a component that presents a hierarchical view of data. A user has the ability to
expand or collapse individual subtrees in this display. Trees are implemented in Swing
by the JTree class, which extends JComponent. Some of its constructors are shown here:
JTree(Hashtable ht)
JTree(Object obj[ ])
JTree(TreeNode tn)
JTree(Vector v)
The first form creates a tree in which each element of the hash table ht is a child node.
Each element of the array obj is a child node in the second form. The tree node tn is the
root of the tree in the third form. Finally, the last form uses the elements of vector v as
child nodes.
Page 12 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
2. Panel:
(Explanation -2 Marks)
The panel class is a subclass of Container. A Panel may be through of as component. Panel is the
superclass for Applet. When screen output is directed to an applet, it is drawn on the surface of a panel
object. In essence, a panel is window that does not contain a title bar, menu bar or border . This is why
you do not see this item when an applet is run inside a browser. Other component can be added to a
panel object by its add () method, (method of container). Once, these component have been added, you
can position and resize them manually using the setLocation (), setSize () or setBounds () methods
define by component class
URL Format
http://www.starwave.com/ or
http://www.starwave.com:80/index.html.
Page 13 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
PrepareStatement ()
The preparedStatement() method creates a PreparedStatement object and returns it as a return value.
The Prepared statement object is used to execute dynamic SQL statement against the database.A
dynamic SQL statement is a statement in which some of the parameters in the statement are unknown
when the statement is created. The parameter is placed into the SQL statement as they are determined
by the application. When all the parameters are specified for the SQL statement, the dynamic SQL
statement will be executed just as a static statement is executed. To create a dynamic SQL statement
that takes a first name and last name as parameters, use the following code:
try
{
String sql=”Select * from temployee” + “where Firstname=?” + “and Lastname=?”;
PreparedStatement p=connection.preparedStatement(sql);
}
Catch(SQLException e)
{
}
iv) Give steps to display icon on a label and a button using swing.
(Correct logical steps - 4 Marks)
Step 1. Create ImageIcon object using any of following constructor. ImageIcon(String filename)
ImageIcon(URL url) The first form uses the image in the file named filename. The second form uses
the image in the resource identified by url.
Step 2. Create JLabel object using any of following constructor. JLabel(Icon i) JLabel(String s, Icon i,
int align) Here, s and i are the text and icon used for the label. The align argument is either LEFT,
RIGHT, or CENTER. These constants are defined in the SwingConstants interface, along with several
others used by the Swing classes.
Step 3. Create JButton object using any of following constructor. The JButton class provides the
functionality of a push button. JButton allows an icon, a string, or both to be associated with the push
Page 14 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
button. Some of its constructors are shown here: JButton(Icon i) JButton(String s) JButton(String s,
Icon i) Here, s and i are the string and icon used for the button.
Step 4. Add the control JButton & JLabel into swing using add() method. The add( ) method of
Container can be used to add a component to a content pane. Its form is shown here: void add(comp)
Here, comp is the component to be added to the content pane
The javax.servlet package contains a number of interfaces and classes that establish the framework in
which servlets operate. The following table summarizes the core interfaces that are provided in this
package. The most significant of these is Servlet. All servlets must implement this interface or extend
a class that implements the interface.
1. Servlet : All servlets must implement the Servlet interface. It declares the init( ),
service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet. A
method is also provided that allows a servlet to obtain any initialization parameters. It eclares life
cycle methods for a servlet.
2. ServletConfig : The ServletContext interface is implemented by the server. It enables servlets
to obtain information about their environment. Allows servlets to get initialization parameters.
3. ServletContext : The ServletContext interface is implemented by the server. It enables servlets
to obtain information about their environment. Enables servlets to log events and access
information about their environment.
4. ServletRequest : The ServletRequest interface is implemented by the server. It enables a
servlet to obtain information about a client request. Used to read data from a client request.
5. ServletResponse : The ServletResponse interface is implemented by the server. It enables a
servlet to formulate a response for a client. Used to write data to a client response.
6. SingleThreadModel : This interface is used to indicate that only a single thread will execute
the service( ) method of a servlet at a given time. It defines no constants and declares no methods.
If a servlet implements this interface, the server has two options. First, it can create several
instances of the servlet. When a client request arrives, it is sent to an available instance of the
Page 15 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
servlet. Second, it can synchronize access to the servlet. Indicates that the servlet is thread safe.
import java.awt.*;
import java.applet.*;
Frame f;
MenuBar mnuBar;
Menu mnuFile,mnuEdit;
MenuItem mnuOpen,mnuNew,mnuSave;
MenuItem mnuCut,mnuCopy,mnuPaste;
mnuFile.add(mnuNew);
Page 16 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
mnuFile.add(mnuOpen);
mnuFile.add(mnuSave);
mnuEdit.add(mnuCut);
mnuEdit.add(mnuCopy);
mnuEdit.add(mnuPaste);
mnuBar.add(mnuFile);
mnuBar.add(mnuEdit);
f.setMenuBar(mnuBar);
f.setSize(250,250);
f.setVisible(true);
</applet>*/
Page 17 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
</applet>*/
JButton jb1;
contentPane.setLayout(new FlowLayout());
jb1.addActionListener(this);
contentPane.add(jb1);
Page 18 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
jbl.setIcon(new ImageIcon("image2.jpg"));
b) Write a program to make connectivity with the database using JDBC and display records in
table of database. Assume suitable data.
( Correct logical statement - 6 Marks, syntax - 2 Marks)
import java.sql.*;
import java.io.*;
public class JDBCDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:IIT");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Student");
System.out.println("Name \t\tRollNumber \t\t Percentage");
while(rs.next())
{
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
con.close();
}
catch(Exception e)
{
e.printStackTrace();}}}
c) Give steps to create simple servlet and also write program to demonstrate.
(Correct Steps – 4 Marks, any one program - 4 Marks)
Steps : 1) Write a java servet code ;
Steps 2) Compile Servet by setting classpath with servlet-api library.
Step 3 ) Copy the .class servlet file in web apps classpath directory.
Steps 4) Open the web.xml ,open it and add servlet tag and sevlet mapping tag.
Steps 5) Start web server( apche tomcat);
Steps 6) Browse servlet using java compatible browser like IE .
Example shows to start, create a file named WelcomeServlet.java that contains the following
program.
import java.io.*;
import javax.servlet.*;
public class WelcomeServletDemo extends GenericServlet
{
public void service(ServletRequest request,ServletResponse response) throws
ServletException,IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Welcome to Servlet");
pw.close();
}
}
Page 20 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
Page 21 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
import javax.swing.*;
</applet> */
Page 22 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
Container cp=getContentPane();
cp.setLayout(new BorderLayout());
int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
cp.add(jsp);
Page 23 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
Cookie is a small piece of information that is passed back and forth in the HTTP request and response.
Even though a cookie can be created on the client side using some scripting language such as
JavaScript, it is usually created by the server resource, such as a Servlet. The Cookies sent by the
server when the client requests another page from the same application. In Servlet programming a
cookie is represented by a Cookie class in the javax.servlet.http package. You can create a cookie by
calling the Cookie class constructor and passing two string objects: the name and value of the cookie.
For instance, the following code creates a cookie object called c1.The cookie has the name
“myCookie” and value of ”secret”:
Cookie c1=new Cookie(“myCookie”,”secret”);
You then can add the cookie to the http response using addCookie method of the
HTTPServletResponse interface: Response.addCookie(c1);
Page 24 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Create cookie.
response.addCookie(cookie);
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println(data);
pw.close();
Page 25 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming
OR
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>");
pw.close();
}
Page 26 of 26