Jee Q Ans Unit I
Jee Q Ans Unit I
SECTION-A (2 MARKS)
1. What is Servlet?
Java Servlets are programs that run on a Web or Application server and act as a
middle layer between a requests coming from a Web browser or other HTTP client
and databases or applications on the HTTP server.
2. What is a cookie?
A cookie is stored on a client and contains state information. Cookies are valuable
for tracking user activities.
5. What is a Session?
A web container provides a common storage area called as session to store the state
and provides access to this session to all the servlets within the web application. For
instance, servlet A can create some state (information) and store it in the session.
Servlet B can then get hold of the session and read the state.
8. Define SSI.
Server-Side Includes is a mechanism that helps developers insert dynamic content
into HTML files without requiring knowledge of the server or client-side
programming language specification.
SECTION-B (5 MARKS)
init() method: The server invokes the init( ) method of the servlet. This method is
invoked only when the servlet is first loaded into memory. It is possible to pass
initialization parameters to the servlet so it may configure itself.
service() method: The server invokes the service( ) method to process the HTTP
request.It may also formulate an HTTP response for the client.The service( ) method
is called for each HTTP request.
destroy() method: The server calls the destroy( ) method to relinquish any resources
such as file handles that are allocated for the servlet.
Since the state (data or information) in the session is user specific, the web container
maintains a separate session for each and every user.
.
A servlet can do the following four most important operations to work with sessions.
Creating a Session
The servlet API provides a class called HttpSession to work with sessions. To create a
session, the following session object is created:
session.setAttribute(“price”,new Double(“12.45”));
Double d = (Double)session.getAttribute(“price”);
session.invalidate();
5.
Type 3: JDBC-Net pure Java:
In a Type 3 driver, a three-tier approach is used to access databases. The
JDBC clients use standard network sockets to communicate with a
middleware application server. The socket information is then translated
by the middleware application server into the call format required by the
DBMS, and forwarded to the database server.
Type 4: 100% Pure Java
In a Type 4 driver, a pure Java-based driver communicates directly with
the vendor's database through socket connection. This is the highest
performance driver available for the database and is usually provided by
the vendor itself.
4. Discuss the Applet-Servlet Communication.
Applet-servlet communication
Applets can connect to a remote computer either by using socket or RMI programming,
but still there is an easier way—HTTP tunneling, which is a way of creating a sub
protocol for specific tasks on top of the existing protocol (HTTP). With the help of the
HTTP protocol, applets can establish a communication channel with servlets and send
and receive any text and binary objects using Java Serialization.
An applet can collect the user’s input from the GUI components, package the data in a
serializable object, and send the data to the servlet.
To receive data from a servlet, the applet has to play the role of a Web browser. It has
to understand the data it receives and populate the appropriate GUI components
The server then passes this information to the servlet in the normal way. Basically, the
applet pretends to be a web browser, and the servlet doesn't know the difference.
As far as the servlet is concerned, the applet is just another HTTP client.
The URLConnection contains both an input and an output stream. These streams can
be used by the applet to transmit data to the servlet and receive data back from it.
Example:
Apps.java
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
/*<applet code="Apps" width=400 height=460>
</applet>*/
public class Apps extends Applet implements
ActionListener
{
Button b;
TextField tf;
public void init()
{
b=new Button("call Servlet");
b.addActionListener(this);
add(b);tf=new TextField(25);
add(tf);
}
public void actionPerformed(ActionEvent ae)
{
try
{
URL u=new URL("http://localhost:8080/WebApplication11/Serv");
URLConnection urlc=u.openConnection();
InputStream isr=urlc.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(isr));
tf.setText(br.readLine());
}
catch (Exception e)
{
showStatus(e.toString());
}
}
}
Serv.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Serv extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws
IOException,ServletException
{
Date d=new Date();
PrintWriter out=res.getWriter();
out.println(d.toString());
}
}
SECTION-C (10 MARKS)
1. Discuss Servlet API in detail.
Two packages contain the classes and interfaces that are required to build servlets.
These are javax.servlet and javax.servlet.http. They constitute the Servlet API.
The javax.servlet package contains a number of interfaces and classes that establish the
framework in which servlets operate.
Interface Description
The javax.servlet.http package contains a number of interfaces and classes that establish
the framework in which servlets operate.
Class/Interface Description
HttpServletRequest The class that encapsulates all the HTTP request details
The steps used to access a database with JDBC can be described in six steps.
Class.forName ("org.postgresql.Driver");
Here, the url variable contains which JDBC driver is to used for this connection and also
which machine, by IP address, hosts the DBMS and the database.
String sql = "SELECT name, title " + "FROM faculty f, course c " + "WHERE f.id =
c.instructor";
ResultSet rs = st.executeQuery(sql);
String faculty = '123456';
String sql = "INSERT INTO faculty VALUES (" + '" + faculty +
"', 'Dave Letterman', 'Estate 1942', '941-6108')";
st.executeUpdate(sql);
rs.close();
st.close();