Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

AAYUSHI MAURYA-02

Advanced Java Programming (AJP)

Practical No. 14: Write a program to demonstrate the use of

InetAddress class and its factory methods.

X. Program Code:
1. Execute the following code and write the output
import java.io.*;
import java.net.*;
public class InetDemo
{
public static void main(String[] args)
{
try
{
InetAddress ip=InetAddress.getByName("localhost");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}
catch(Exception e){System.out.println(e);}
}
}
ANS-

1|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions


1. Write any four differences between IPV4 and IPV6
ANS-

 IPv4 is 32-Bit IP address whereas IPv6 is a 128-Bit IP address.


 IPv4 is a numeric addressing method whereas IPv6 is an alphanumeric
addressing method.
 IPv4 binary bits are separated by a dot(.) whereas IPv6 binary bits are
separated by a colon(:).
 IPv4 offers 12 header fields whereas IPv6 offers 8 header fields.
 IPv4 supports broadcast whereas IPv6 doesn’t support broadcast.
 IPv4 has checksum fields while IPv6 doesn’t have checksum fields
 IPv4 supports VLSM (Variable Length Subnet Mask) whereas IPv6 doesn’t
support VLSM.

2. Write the use of getByName() and getAllByName() method.


ANS-
getByName() : Returns the IP Address of the host specified. If the host is a
literal IP address, then only its validity is checked.
getAllByName() : Returns an array of IP addresses for the given host

3. Write the steps to assign IP address to your machine


ANS-
The steps to assign IP address are-
1) Click Start >Settings >Control Panel.
2) On the control panel, double-click Network Connections.
3) Right-click Local Area Connection.
4) Click Properties.
5) Select Internet Protocol (TCP/IP), and then click Properties.
6) Select Use the Following IP Address.
7) Complete the IP address, Subnet mask, and Default gateway fields by
using the values in step 4 from Accessing the ASMI using a Web
Browser.
8) Click OK on the Local Area Connection Properties window. It is not
necessary to restart your PC.

2|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise
1. Develop a program using InetAddress class to retrieve IP address of
computer when hostname is entered by the user.
ANS-

3|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 15: Write a program to demonstrate the use of URL


and URLConnection class and its methods
X. Program Code:
1. Execute the following code and write the output
import java.net.*;
class URLDemo
{
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("https://www.javatpoint.com/javafx-tutorial");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Ext:" + hp.toExternalForm());
}
}

4|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions


1. Write the use of openConnection() method of URLConnection class.
ANS-
The openConnection() method of URL class opens the connection to
specified URL and URLConnection instance that represents a connection
to the remote object referred by the URL.
2. Write the name of exception that can be thrown by URL class
ANS-
A MalformedURLException is thrown when the built-in URL class
encounters an invalid URL; specifically, when the protocol that is provided
is missing or invalid.
3. Name the package in which URL class is defined.
ANS-
The package java.net contains classes and interfaces that provide a
powerful infrastructure for networking in Java. These include: The URL
class for basic access to Uniform Resource Locators (URLs).

5|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise
1. Write a program using URL class to retrieve the host, protocol, port and
file of URL http://www.msbte.org.in
ANS-

6|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write a program using URL and URLConnection class to retrieve the date,
content type, content length information of any entered URL
ANS-

7|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 18: Write a program to insert and retrieve the data
from database using JDBC
X. Program Code-
1. Write a Program to create a Student Table in database and insert a record in a
student table.
ANS-

8|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions

1. List the advantages of JDBC over ODBC?


ANS-
 JDBC applications enjoy the platform-independence of Java, which lends
itself to Internet applications. ODBC applications must, at a minimum, be
recompiled to run on a different operating-system/hardware combination.
 JDBC does not require software on each client system, which lends itself
well for Internet applications.
 JDBC is simpler and easier to learn than ODBC.
 JDBC is not primarily targeted for desktop application development, which
makes for faster implementation outside the Windows environment and is
frequently used in enterprise class applications.
2. Write the Use of Class.forName()?
ANS-
forName(String name, boolean initialize, ClassLoader loader) method returns
the Class object associated with the class or interface with the given string
name, using the given class loader. The specified class loader is used to load
the class or interface
3. Write the steps to establish DSN oriented connection and DSNLess
connection.
ANS-
Steps for DSN:
I. connect to a DSN
II. Write SQL statement that will query the database
III. Fetch the data from the database
IV. Close the connection
Steps for DSN Less:
1. Create an instance of the connection object
2. Define connection string, specify database driver
3. Write the SQL statement that will query the database
4. Continue with step 3 to 4 while there are records and move to next record
5. Close the connection and record set objects freeing up resources

9|P age
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. EXERCISE-
1. Develop a program to create employee table in database having two columns
“emp_id” and “emp_name”.
Ans-

10 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Develop a program to display the name and roll_no of students from “student
table” having percentage > 70.
ANS-

11 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

12 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 19: Write a program to demonstrate the use of


PreparedStatement and ResultSet interface.
X. Program Code-
1. Write a Program to update row of student table from MSBTE database using
Mysql 5.5 a database server.
ANS-

13 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write the output of following JDBC code. Use Mysql server 5.5 as database

14 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

server
ANS-

15 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions


1. Explain Advantages of Prepared Statement Interface.
Ans-
PreparedStatement objects are used to execute repetitive SQL statements.
Compared to Statement object execution, Prepared Statement object creation
is faster. The reason is the object is pre compiled, by eliminating the
compilation task by DBMS. The PreparedStatement object can be used by just
replacing the parameters.
2. Explain the methods of ResultSet Interface
Ans-
Commonly used methods of ResultSet interface

1) public boolean next(): is used to move the cursor to the one row next from
the current position.

2) public boolean previous(): is used to move the cursor to the one row previous
from the current position.

3) public boolean first(): is used to move the cursor to the first row in result set
object.

4) public boolean last(): is used to move the cursor to the last row in result set
object.

5) public boolean absolute(int is used to move the cursor to the specified row
row): number in the ResultSet object.

6) public boolean relative(int is used to move the cursor to the relative row number
row): in the ResultSet object, it may be positive or negative.

7) public int getInt(int is used to return the data of specified column index of
columnIndex): the current row as int.

16 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

8) public int getInt(String is used to return the data of specified column name of
columnName): the current row as int.

9) public String getString(int is used to return the data of specified column index of
columnIndex): the current row as String.

10) public String getString(String is used to return the data of specified column name of
columnName): the current row as String.
3. Explain Types of ResultSet
ANS-
There are two types of result sets namely, forward only and, bidirectional.
Forward only ResultSet: The ResultSet object whose cursor moves only in one
direction is known as forward only ResultSet. By default, JDBC result sets are
forward-only result sets

4. Explain disadvantages of Prepared Statements


ANS-
 Since a PreparedStatement object represents only one SQL statement at a
time, we can execute only one statement by one prepared statement object.
 To prevent injection attacks it does not allow more than one value to a place
holder

17 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise
1. Develop JDBC program to retrieve data using ResultSet
ANS-

18 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Develop a program to update a record in database table


ANS-

19 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

20 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 20: Write a program to update and delete a


record from a database table.
X. Program Code:

1. Write a program to delete a record from a table.


ANS-

21 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

22 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write the output of following JDBC code.

ANS-

23 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions

24 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

1. Write ACID properties of Transaction.


ANS-
In the context of transaction processing, the acronym ACID refers to the four
key properties of a transaction: atomicity, consistency, isolation, and
durability. All changes to data are performed as if they are a single operation.
2. Write the use of DDL DML and DCL
ANS-
DDL is Data Definition Language
DML is Data Manipulation Language
DCL is Data Control Language

Language Command List

Create
Drop
DDL Alter
Rename
Truncate

Select
Insert
DML
Update
Delete

Grant
DCL Revoke

3. Write the use of Delete Cascade.


ANS-
DELETE CASCADE: When we create a foreign key using this option, it
deletes the referencing rows in the child table when the referenced row is
deleted in the parent table which has a primary key.
4. Write the use of Update Cascade.
ANS-
UPDATE CASCADE: When we create a foreign key using UPDATE
CASCADE the referencing rows are updated in the child table when the
referenced row is updated in the parent table which has a primary key.

25 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. EXERCISE-

1. Develop a program to delete all record for a product whose "price is greater
than 500" and Id is "P1234".
ANS-

26 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 22: Write a Servlet program to send username


and password using HTML forms and authenticate the
User.
X. Program Code:
1. Write a Program to send the username to server and server will send the length
of username to client.

27 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

ANS-

28 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

29 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write the output of following code considering below HTML is front end and
servlet as back end

ANS-

30 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions


1. List the types of servlet and default port for there services.
ANS-
There are two main servlet types, generic and HTTP:
1) Generic servlets
 Extend javax.servlet.GenericServlet.
 Are protocol independent. They contain no inherent HTTP support or any
other transport protocol.
2) HTTP servlets
 Extend javax.servlet.HttpServlet.
 Have built-in HTTP protocol support and are more useful in a Sun Java
System Web Server environment.
 For both servlet types, you implement the constructor method init() and
the destructor method destroy() to initialize or deallocate resources.
2. List the difference between doGet() and doPost() method of servlet.
ANS-
DoGet DoPost
In doGet Method the parameters are In doPost, parameters are sent in separate
appended to the URL and sent along line in the body
with header information
Maximum size of data that can be sent There is no maximum size for data
using doget is 240 bytes
Parameters are not encrypted Parameters are encrypted
DoGet method generally is used to Dopost is generally used to update or post
query or to get some information from some information to the server
the server

31 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

DoGet is faster if we set the response DoPost is slower compared to doGet since
content length since the same doPost does not write the content length
connection is used. Thus increasing the
performance
DoGet should be idempotent. i.e. doget This method does not need to be idempotent.
should be able to be repeated safely Operations requested through POST can
many times have side effects for which the user can be
held accountable, for example, updating
stored data or buying items online.
DoGet should be safe without any side This method does not need to be either safe
effects for which user is held
responsible

3. Explain ServletConfig and ServletContext.


ANS-
ServletConfig is an object containing some initial parameters or configuration
information created by Servlet Container and passed to the servlet during
initialization. ServletConfig is for a particular servlet, that means one should
store servlet specific information in web.
ServletContext is created by the web container at time of deploying the
project. This object can be used to get configuration information from
web.xml file. There is only one ServletContext object per web application.
If any information is shared to many servlet, it is better to provide it from the
web.xml file using the <context-param> element.

4. Explain ServletInputStream Class and ServletOutputStream Class with


methods.
ANS-
ServletOutputStream is an abstract class in the javax.servlet package. It is
used in servlets to write the response to the client. ServletOutputStream class
extends java.io.OutputStream. It also defines the print( ) and println( )
methods, which output data to the stream.
The ServletInputStream class extends java.io.InputStream. It is
implemented by the servlet container and provides an input stream, that a
developer can use to read the data from a client request.

32 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise

1. Develop servlet program to retrieve data from List and Radio Button using
HTML forms.

ANS-

33 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

34 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Develop a program to receive student subject marks through HTML forms


TextField and send the response as passed or Failed in Examination...

ANS-

35 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

36 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

Practical No. 24: Write a Servlet program to implement session


tracking using Cookies.
X. Program Code:

1. Write a Program to Create Cookie.


ANS-

37 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

38 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

39 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

2. Write the output of following servlet code


ANS-

40 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

41 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

42 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

43 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XII. Practical Related Questions

1. Write the methods of Cookie.


ANS-
Cookie methods
 public int getMaxAge()
This method returns the maximum age of the cookie, specified in
seconds,By default, -1 indicating the cookie will persist until browser
shutdown.
 public void setDomain(String pattern)
This method sets the domain to which cookie applies, for example
msbte.com.
 public String getDomain()
This method gets the domain to which cookie applies, for example
msbte.com.
2. Write the advantages of Cookie over URL rewriting.
ANS-
Advantages with cookies are that it is supported by default by Java EE
containers, and does not have url breakage errors like that of URL rewriting.
However a browser may disable cookies for security and privacy reasons, in
which case, session management through cookies will not work.
3. Write steps to disable Cookie.
ANS-

44 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

XIII. Exercise

1. Develop a program to collect user information using cookie.

ANS-

45 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

46 | P a g e
AAYUSHI MAURYA-02
Advanced Java Programming (AJP)

47 | P a g e

You might also like