Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for ping to database
Previous Home Next

In this servlet program, ping to database for retrieve database version information and display it on browser. A Driver must be added into Library folder of program for connectivity java to MYSQL database. In this program a Type IV Driver (Connector/J) can be used-

  • MYSQL JDBC Driver ( Connector/J 5.1.18) (click here)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
 <servlet-name>pingDataBaseServlet</servlet-name>
 <servlet-class>r4r.servlet.pingDataBaseServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>pingDataBaseServlet</servlet-name>
 <url-pattern>/pingDataBaseServlet</url-pattern>
</servlet-mapping>
<session-config>
 <session-timeout>
  30
 </session-timeout>
</session-config>
</web-app>

Index.jsp

<%-- 
Document   : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>r4r.co.in-servlet</title>
</head>
<body>
<form>
<h1>Access Servlet by servlet's Name!</h1>
<a href="pingDataBaseServlet">pingDataBaseServlet</a>
</form>
</body>
</html>

Servlet Program

/*
 * Save as a pingDataBaseServlet.java
 */
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author R4R
 */
public class pingDataBaseServlet extends HttpServlet {

private Connection con = null;

protected void processRequest
	 (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet pingDataBaseServlet at "
         + request.getContextPath() + "</h1>");
out.println(" <form name=\"form\" action=\
           "pingDataBaseServlet\" method=\"POST\">");
out.println("<BR><input type=\"submit\" value=\
          "Ping To MySQL DataBase\" />");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

@Override
protected void doGet
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost
	(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
//Load dynamic Class
Class.forName("com.mysql.jdbc.Driver").newInstance();
// Make connection to class
con = (Connection) 
 DriverManager.getConnection("jdbc:mysql://localhost", "root", "root");
// Check connection status
if (!con.isClosed()) {
out.println("Successfully Connect to MYSQL DataBase");
// Get the MYSQL DataBase Version
Statement st = (Statement) con.createStatement();
ResultSet rs = st.executeQuery("SELECT VERSION()");
while (rs.next()) {
out.println
("<BR>MYSQL DataBase version - " + "<b>" + rs.getString(1) + "</b>");
}
} else {
out.println("Sorry, Cann't connect to DataBase");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} catch (Exception ex) {
Logger.getLogger(pingDataBaseServlet.class.getName())
	      .log(Level.SEVERE, "An Exception generate", ex);
} finally {
// Close all resource.
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(pingDataBaseServlet.class.getName())
	    .log(Level.SEVERE, null, ex);
}
out.close();
}
}

@Override
public String getServletInfo() {
return "r4r.co.in-pingDataBaseServlet";
}
}
Output of Program
Previous Home Next