Create a Panchang Viewer Using Servlet
This guide shows how to build a simple Panchang Viewer web app using Java Servlets and JDBC. The app fetches daily Panchang details (tithi, nakshatra, sunrise time) from a MySQL database and displays them on a JSP page.
What you will build
- A
panchang
table in MySQL that stores Panchang rows for dates. - A Java Servlet (
PanchangServlet
) that queries the DB for today's Panchang (or a requested date). - A JSP (
panchang.jsp
) to display tithi, nakshatra, sunrise/sunset times.
Database: Table Schema and sample data
-- create database and table CREATE DATABASE iks_db; USE iks_db; CREATE TABLE panchang ( id INT AUTO_INCREMENT PRIMARY KEY, pdate DATE NOT NULL, tithi VARCHAR(100), nakshatra VARCHAR(100), sunrise TIME, sunset TIME, notes TEXT, UNIQUE KEY (pdate) ); -- sample rows INSERT INTO panchang (pdate, tithi, nakshatra, sunrise, sunset, notes) VALUES ('2025-08-10','Pratipada','Ashwini','06:02:00','18:48:00','Sample Panchang entry for demo'), ('2025-08-11','Dwitiya','Bharani','06:03:00','18:47:00','Another example');
Project notes
- Use
mysql-connector-java
in your project (Maven/Gradle or add JAR to WEB-INF/lib). - Project structure (typical):
src/main/java
for servlets,webapp/WEB-INF
for JSP and web.xml.
Servlet: PanchangServlet (Java)
import java.io.IOException; import java.sql.*; import java.time.LocalDate; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; @WebServlet("/panchang") public class PanchangServlet extends HttpServlet { private static final long serialVersionUID = 1L; // Update these DB values for your environment 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 { // Accept optional date param in yyyy-MM-dd format; default = today String dateParam = req.getParameter("date"); LocalDate date; if (dateParam == null || dateParam.isBlank()) { date = LocalDate.now(); // server local date } else { date = LocalDate.parse(dateParam); } String sql = "SELECT pdate, tithi, nakshatra, sunrise, sunset, notes FROM panchang WHERE pdate = ?"; try (Connection con = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS); PreparedStatement ps = con.prepareStatement(sql)) { // (Optional) load driver - modern JVMs auto-load, but safe to keep: try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { // ignore if driver is auto-loaded by JDBC 4+ } ps.setDate(1, Date.valueOf(date)); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { req.setAttribute("pdate", rs.getDate("pdate").toString()); req.setAttribute("tithi", rs.getString("tithi")); req.setAttribute("nakshatra", rs.getString("nakshatra")); req.setAttribute("sunrise", rs.getTime("sunrise").toString()); req.setAttribute("sunset", rs.getTime("sunset").toString()); req.setAttribute("notes", rs.getString("notes")); } else { req.setAttribute("message", "No Panchang data found for " + date.toString()); } } } catch (SQLException e) { // log error and set friendly message e.printStackTrace(); req.setAttribute("message", "Database error occurred while fetching Panchang. Contact admin."); } // forward to JSP req.getRequestDispatcher("/panchang.jsp").forward(req, resp); } }
JSP: panchang.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Panchang Viewer</title> <style> body{font-family: Arial; max-width:800px;margin:20px auto;color:#222} .card{background:#fff;padding:18px;border-radius:8px;box-shadow:0 1px 4px rgba(0,0,0,.08)} .row{display:flex;gap:10px;margin:10px 0} .label{width:120px;color:#555} </style> </head> <body> <div class="card"> <h2>Panchang Viewer</h2> <% String message = (String) request.getAttribute("message"); %> <c:if test="${not empty message}"> <p style="color:#a00">${message}</p> </c:if> <!-- Show data if present --> <% String pdate = (String) request.getAttribute("pdate"); if (pdate != null) { %> <div class="row">Date:<div>${pdate}</div></div> <div class="row">Tithi:<div>${tithi}</div></div> <div class="row">Nakshatra:<div>${nakshatra}</div></div> <div class="row">Sunrise:<div>${sunrise}</div></div> <div class="row">Sunset:<div>${sunset}</div></div> <div class="row">Notes:<div>${notes}</div></div> <% } else { %> <p>No data to display. Try adding a date query parameter:?date=2025-08-10
</p> <% } %> </div> </body> </html>
Note: the JSP example uses JSP scriptlets for simplicity (works for small demos). For production prefer JSTL & EL and MVC frameworks (no scriptlets).
web.xml mapping (optional if not using @WebServlet)
<web-app> <servlet> <servlet-name>PanchangServlet</servlet-name> <servlet-class>com.yourpackage.PanchangServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PanchangServlet</servlet-name> <url-pattern>/panchang</url-pattern> </servlet-mapping> </web-app>
Optional: Fetch Panchang from external API
If you prefer not to store Panchang data in your DB, your Servlet can call an external Panchang API (JSON) and parse the response. Example idea:
- Make an HTTP GET request to API (e.g., a trusted Panchang JSON provider).
- Parse JSON (use
org.json
or Jackson/Gson) and set request attributes to show on JSP. - Cache results for the day to reduce API calls.
Security & best practices
- Use PreparedStatement for all SQL with parameters to avoid SQL injection.
- Do not store DB credentials in code in production; use environment variables or encrypted config.
- Validate date input from users before parsing.
- Consider timezones when storing/displaying Panchang (use location-aware logic if needed).
How to test
- Deploy WAR to Tomcat (or other servlet container) with MySQL accessible.
- Insert test panchang rows for the dates you will query.
- Open
http://localhost:8080/yourapp/panchang
(for today's date) or?date=YYYY-MM-DD
.
If you want, I can now:
- Provide a downloadable ZIP with the Servlet + JSP + Maven POM ready to run, or
- Create a Blogger-ready post version (HTML) formatted for your BCA School theme (shorter), or
- Design a matching thumbnail image for this article.
Tags:
j2ee