OneCompiler

harikrish

139

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html> <html> <head> <title>Edit Patient Information</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f2f2f2; margin: 0; padding: 0; } .container { width: 60%; margin: 50px auto; padding: 20px; background-color: #fff; box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1); border-radius: 8px; } h2 { text-align: center; color: #333; } .form-group { margin-bottom: 15px; } .form-group label { display: block; margin-bottom: 5px; font-weight: bold; } .form-group input, .form-group textarea { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; } .form-group input[disabled] { background-color: #e9ecef; } button { width: 100%; padding: 10px; background-color: #007bff; border: none; border-radius: 4px; color: #fff; font-size: 16px; cursor: pointer; } button:hover { background-color: #0056b3; } .message { margin-top: 20px; padding: 10px; border: 1px solid; border-radius: 4px; display: none; } .success { border-color: #28a745; color: #28a745; } .error { border-color: #dc3545; color: #dc3545; } </style> <script> function showMessage(type, message) { var messageDiv = document.getElementById("message"); messageDiv.className = "message " + type; messageDiv.innerHTML = message; messageDiv.style.display = "block"; }
    window.onload = function() {
        var message = "<%= request.getAttribute("message") != null ? request.getAttribute("message") : "" %>";
        if (message && message.trim() !== "") {
            var type = message.startsWith("Error") ? "error" : "success";
            showMessage(type, message);
        }
    };
</script>
</head> <body> <div class="container"> <h2>Edit Patient Information</h2> <form action="EditPatientServlet" method="post"> <div class="form-group"> <label for="email">Email:</label> <input type="email" id="email" name="email" value="${patient.email}"> </div> <div class="form-group"> <label for="phone">Phone:</label> <input type="text" id="phone" name="phone" value="${patient.phone}"> </div> <div class="form-group"> <label for="notes">Notes:</label> <textarea id="notes" name="notes" rows="4">${patient.notes}</textarea> </div> <button type="submit">Update</button> </form> <div id="message"></div> </div> </body> </html>

package com.hospital.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/EditPatientServlet")
public class EditPatientServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Fetch parameters from the form
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String notes = request.getParameter("notes");

// Validate inputs (basic validation for demonstration)
if (email == null || email.isEmpty() || phone == null || phone.isEmpty() || notes == null || notes.isEmpty()) {
    request.setAttribute("message", "Error: All fields are required.");
    request.getRequestDispatcher("/editPatient.jsp").forward(request, response);
    return;
}

// Simulate update operation (replace with your actual database logic)
boolean updateSuccessful = updatePatientInfo(email, phone, notes);

// Set success or error message based on update result
if (updateSuccessful) {
    request.setAttribute("message", "Patient information updated successfully.");
} else {
    request.setAttribute("message", "Error: Failed to update patient information.");
}

// Forward back to editPatient.jsp with appropriate message
request.getRequestDispatcher("/editPatient.jsp").forward(request, response);

}

// Simulated update method (replace with actual database logic)
private boolean updatePatientInfo(String email, String phone, String notes) {
// Implement actual database update logic here
// For demonstration, we assume the update is always successful
return true;
}
}