AJ Answer Bank

You might also like

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

UNIT 1

1 Difference between TCP and UDP.


Transmission control User datagram
Basis protocol (TCP) protocol (UDP)

UDP is the Datagram-


oriented protocol.
TCP is a connection-oriented This is because there
protocol. Connection-orientation is no overhead for
means that the communicating opening a connection,
devices should establish a maintaining a
connection before transmitting connection, and
data and should close the terminating a
connection after transmitting the connection. UDP is
data. efficient for broadcast
and multicast types of
Type of Service network transmission.

The delivery of data


TCP is reliable as it guarantees
to the destination
the delivery of data to the
cannot be guaranteed
destination router.
Reliability in UDP.

TCP provides extensive error- UDP has only the


checking mechanisms. It is basic error checking
Error checking because it provides flow control mechanism using
mechanism and acknowledgment of data. checksums.

An acknowledgment segment is No acknowledgment


Acknowledgment present. segment.

There is no
Sequencing of data is a feature of sequencing of data in
Transmission Control Protocol UDP. If the order is
(TCP). this means that packets required, it has to be
arrive in order at the receiver. managed by the
Sequence application layer.
Transmission control User datagram
Basis protocol (TCP) protocol (UDP)

UDP is faster,
TCP is comparatively slower than
simpler, and more
UDP.
Speed efficient than TCP.

There is no
retransmission of lost
Retransmission of lost packets is
packets in the User
possible in TCP, but not in UDP.
Datagram Protocol
Retransmission (UDP).

TCP has a (20-60) bytes variable UDP has an 8 bytes


Header Length length header. fixed-length header.

Weight TCP is heavy-weight. UDP is lightweight.

It’s a connectionless
Uses handshakes such as SYN,
Handshaking protocol i.e. No
ACK, SYN-ACK
Techniques handshake

TCP doesn’t support UDP supports


Broadcasting Broadcasting. Broadcasting.

UDP is used
TCP is used by HTTP, by DNS, DHCP,
HTTPs, FTP, SMTP and Telnet. TFTP, SNMP, RIP,
Protocols and VoIP.

The TCP connection is a byte UDP connection is


Stream Type stream. message stream.

Overhead Low but higher than UDP. Very low.

2 WAP for server client connection using UDP.


3 WAP for server client connection using TCP.
4 Write an RMI application where client supplies two numbers
and server response by summing it. Provide your custom
security policy for this application
6 Write a note on the Protocol handler
 Handling a protocol means taking care of the interaction between a client and a
server: generating requests in the correct format, interpreting the headers that
come back with the data, acknowledging that the data has been received.

 Java divides the task of handling protocols into a number of pieces. As a result,
there is no single class called Protocol Handler. Instead, pieces of the protocol
handler mechanism are implemented by four different class es in the java.net
package: URL, URLStreamHandler, URLConnection, and
URLStreamHandlerFactory. URL is the only concrete class in this group.

 URLStreamHandler and URLConnection are both abstract classes, and
URLStreamHandlerFactory is an interface. Therefore, if you are going to
implement a new protocol handler, you have to write concrete subclasses for the
URLStreamHandler and the URLConnection. To use these classes, you may also
have to write a class that implements the URLStreamHandlerFactory interface.
7 Describe the use of URL class.
\

8 Explain the following classes with their use. i.


URLConnection class ii. DatagramSocket and DatagramPacket
class
9 Explain Socket, ServerSocket, InetAddress classes. Write a
java program to find an IP address of the machine on which
the program runs

Refer classes from previous answer


10 Difference between socket and server socket
UNIT 2
1 What is JDBC? WAP to get data from the user & save it into
the table.
2 Explain Statement in context of JDBC. Difference between
prepared & callable statements
CallableStatement PreparedStatement

It is used when the stored procedures are It is used when SQL query is to be executed
to be executed. multiple times.

You can pass 3 types of parameter IN, You can pass any type of parameters at
OUT, INOUT. runtime.

Used for the queries which are to be


Used to execute functions.
executed multiple times.

Performance is very high. Performance is better than Statement.

Used to call the stored procedures. Used to execute dynamic SQL queries.

It extends PreparedStatement interface. It extends Statement Interface.

No protocol is used for communication. Protocol is used for communication.

3 Retrieve data which is stored in the database table using


Resultset
<%
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/db",
"root","root");
Statement st=con.createStatement();

ResultSet rs=st.executeQuery("Select * from tbl");


%>
<table border="1">
<tr>
<th>id</th>
<th>fname</th>
<th>lname</th>
<th>delete</th>
</tr>
<%
while(rs.next())
{
int i=rs.getInt("id");
String fn=rs.getString("fname");
String ln=rs.getString("lname");
%>
<tr>
<td><%out.println(i);%></td>
<td><%out.println(fn);%></td>
<td><%out.println(ln);%></td>
<td><a href="4.jsp?id=<%=i%>">delete</a></td>
</tr>
<%}
%>
</table>
</scriplet>
4 Explain different types of JDBC drivers
5 What is a ResultSet in JDBC? How do you process a
ResultSet?
6 Explain JDBC Architecture.

7 What are the differences between Java Bean and basic java
class? Explain Java Bean Architecture
• Both classes must be public i.e accessible to all.
• Properties or variables defined in both classes must be private i.e. can't be
accessed directly.
• Both classes must have default constructor i.e no argument constructor.
• Public Getter and Setter must be present in both the classes in order to
access the variables/properties.
The only difference between both the classes is Java make java beans objects serialized
so that the state of a bean class could be preserved in case required.So due to this a
Java Bean class must either implements Serializable or Externalizable interface.
Due to this it is stated that all JavaBeans are POJOs but not all POJOs are JavaBeans.
Example of Java Bean Class.
public class Employee implements java.io.Serializable {
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}

Example of POJO Class.


public class Employee {
String name;
public String id;
private double salary;
public Employee(String name, String id,double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public Double getSalary() {
return salary;
}
}
8 Explain the use of CallableStatement and
PreparedStatement with example.
10 Explain ResultSetMetaData with suitable program.
11 Explain JDBC Transaction Management in detail
12 What is ResultSet interface. Write various method for
ResultSet interface. Write a code to update record using this
interface.
UNIT 3
1 What is a Session? How to set value and get value from
session?
Session simply means a particular interval of time.

Session Tracking is a way to maintain state (data) of an user. It is also known as session
management in servlet.
2 Explain Servlet life cycle.
3 Difference between servlet config and servlet context.
ServletConfig ServletContext

ServletConfig is servlet specific ServletContext is for whole application

Parameters of servletContext are present


Parameters of servletConfig are
as name-value pair in <context-param>
present as name-value pair in
which is outside of <servlet> and inside
<init-param> inside <servlet>.
<web-app>

ServletConfig object is obtained ServletContext object is obtained by


by getServletConfig() method. getServletContext() method.

Each servlet has got its own ServletContext object is only one and used
ServletConfig object. by different servlets of the application.

Use ServletConfig when only one


Use ServletContext when whole application
servlet needs information shared
needs information shared by it
by it.

4 What is filter API?


5 Explain advantages of Filter API?

1. Efficiency: The Filter API is designed to be efficient in terms of processing data. It


is faster than using loops or other methods to filter data.
2. Flexibility: The Filter API allows you to filter data in a variety of ways. You can filter
data based on a range of values, by certain criteria, or even by comparing one
data set against another.
3. Ease of Use: The Filter API is easy to use and can be implemented in a variety of
programming languages. The API is well-documented and there are many
resources available to help you get started.
4. Reusability: The Filter API can be reused across multiple projects. Once you have
implemented a filter, you can use it again and again.
5. Scalability: The Filter API is scalable and can handle large datasets without
sacrificing performance.

6 What is the difference between doGet() and doPost()


methods in Servlets?

GET method type and doGet() method

The doGet() method in servlets is used to process the HTTP GET requests. So,
basically, the HTTP GET method should be used to get the data from the server
to the browser. Although in some requests, the GET method is used to send
data from the browser to the server also. In this case, you need to understand
the below points on how the GET method will work.
• The data that is being submitted to the server will be visible in the URL
using query parameters like this
“http://localhost:8080/HelloServlet/hello?myParam=myValue”.
• So, if you are sending any sensitive information like passwords, you
should not use the GET method as the data entered can be clearly
visible in the browser URL.

POST method type and doPost() method

The doPost() method in servlets is used to process the HTTP POST requests. It
is used to submit the data from the browser to the server for processing. The
data submitted with POST method type is sent in the message body so it is
secure and cannot be seen in the URL. And there is no limit on the data that
can be sent through the POST method. Ideally, we need to use the POST
method, to send the form data to the webserver. So, we will be using the
doPost() method in this example. And we will learn how to handle some of the
common HTML fields data such as Text field, Checkbox, Radio button,
Dropdown, etc., values in the servlet.
To achieve this, Java provides ServletRequest interface.

7 Discuss the use of GET and POST with example.


Servlet - Form Data - GeeksforGeeks
8 What is Filter? List the applications of filter.
Refer from question 5
9 Explain Request and Response object in Servlet
Servlet Request and Response objects | H2kinfosys Blog

10 Write a Java Servlet to demonstrate the use of Session


Management.
UNIT 4
1 What is JSP? Explain Life cycle of JSP.
jsp stands for java server pages.It is a server side scripting language.
2 Write a program to create a java class and call it in the JSP
page.
3 Explain Core tag library.
4 What is the difference between a Servlet and a JSP? When
would you use one over the other?
Servlet JSP

Servlet is a java code. JSP is a HTML-basedcompilation code.

Writing code for servlet is harder than JSP


JSP is easy to code as it is java in HTML.
as it is HTML in java.

Servlet plays a controller role in the ,MVC JSP is the view in the MVC approach for
approach. showing output.

JSP is slower than Servlet because the first


Servlet is faster than JSP. step in the JSP lifecycle is the translation
of JSP to java code and then compile.

Servlet can accept all protocol requests. JSP only accepts HTTP requests.

In Servlet, we can override the service() In JSP, we cannot override its service()
method. method.

In Servlet by default session management is


In JSP session management is
not enabled, user have to enable it
automatically enabled.
explicitly.

In Servlet we have to implement everything In JSP business logic is separated from


like business logic and presentation logic in presentation logic by using
just one servlet file. JavaBeansclient-side.

Modification in Servlet is a time-


consuming compiling task because it JSP modification is fast, just need to click
includes reloading, recompiling, JavaBeans the refresh button.
and restarting the server.

It does not have inbuilt implicit objects. In JSP there are inbuilt implicit objects.
Servlet JSP

There is no method for running JavaScript While running the JavaScript at the client
on the client side in Servlet. side in JSP, client-side validation is used.

Packages can be imported into the JSP


Packages are to be imported on the top of
program (i.e, bottom , middleclient-side, or
the program.
top )

It cannot handle extensive data processing


It can handle extensive data processing.
very efficiently.

The facility of writing custom tags is not The facility of writing custom tags is
present. present.

Before the execution, JSP is compiled in


Servlets are hosted and executed on Web
Java Servlets and then it has a similar
Servers.
lifecycle as Servlets.

Use Servlets when:

1. You need to handle low-level HTTP requests and responses.


2. You want to generate dynamic content based on user input.
3. You need to handle complex business logic and data processing.
4. You want to create custom servlets to handle specific tasks.

Use JSPs when:

1. You want to separate presentation logic from business logic.


2. You need to create dynamic web pages that can be easily maintained.
3. You want to use tag libraries and custom tags for modular code.
4. You want to use JSTL (JavaServer Pages Standard Tag Library) for advanced
functionality.

In general, Servlets are more suitable for handling low-level HTTP requests and
performing complex business logic, while JSPs are more appropriate for generating
dynamic web pages with a focus on presentation. However, both technologies are
powerful tools for developing web applications and can be used together to create a
robust, scalable solution.
5 Difference between @include and <jsp:include>

6 How do you pass data from a Servlet to a JSP page


Java
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"
%>
<table>
<c:forEach items="${sessionScope.data}" var="i">
<tr>
<td>${i.id}</td>
<td>${i.fn}</td>
</tr>
</c:forEach>
</table>
<a href="data">search</a>
</body>
</html>
Jsp
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"
%>
<table>
<c:forEach items="${sessionScope.data}" var="i">
<tr>
<td>${i.id}</td>
<td>${i.fn}</td>
</tr>
</c:forEach>
</table>
<a href="data">search</a>
</body>
</html>

7 Explain Use of scriptlet and expression tag. Explain it with an


example
Java Server Page (JSP) is a technology for controlling the content or
appearance of Web pages through the use of servlets. Small programs that are
specified in the Web page and run on the Web server to modify the Web page
before it is sent to the user who requested it
8 Explain Directives. Give its type and explain any one in detail
9 Explain JSP Object scope: (i) Page (ii) Request (iii) Session (iv)
Application with example.
10 Show the use of JSP page directive tag with its attributes
Refer ans 8
11 Show the use of JSP inbuilt objects: request and response,
with their use in application

You might also like