Servlet with JDBC for Ayurvedic Remedies
Build a simple, form-based web system using Java Servlets and JDBC that recommends Ayurvedic remedies for common ailments. This tutorial covers database schema, sample data, a servlet to process the form and query remedies, and a JSP to display results. Ideal for BCA students and IKS projects.
What you'll build
- A MySQL table that stores ailments and recommended Ayurvedic remedies.
- A form (HTML/JSP) where users select or type an ailment.
- A servlet that accepts the form, queries the database via JDBC, and forwards results to a JSP.
Database: Schema & Sample Data
CREATE DATABASE iks_db; USE iks_db; CREATE TABLE ailments ( id INT AUTO_INCREMENT PRIMARY KEY, ailment_name VARCHAR(150) NOT NULL, symptoms TEXT, remedy TEXT, precautions TEXT, last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); INSERT INTO ailments (ailment_name, symptoms, remedy, precautions) VALUES ('Common Cold', 'runny nose, cough, mild fever', 'Tulsi kadha with honey; steam inhalation', 'Avoid cold drinks; consult doctor if high fever'), ('Indigestion', 'bloating, belching, mild pain', 'Ajwain with warm water; ginger tea', 'Avoid heavy fried foods; seek doctor if persistent'), ('Insomnia', 'difficulty sleeping', 'Warm milk with nutmeg; Ashwagandha supplements', 'Consult physician for chronic insomnia');
Frontend: Simple JSP form (index.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Ayurvedic Remedies Finder</title> <style> body{font-family:Arial,Helvetica,sans-serif;max-width:900px;margin:20px auto;color:#222} .card{background:#fff;padding:16px;border-radius:8px;box-shadow:0 1px 6px rgba(0,0,0,.06)} input[type="text"]{width:60%;padding:8px;margin-right:8px} button{padding:8px 12px} </style> </head> <body> <div class="card"> <h2>Find Ayurvedic Remedies</h2> <form method="get" action="findRemedy"> <label for="ailment">Enter ailment (or select):</label><br/> <input type="text" id="ailment" name="ailment" placeholder="e.g., Indigestion or Common Cold" required /> <button type="submit">Find Remedy</button> </form> </div> </body> </html>
Servlet: FindRemedyServlet (Java)
import java.io.IOException; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; @WebServlet("/findRemedy") public class FindRemedyServlet extends HttpServlet { private static final String JDBC_URL = "jdbc:mysql://localhost:3306/iks_db?useSSL=false&serverTimezone=UTC"; private static final String DB_USER = "root"; private static final String DB_PASS = "your_password"; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String ailmentParam = req.getParameter("ailment"); if (ailmentParam == null || ailmentParam.isBlank()) { req.setAttribute("message", "Please enter an ailment to search."); req.getRequestDispatcher("/result.jsp").forward(req, resp); return; } String sql = "SELECT ailment_name, symptoms, remedy, precautions FROM ailments WHERE LOWER(ailment_name) LIKE ? OR LOWER(symptoms) LIKE ? LIMIT 10"; try { Class.forName("com.mysql.cj.jdbc.Driver"); // optional in modern JVM } catch (ClassNotFoundException e) { // log - driver not found } try (Connection con = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS); PreparedStatement ps = con.prepareStatement(sql)) { String q = "%" + ailmentParam.toLowerCase() + "%"; ps.setString(1, q); ps.setString(2, q); try (ResultSet rs = ps.executeQuery()) { // Build a simple result list to pass to JSP java.util.List> results = new java.util.ArrayList<>(); while (rs.next()) { java.util.Map row = new java.util.HashMap<>(); row.put("ailment_name", rs.getString("ailment_name")); row.put("symptoms", rs.getString("symptoms")); row.put("remedy", rs.getString("remedy")); row.put("precautions", rs.getString("precautions")); results.add(row); } if (results.isEmpty()) { req.setAttribute("message", "No remedies found. Try a different keyword."); } else { req.setAttribute("results", results); } } } catch (SQLException e) { e.printStackTrace(); // log on server req.setAttribute("message", "Database error occurred. Contact admin."); } req.getRequestDispatcher("/result.jsp").forward(req, resp); } }
Result JSP: result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Remedy Results</title> <style> body{font-family:Arial;max-width:900px;margin:18px auto;color:#222} .card{background:#fff;padding:16px;border-radius:8px;box-shadow:0 1px 6px rgba(0,0,0,.06)} .rem{border-left:4px solid #0b7;padding:10px;margin:10px 0} .label{color:#555;font-weight:bold} </style> </head> <body> <div class="card"> <h2>Remedy Results</h2> <% String message = (String) request.getAttribute("message"); %> <c:if test="${not empty message}"> <p style="color:#a00">${message}</p> </c:if> <@ page import="java.util.*" %> <ListRemedy:Precautions: <} %> <div><a href="index.jsp">Search again</a></div> </div> </body> </html>
Security & best practices
- Always use PreparedStatement to avoid SQL injection.
- Validate user inputs (limit length, normalize case) and sanitize outputs to avoid XSS.
- Do not display medical claims as absolute — include disclaimers: “This information is educational; consult a qualified practitioner for diagnosis and treatment.”
- Use HTTPS and secure DB credentials (do not hard-code in production; use environment variables or secure vaults).
- Log changes and maintain provenance for remedies (who added/updated entries).
Optional enhancements
- Add user accounts so users can save favorite remedies or add notes.
- Add full-text search for symptoms to improve match accuracy.
- Integrate image or video guidance for remedies and yoga practices.
- Use transliteration/storage of original languages (Devanagari) and English transliteration separately.
Note (ethics): Ayurvedic remedies are traditional knowledge. Always cite sources, avoid making unverified medical claims, and encourage users to consult health professionals for serious conditions.
Tags:
j2ee