0% found this document useful (0 votes)
16 views5 pages

Notes 2

The document provides comprehensive study notes on Java EE, Servlets, and JDBC, covering key concepts such as multi-tier architecture, servlet lifecycle, and database access. It includes sample servlet code for basic operations like login validation and user insertion, along with MySQL commands for database setup. Key points emphasize the importance of using JDBC for database connections, security practices, and the efficiency of servlets in web applications.

Uploaded by

yuvashree.ra05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Notes 2

The document provides comprehensive study notes on Java EE, Servlets, and JDBC, covering key concepts such as multi-tier architecture, servlet lifecycle, and database access. It includes sample servlet code for basic operations like login validation and user insertion, along with MySQL commands for database setup. Key points emphasize the importance of using JDBC for database connections, security practices, and the efficiency of servlets in web applications.

Uploaded by

yuvashree.ra05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Can you give neat notes for this ppt......

explain
clearly everything and give appropiate code in this
give the same neatly and give it in a pdf
Here are neat and clear notes covering all topics from your PPT. Since automatic PDF creation
with code rendering is not supported in this environment, I will present the well-organized notes
and code here. You can easily copy this into Word/LaTeX and export to PDF.

Java EE/Servlets/JDBC Study Notes


1. Java Enterprise Edition (JEE/Jakarta EE) Overview
JEE/Jakarta EE: Platform for building large, robust, scalable, secure web apps and server-
side solutions. Widely used in banking, health, e-commerce.
Multi-tier Architecture:
Client Tier: End user interface (browser, app)
Web Tier: Handles user requests/responses (Servlet, JSP)
Business Tier: Business logic (EJB, services)
Data Tier: Data access/storage (JPA, JDBC)
Integration Layer (Optional): Connect with APIs/external services (JMS, Web Services)
JEE (Java EE/Jakarta EE) JSE (Java SE)

Web, enterprise applications Desktop, general use

APIs: JSP, Servlet, EJB, etc. Core Java libraries

Web app support: Yes No

2. Servlets

Definition
A Java class that runs on a server, handles HTTP requests and generates responses (typically
HTML).
Key Advantages Over CGI
Efficient: Runs as threads (not processes); better memory and response times.
Convenient: Use Java, don't need Perl/shell scripting. Built-in form/data parsing,
header/cookie/session handling.
Powerful: Talks directly with web server, can maintain state/session, share resources easily,
optimize performance.
Portable/Free: Works across servers/platforms, easy to deploy.

3. Servlet Architecture & Lifecycle

Lifecycle Methods
init(ServletConfig): Run once when servlet is loaded. For startup/config.
service(ServletRequest, ServletResponse): Called for each request.
destroy(): Called when server removes the servlet (cleanup).

Request Handling Flow


1. User request (via browser)
2. Web server receives and maps to a Servlet
3. Servlet loaded, init() calls once
4. service() invoked for every HTTP request
5. Eventually, destroy() called for cleanup

4. Important Servlet Classes & Methods

Main Packages / Interfaces


ServletRequest/ServletResponse: Handle client data and server response
HttpServlet: Extends GenericServlet with HTTP functionality
doGet()/doPost(): Methods for handling GET/POST requests

Example: doGet() and doPost()

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello, GET method called!</h1>");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
// Similar to doGet(), but for POST
}

5. Sample Servlet Code: Basic (Hello World)

import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;

@WebServlet(urlPatterns = {"/HelloServlet"})
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body><h1>Hello World Servlet!</h1></body></html>");
out.close();
}
}

6. Sample Login Servlet (Hardcoded Validation)

@WebServlet(urlPatterns = {"/LoginValidation"})
public class LoginValidation extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse respons
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("uname");
String pwd = request.getParameter("pwd");
if (name.equals("marees") && pwd.equals("msw"))
response.sendRedirect("http://www.vit.ac.in");
else
out.println("<b>Invalid User</b>");
out.close();
}
}

7. Three-Tier Web App with JDBC (Database Access)


1. Frontend: HTML/JSP sends data (e.g., username/password)
2. Servlet: Processes input, connects to DB via JDBC
3. Database: Stores user data (e.g., MySQL)
MySQL Commands for Setup

create database mareesDB;


use mareesDB;
create table users(name varchar(20) not null, password varchar(15) not null);
insert into users values("marees", "msw");
insert into users values("eswari", "123");
select * from users;

8. Servlet JDBC Login Example

@WebServlet (urlPatterns = {"/LoginDatabase"})


public class LoginDatabase extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse respons
throws ServletException, IOException, ClassNotFoundException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
int verified = 0;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mareesD
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users");
while (rs.next()) {
String dbname = rs.getString("name");
String dbpassword = rs.getString("password");
if (uname.equals(dbname) && pwd.equals(dbpassword)) verified = 1;
}
if (verified == 1) out.println("Valid User");
else out.println("Invalid User. Login Again");
out.close();
}
}

9. Servlet to Insert New User in Database

@WebServlet(urlPatterns = {"/InsertDatabase"})
public class InsertDatabase extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse respons
throws ServletException, IOException, ClassNotFoundException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mareesD
PreparedStatement st = con.prepareStatement("INSERT INTO users (name, password) V
st.setString(1, uname);
st.setString(2, pwd);
st.executeUpdate();
out.println("Inserted");
out.close();
}
}

10. Sample HTML Login & Insert Forms


Login Form

<form action="http://localhost:8080/WebApplication2/LoginDatabase" method="get">


User Name <input type="text" name="uname"><br>
Password <input type="password" name="pwd">
<input type="submit" value="login">
</form>

Insert User Form

<form action="http://localhost:8080/WebApplication5/InsertDatabase" method="get">


User Name <input type="text" name="uname"><br>
Password <input type="password" name="pwd">
<input type="submit" value="Insert">
</form>

11. Key Points


Servlets make Java web applications efficient, portable, powerful, and maintainable.
Always use JDBC for database connections in servlets.
Use POST for sensitive/large form data, GET for short/simple requests.
Always close your database connections and output streams.
For security, use PreparedStatement to prevent SQL injection in JDBC.

You can simply copy this structured content into Microsoft Word or Google Docs, adjust
fonts/headings/codes, then select "Save as PDF" to create your handout.
If you need the PDF generation done for you with the above content, let me know—I can guide
you to use Python on your computer or provide you the code to run!

1. Module-2.pdf

You might also like