Professional Documents
Culture Documents
Open Book
Open Book
Open Book
Understanding Java EE
What is an Enterprise Application? What is java enterprise edition? Java EE
Technologies, Java EE evolution, Glassfish server.
Topics :
1.1 Understanding Java EE
1.2 Java EE Architecture, Server and Containers
1.3 Introduction to Java Servlets
1.4 Servlet API and Lifecycle
1.5 Working with Servlets
1.6 Working with Databases
1.1 UNDERSTANDING JAVA EE
1.1.1 Enterprise Application
Q.1 What is an Enterprise Application? Explain architecture of an enterprise
application.
OR
Explain the architecture of Java Enterprise application. [O-18]
Ans.: An enterprise applications are complex, distributed, scalable, component based
and mission critical business application.
It can be used on variety of platforms across internet, or extranets.
httprequest
Client EJB Container Database
httpresponse
EJB EJB
Java EE server
httprequest
Client EJB Container Database
httpresponse
EJB EJB
Working :
In this when a client sends a request to a web server, web server first checks
whether a client wants static response or dynamic response.
If client wants dynamic response then web server forwards the request to
CGI, to handle that request CGI creates CGI process and it will get loaded
inside the web server.
Now that CGI process is responsible for sending the dynamic response to a
client.
Once the client gets the response web server unloads CGI process to
release its memory.
This entire process will get repeated whenever a web server get a request for
dynamic response.
Disadvantages :
(i) CGI process are platform dependents because they are developed in C, C++
and pearl.
(ii) It increases the overhead of a web server.
(iii) It is difficult to implement.
(iv) Lack of scalability.
(v) It uses lots of web server resources.
(vi) lack of security.
Web container
Web server
In case of servlet, web server creates the servlets and load it inside the web
container.
When a particular client sends a request, first time for a particular servlet,
web server initialize that servlet using init( )
Once the servlet gets initialized, it accept the request and provide the
dynamic response to a client using service( )
This process will get repeated, whenever a client send a request for a
particular servlet.
At one stage when there is no any request for a particular servlet, in that
case a web server destroys that servlet using destroy( )
The destruction can be done, so that the memory occupied by that servlet
will get released.
Initialisation and destruction can be done by the web server, but service is
provided by servlet itself.
Initialisation and destruction can happen only once, but service will happen
many times whenever servlet get the request.
(2) ServletRequest :
It is used to extract the required information from the HTTP request.
(3) ServletResponse :
It is used to create or form a HTTP response so that it can be given
to client.
(4) Servletconfig :
It is used to configure or initialize a servlet.
(5) ServletContext :
It is used to provide the information about the environment of the
servlet i.e. web server of the servlet.
Classes :
(1) GenericServlet :
It is derived from Servlet interface.
It is used to create servlet.
(2) ServletInputStream :
It is used to accept a HTTP request from a client.
(3) ServletOutputStream :
It is used to send a HTTP response to a client.
(4) ServletException :
It is used to handle the exception generated in servlet application.
javax.servlet.http :
Interfaces :
(1) HttpServletRequest :
It is derived from ServletRequest interface.
It is used to extract required information from the HTTP request.
(2) HttpServletResponse :
It is derived from ServletResponse interface.
It is used in create or form a HTTP response so that it can be sent to
the client.
(3) HttpSession :
It is used to create a session between client and the servlet.
Classes :
(1) HttpServlet :
It is derived from GenericServlet class.
It is used to create a servlet.
(2) HttpServletEvent :
It is used to handle events generated by servlet application.
(3) Cookie :
It is used to create a cookie.
Cookie is a temporary storage created by servlet and stored inside
the client.
3. Initialization :
In this when a servlet gets its first
request, web server initializes that Web server Servlet process
delegates the the request
servlet using init( ). request to a using the
servlet service( )
4. Service :
Once the servlet gets initialized it
accepts the request and provides the
response to client by using service( )
Servlet
The same servlet will wait for some provides the
Servlet waits
other request and generate responses for the other
responses to
request
for them. the client
5. Destruction :
In this if a servlet does not get request,
a web server will destroy that servlet Web server
using destroy( ), to unload its memory Web server
unloads the
destroys servlet
so that the same memory can be used using destroy( ) memory of the
for some other purpose. servlet.
1.4.4 A Simple Welcome Servlet
Q.4 Write a servlet program to display the simple sentence welcome along with
the name entered by the user through an HTML form.
Ans.: //index.jsp
<html>
<body>
<form method="post" action="test">
<b>Enter Name </b>
<input type="text" name="n" value="" size="10">
<br>
<input type="submit" value="Display">
</form>
</body>
</html>
//test.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class test extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)throws
ServletException, IOException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String s=req.getParameter("n");
pw.println("Welcome "+s);
pw.close();
}
}
//test.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Q.2 Create a servlet for a login page. If the username & password are correct
then it says message “Hello” <username> else a message “login failed”.
Ans.: //index.jsp
<html>
<body>
<form method="post" action="test">
<b>Username</b>
<input type="text" name="u" value="" size="10">
<br>
<b>Password</b>
<input type="password" name="p" value="" size="10">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>
//test.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class test extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)throws
ServletException, IOException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String s1=req.getParameter("u");
String s2=req.getParameter("p");
if((s1.equals("servlet"))&&(s2.equals(“sss”)))
{
pw.println("Hello"+s1);
}
else
{
pw.println("Login Failed !!!");
}
pw.close();
}
}
//Question.java (Servlet)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Question extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
int correct=0;
int incorrect=0;
String a=request.getParameter("q1");
String b=request.getParameter("q2");
String c=request.getParameter("q3");
if(a.equals("Java Server Pages"))
{
correct++;
}
else
{
incorrect++;
}
if(c.equals("rm"))
{
correct++;
}
else
{
incorrect++;
}
PrintWriter out = response.getWriter();
try
{
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Question</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Result Of the Test</h1><br>");
out.println("<h3>Correct Answer :::"+correct+"</h3>");
out.println("<h3>Incorrect Answer :::"+incorrect+"</h3>");
out.println("</body>");
out.println("</html>");
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
}
}
Here Type 2 driver will convert JDBC calls into database specific call, but to
send those calls it uses native API.
Advantage : It is faster as compared to type 1 and type 3 driver.
Disadvantage : Just to send the calls it requires Native API.
Here type 3 driver will convert JDBC calls into middleware server specific
calls and vice versa.
Middleware server is normally used to implement different security and traffic
control mechanism.
Advantage : It is comparatively secure among all other type of drivers.
Disadvantage : It is slower as compared to type 2 and type 4 driver.
Type 4 driver will directly convert JDBC calls into database specific calls and
vice versa, as well as it has the capability to send those calls.
Advantage : It is the fastest of all drivers.
Disadvantage : It is costliest of all drivers.
1.6.2 JDBC Architecture
Q.2 Explain JDBC architecture with diagram JDBC Architecture follows layered
Approach.
Ans.:
Driver Manager class
Driver
Layer
Driver Interface
Connection
Application
Layer Prepared Statement Statement Callable Statement Interface
Q.4 Write a program to establish a connection with database table and perform
following operations on table “employee” Assume following database
table.
(i) Insert a record in database table
(ii) Delete a particular row
(iii) Update a particular row
(iv) Display the contents of database table.
id name address
1 sss dadar
2 akn sion
Ans.: import java.sql.*;
class Main
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection(“jdbc:mysql://localhost/vcp”,
“root”,“root”);
s.execute("insert into employee values(3,'sdw','thane')");
System.out.println("Record Inserted");
s.execute("delete from employee where id=2");
System.out.println("Record Deleted");
s.executeUpdate("update employee set name='swapnil' where
address='dadar'");
System.out.println("Record Updated");
ResultSet rs=s.executeQuery("select * from employee");
while(rs.next())
{
System.out.println("ID="+rs.getInt(1));
System.out.println("Name="+rs.getString(2));
System.out.println("Address="+rs.getString(3));
}
rs.close();
s.close();
c.close();
}
catch(Exception e)
{
System.out.println("Exception Occur");
}
}
}