Name Balkrishna Katakwar
Enrollment:- A80105222059
Branch B.- Tech CSE (A)
QUE 1. Write a JDBC application which will interact with Database and perform the
following task. 1) Create Student Table with RollNo. Name, and Address field and
insert few records. 2) Using Statement Object display the content of Record. 3)
Using Staternent Object Insert Two Record. 4) Using Statement Object Update One
Record. 5) Using Statement Object Delete One Record. 6) Using Statement Object
display the content of Record. Make an content with Orecel.
Ans. Import [Link].*;
Public class StudentJDBC { public static void main(String[] args) { String url =
“jdbc:oracle:thin:@localhost:1521:xe”; // Change as per your DB setup String user =
“your_username”; // Change to your DB username String password =
“your_password”; // Change to your DB password
Try (Connection conn = [Link](url, user, password);
Statement stmt = [Link]()) {
// Step 1: Create Student Table
String createTableSQL = “CREATE TABLE Student (RollNo NUMBER
PRIMARY KEY, Name VARCHAR2(50), Address VARCHAR2(100))”;
[Link](createTableSQL);
[Link](“Step 1: Student table created successfully.”);
// Step 2: Insert Initial Records
[Link](“INSERT INTO Student VALUES (1, ‘John Doe’, ‘New
York’)”);
[Link](“INSERT INTO Student VALUES (2, ‘Jane Smith’, ‘Los
Angeles’)”);
[Link](“Step 2: Initial records inserted successfully.”);
// Step 3: Display Existing Records
[Link](“Step 3: Displaying existing records”);
displayRecords(stmt);
// Step 4: Insert Two More Records
[Link](“INSERT INTO Student VALUES (3, ‘Mike Johnson’,
‘Chicago’)”);
[Link](“INSERT INTO Student VALUES (4, ‘Emily Davis’,
‘Houston’)”);
[Link](“Step 4: Two more records inserted.”);
// Step 5: Update One Record
[Link](“UPDATE Student SET Address=’San Francisco’ WHERE
RollNo=2”);
[Link](“Step 5: Record updated.”);
// Step 6: Delete One Record & Display Final Records
[Link](“DELETE FROM Student WHERE RollNo=1”);
[Link](“Step 6: Record deleted.”);
[Link](“Displaying final records:”);
displayRecords(stmt);
} catch (SQLException e) {
[Link]();
Private static void displayRecords(Statement stmt) throws SQLException {
ResultSet rs = [Link](“SELECT * FROM Student”);
[Link](“RollNo | Name | Address”);
While ([Link]()) {
[Link]([Link](“RollNo”) + “ | “ + [Link](“Name”) + “ | “ +
[Link](“Address”));
[Link]();
Explanation:
1. Establishes a connection with an Oracle database.
2. Creates a Student table.
3. Inserts initial records.
4. Displays existing records.
5. Adds two more records.
6. Updates one record.
7. Deletes one record.
8. Displays the final records.
QUE 2.
Write down the Program for testing the Servlet and study deployment descriptor.
Import [Link]; import [Link]; import [Link];
import [Link]; import [Link]; import [Link];
import [Link]; import [Link];
import [Link]; import [Link];
import [Link];
@WebServlet(“/OracleServlet”) public class OracleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { [Link](“text/html”);
PrintWriter out = [Link](); // Database connection parameters String url
= “jdbc:oracle:thin:@localhost:1521:xe”; // Update as per your setup String user =
“your_username”; // Replace with your Oracle username String password =
“your_password”; // Replace with your Oracle password try { // Load Oracle JDBC
Driver [Link](“[Link]”); Connection conn =
[Link](url, user, password); Statement stmt =
[Link](); ResultSet rs = [Link](“SELECT * FROM
Student”); // Display records [Link](“<h2>Student Records</h2>”);
[Link](“<table
border=’1’><tr><th>RollNo</th><th>Name</th><th>Address</th></tr>”); while
([Link]()) { [Link](“<tr><td>” + [Link](“RollNo”) + “</td><td>” +
[Link](“Name”) + “</td><td>” + [Link](“Address”) + “</td></tr>”); }
[Link](“</table>”); // Close connection [Link](); } catch (Exception e) {
[Link](); [Link](“<p>Error retrieving data</p>”); } }
}
Deployment Descriptor ([Link])
Place this file inside WEB-INF/:
<web-app xmlns=[Link] version=”3.0”>
<servlet>
<servlet-name>OracleServlet</servlet-name>
<servlet-class>OracleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OracleServlet</servlet-name>
<url-pattern>/OracleServlet</url-pattern>
</servlet-mapping>
</web-app>
Steps to Deploy and Test:
1. Compile and Package your servlet in a Java web project.
2. Deploy on Tomcat or any Java EE server.
3. Ensure Oracle Database is running and contains a Student table.
4. Access Servlet in Browser:
[Link]
5. Expected Output: A table displaying student records.
QUE 3. Write down the program for testing the include action for servlet
collaboration.
Ans - // First Servlet: [Link] import [Link]; import
[Link]; import [Link]; import
[Link]; import [Link]; import
[Link]; import [Link];
import [Link];
@WebServlet(“/MainServlet”) public class MainServlet extends HttpServlet { private
static final long serialVersionUID = 1L;
Protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link](“text/html”);
PrintWriter out = [Link]();
[Link](“<h2>Main Servlet Content</h2>”);
[Link](“<p>Before including another servlet.</p>”);
// Step 3: Include Another Servlet
RequestDispatcher rd = [Link](“/IncludedServlet”);
[Link](request, response);
[Link](“<p>After including another servlet.</p>”);
// Second Servlet: [Link] @WebServlet(“/IncludedServlet”) public class
IncludedServlet extends HttpServlet { private static final long serialVersionUID = 1L;
Protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = [Link]();
[Link](“<p>This is content from Included Servlet.</p>”);
}
Explanation:
1. MainServlet:
Displays some content.
Uses [Link]() to include IncludedServlet.
2. IncludedServlet:
Outputs additional content when included by MainServlet.
Testing the Include Action:
1. Deploy both servlets in your Java web application.
2. Run the application and access:
[Link]
3. Output:
Main Servlet Content
Before including another servlet.
This is content from Included Servlet.
After including another servlet.
QUE 4 : Write down the program for testing the forward action for servlet
collaboration.
ANS - : // First Servlet: [Link] import [Link]; import
[Link]; import [Link]; import
[Link]; import [Link]; import
[Link]; import [Link];
import [Link]; import [Link].*;
@WebServlet(“/ForwardingServlet”) public class ForwardingServlet extends
HttpServlet { private static final long serialVersionUID = 1L;
Protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link](“text/html”);
PrintWriter out = [Link]();
// Step 1: Connect to Oracle Database
String url = “jdbc:oracle:thin:@localhost:1521:xe”; // Change as per your DB setup
String user = “your_username”; // Change to your DB username
String password = “your_password”; // Change to your DB password
Try {
[Link](“[Link]”);
Connection conn = [Link](url, user, password);
Statement stmt = [Link]();
ResultSet rs = [Link](“SELECT * FROM Student”);
[Link](“<h2>Student Records</h2>”);
[Link](“<table
border=’1’><tr><th>RollNo</th><th>Name</th><th>Address</th></tr>”);
While ([Link]()) {
[Link](“<tr><td>” + [Link](“RollNo”) + “</td><td>” +
[Link](“Name”) + “</td><td>” + [Link](“Address”) + “</td></tr>”);
[Link](“</table>”);
[Link]();
} catch (Exception e) {
[Link]();
[Link](“<p>Error retrieving data</p>”);
[Link](“<p>Request is being forwarded to another servlet...</p>”);
// Step 3: Forward request to TargetServlet
RequestDispatcher rd = [Link](“/TargetServlet”);
[Link](request, response);
// Second Servlet: [Link] @WebServlet(“/TargetServlet”) public class
TargetServlet extends HttpServlet { private static final long serialVersionUID = 1L;
Protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link](“text/html”);
PrintWriter out = [Link]();
[Link](“<h2>Target Servlet</h2>”);
[Link](“<p>This response is generated after forwarding.</p>”);
QUE 5. Create login form and perform state managernent using Cookies,
HttpSession and URL Rewriting.
A login form implementation with state management using Cookies, HttpSession,
and URL Rewriting in Oracle-based Java web applications (likely using Servlets and
JSP). Below is a breakdown of how we can implement this:
Steps to Implement:
1. Create a Login Form (JSP/HTML).
2. Create a Servlet to Handle Login Authentication.
3. Manage Session Using Cookies, HttpSession, and URL Rewriting.
4. Create a Logout Feature.
5. Ensure Oracle Database Integration for User Authentication.
Login Form ([Link])
<form action=”LoginServlet” method=”post”>
<label>Username:</label>
<input type=”text” name=”username” required><br>
<label>Password:</label>
<input type=”password” name=”password” required><br>
<input type=”submit” value=”Login”>
</form>
Servlet: [Link]
This servlet will:
Authenticate the user using an Oracle database.
Manage session using Cookies, HttpSession, and URL Rewriting.
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link].*;
@WebServlet(“/LoginServlet”)
Public class LoginServlet extends HttpServlet {
Protected void doPost(HttpServletRequest request, HttpServletResponse
response)
Throws ServletException, IOException {
[Link](“text/html”);
PrintWriter out = [Link]();
String username = [Link](“username”);
String password = [Link](“password”);
Try {
// Load Oracle JDBC Driver
[Link](“[Link]”);
Connection con = [Link](
“jdbc:oracle:thin:@localhost:1521:xe”, “your_username”,
“your_password”);
PreparedStatement ps = [Link](
“SELECT * FROM users WHERE username=? AND password=?”);
[Link](1, username);
[Link](2, password);
ResultSet rs = [Link]();
If ([Link]()) {
// Create Session
HttpSession session = [Link]();
[Link](“username”, username);
// Create Cookie
Cookie cookie = new Cookie(“user”, username);
[Link](cookie);
// Redirect with URL Rewriting
[Link](“[Link]?user=” + username);
} else {
[Link](“Invalid Username or Password”);
[Link](“[Link]”).include(request, response);
[Link]();
} catch (Exception e) {
[Link](“Database Connection Error: “ + [Link]());
Welcome Page ([Link])
This page will:
Retrieve the session, cookies, and URL rewriting parameters.
<%@ page import=”[Link].*, [Link].*” %>
<%
HttpSession session = [Link](false);
String username = (String) [Link](“username”);
// Retrieve from Cookie
Cookie[] cookies = [Link]();
String cookieUser = null;
If (cookies != null) {
For (Cookie c : cookies) {
If ([Link]().equals(“user”)) {
cookieUser = [Link]();
// Retrieve from URL Rewriting
String urlUser = [Link](“user”);
If (username == null && cookieUser == null && urlUser == null) {
[Link](“[Link]”);
} else {
[Link](“Welcome, “ + (username != null ? username : (cookieUser != null ?
cookieUser : urlUser)));
%>
<br>
<a href=”LogoutServlet”>Logout</a>
Logout Servlet ([Link])
This servlet will:
Invalidate the session.
Delete cookies.
Redirect to the login page.
Import [Link];
Import [Link];
Import [Link];
Import [Link].*;
@WebServlet(“/LogoutServlet”)
Public class LogoutServlet extends HttpServlet {
Protected void doGet(HttpServletRequest request, HttpServletResponse
response)
Throws ServletException, IOException {
[Link](“text/html”);
// Invalidate Session
HttpSession session = [Link](false);
If (session != null) {
[Link]();
// Delete Cookie
Cookie cookie = new Cookie(“user”, “”);
[Link](0);
[Link](cookie);
// Redirect to login
[Link](“[Link]”);
Database Setup (Oracle)
Create a table in Oracle to store user credentials.
CREATE TABLE users (
Id NUMBER PRIMARY KEY,
Username VARCHAR2(50) UNIQUE NOT NULL,
Password VARCHAR2(50) NOT NULL
);
INSERT INTO users (id, username, password) VALUES (1, ‘admin’, ‘admin123’);
COMMIT;