Java EE and Servlets - Unit 1 (Full Notes with Examples)
Unit 1 - Java EE and Servlets (Detailed Notes with Examples)
1. What is Java EE (Enterprise Edition)?
Java EE is a platform for building large scale, multi-user, secure, and scalable business
applications.
Example: Online banking software, IRCTC ticket booking system.
2. What is an Enterprise Application?
It is a business application used in organizations to manage operations like HR, sales, banking, etc.
Features: High Availability, Scalability, Security, Fast Performance.
3. Java EE Technologies:
- Servlets: Used for handling client requests on web servers.
- JSP (Java Server Pages): Used for creating dynamic web pages.
- EJB (Enterprise JavaBeans): Used for business logic.
- JDBC: Used to connect Java applications with databases like MySQL.
- JMS (Java Messaging Service): Used for message communication between different parts of the
application.
4. Java EE Architecture Types:
- Single-tier Architecture: All code (UI, logic, database) on one system.
- Two-tier Architecture: Client and Server.
Example: Java Swing application connecting directly to MySQL.
- Three-tier Architecture: Client -> Web Server -> Database Server.
Example: Web browser -> Servlet/JSP -> MySQL Database.
- Four-tier Architecture: Client -> Web Server -> Application Server -> Database.
- Multi-tier Architecture: Multiple layers for better control and maintenance.
5. Java EE Containers:
- Web Container: Runs Servlets and JSP.
- EJB Container: Runs EJBs (business logic components).
- Application Client Container: Runs standalone Java client programs.
- Applet Container: Runs Java Applets inside browsers.
6. GlassFish Server:
GlassFish is a Java EE Application Server that runs Java EE components like Servlets, JSPs, EJBs.
Example: Deploying your Servlet on GlassFish for handling web requests.
7. Introduction to Servlets:
Servlet is a Java class that runs on server and handles client requests and responses.
Advantages:
- High performance (uses threads, not processes)
- Portable (Java based)
- Secure
- Robust
Example Servlet Code:
@WebServlet("/Hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<h1>Hello, this is my first Servlet!</h1>");
8. Servlet Life Cycle:
- init(): Initializes the servlet. Runs once when servlet loads.
- service(): Runs every time client sends request.
- destroy(): Runs before servlet unloads. Cleans up resources.
9. Servlet API Packages:
- javax.servlet: For generic servlet classes and interfaces.
- javax.servlet.http: For HTTP-specific classes like HttpServlet, HttpServletRequest,
HttpServletResponse.
10. Using Annotations instead of web.xml:
Instead of configuring servlet in web.xml, we use @WebServlet annotation.
Example:
@WebServlet("/Welcome")
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<h2>Welcome Servlet using Annotation</h2>");
11. JDBC (Java Database Connectivity):
Java API for connecting Java programs to databases.
Types of JDBC Drivers:
- Type 1: JDBC-ODBC Bridge
- Type 2: Native API Driver
- Type 3: Network Protocol Driver
- Type 4: Pure Java Thin Driver (Most commonly used)
Steps for JDBC Connection:
1. Load JDBC Driver
2. Establish connection with database
3. Create Statement
4. Execute SQL Queries
5. Process Results
6. Close connection
Example JDBC Connection:
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "root");
12. Servlet + JDBC Example (Registration Form):
HTML Form (index.html):
<form action='RegisterServlet' method='get'>
Username: <input type='text' name='uname'><br>
Password: <input type='password' name='pass'><br>
Email: <input type='text' name='email'><br>
Country: <input type='text' name='country'><br>
<input type='submit' value='Register'>
</form>
Servlet Code (RegisterServlet.java):
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uname = request.getParameter("uname");
String pass = request.getParameter("pass");
String email = request.getParameter("email");
String country = request.getParameter("country");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb", "root",
"root");
PreparedStatement pst = con.prepareStatement("insert into user values (?, ?, ?, ?)");
pst.setString(1, uname);
pst.setString(2, pass);
pst.setString(3, email);
pst.setString(4, country);
int row = pst.executeUpdate();
out.print("<h3>Registration Successful! Rows inserted: " + row + "</h3>");
} catch(Exception e) {
out.print(e);
13. Important Exam Questions:
- Explain 3-tier architecture.
- Explain types of JDBC drivers.
- Explain Servlet Life Cycle.
- Explain JDBC Architecture.
- Difference between CGI and Servlet.
- Servlet login or registration form example.
- JDBC insert example program.
End of Unit 1 Notes.