0% found this document useful (0 votes)
3 views

unit 3 (1)

The document provides Java servlet examples for three functionalities: displaying an employee's net salary from a database, checking the status of a train using a PNR number, and managing user sessions with cookies. Each servlet includes the necessary database table creation SQL and Java code to implement the functionality. These examples serve as concise solutions for basic Java web applications.

Uploaded by

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

unit 3 (1)

The document provides Java servlet examples for three functionalities: displaying an employee's net salary from a database, checking the status of a train using a PNR number, and managing user sessions with cookies. Each servlet includes the necessary database table creation SQL and Java code to implement the functionality. These examples serve as concise solutions for basic Java web applications.

Uploaded by

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

2.

Here are simple solutions to your requests, written in Java with basic
functionality:

### (i) Java Servlet to Display Net Salary of Employee using JDBC

**Step 1: Create Database Table (MySQL)**

```sql

CREATE TABLE employee (

id INT PRIMARY KEY,

name VARCHAR(50),

basic_salary DECIMAL(10,2),

hra DECIMAL(10,2),

other_allowances DECIMAL(10,2)

);

```

**Step 2: Servlet Code**

```java

@WebServlet("/EmployeeSalary")

public class EmployeeSalaryServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

int empId = Integer.parseInt(request.getParameter("empId"));

try (Connection con =


DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDB",
"root", "password");

PreparedStatement stmt = con.prepareStatement("SELECT *


FROM employee WHERE id = ?")) {

stmt.setInt(1, empId);
ResultSet rs = stmt.executeQuery();

if (rs.next()) {

double basicSalary = rs.getDouble("basic_salary");

double hra = rs.getDouble("hra");

double otherAllowances = rs.getDouble("other_allowances");

double netSalary = basicSalary + hra + otherAllowances;

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<h3>Employee Net Salary</h3>");

out.println("Net Salary: " + netSalary);

} else {

response.getWriter().println("Employee not found");

} catch (Exception e) {

e.printStackTrace();

```

3) ### (i) Servlet to Display Waiting List Status of Train (using PNR)

**Step 1: Database Table (Train Details)**

```sql

CREATE TABLE train_status (

pnr_number VARCHAR(10) PRIMARY KEY,

status VARCHAR(50)
);

```

**Step 2: Servlet Code**

```java

@WebServlet("/TrainStatus")

public class TrainStatusServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

String pnr = request.getParameter("pnr");

try (Connection con =


DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDB",
"root", "password");

PreparedStatement stmt = con.prepareStatement("SELECT status


FROM train_status WHERE pnr_number = ?")) {

stmt.setString(1, pnr);

ResultSet rs = stmt.executeQuery();

if (rs.next()) {

String status = rs.getString("status");

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<h3>Train PNR Status</h3>");

out.println("PNR Status: " + status);

} else {

response.getWriter().println("PNR not found");

} catch (Exception e) {
e.printStackTrace();

```

### (ii) Servlet to Illustrate the Principle of Cookies

**Step 1: Servlet Code**

```java

@WebServlet("/CookieExample")

public class CookieExampleServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

Cookie[] cookies = request.getCookies();

boolean cookieFound = false;

if (cookies != null) {

for (Cookie cookie : cookies) {

if ("user".equals(cookie.getName())) {

response.getWriter().println("Welcome back, " +


cookie.getValue());

cookieFound = true;

break;

if (!cookieFound) {

Cookie newCookie = new Cookie("user", "JohnDoe");


newCookie.setMaxAge(60 * 60); // 1 hour

response.addCookie(newCookie);

response.getWriter().println("New user! Cookie set for 1 hour.");

```

### Summary:

1. **Employee Salary Servlet**: Connects to the database to retrieve


employee details and calculates the net salary.

2. **Train Status Servlet**: Fetches the train status from the database
using a PNR number.

3. **Cookie Servlet**: Demonstrates creating and reading cookies to


maintain session information.

These are simple and short examples for your 16-mark answer.

You might also like