Skip to content

Commit

Permalink
servlet to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Nareshit authored and Nareshit committed Nov 17, 2021
1 parent 0236efc commit 054a79b
Show file tree
Hide file tree
Showing 26 changed files with 281 additions and 17 deletions.
18 changes: 18 additions & 0 deletions EmployeeSearchApp/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v10.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="C:/oraclexe/app/oracle/product/11.2.0/server/jdbc/lib/ojdbc6.jar"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
31 changes: 31 additions & 0 deletions EmployeeSearchApp/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>EmployeeSearchApp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions EmployeeSearchApp/.settings/.jsdtscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="src/main/webapp"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
10 changes: 10 additions & 0 deletions EmployeeSearchApp/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=16
8 changes: 8 additions & 0 deletions EmployeeSearchApp/.settings/org.eclipse.wst.common.component
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="EmployeeSearchApp">
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<property name="context-root" value="EmployeeSearchApp"/>
<property name="java-output-path" value="/EmployeeSearchApp/build/classes"/>
</wb-module>
</project-modules>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v10.0"/>
<fixed facet="jst.web"/>
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
<installed facet="java" version="16"/>
<installed facet="jst.web" version="5.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Window
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//EmployeeSearchServlet.java
package com.nt.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class EmployeeSearchServlet extends HttpServlet {
private static final String GET_EMP_INFO="SELECT EMPNO,ENAME,JOB,SAL,DEPTNO FROM EMP WHERE EMPNO=?";
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// set response content type
res.setContentType("text/html");
//get PrintWriter
PrintWriter pw=res.getWriter();
//read form data
int eno=Integer.parseInt(req.getParameter("eno"));
//write jdbc code
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
//Load jdbc driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//establish the connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system","manager");
//create Jdbc Statement object
ps=con.prepareStatement(GET_EMP_INFO);
//set IN param values
ps.setInt(1, eno);
//execute the SQL query
rs=ps.executeQuery();
//process the ResultSet
if(rs.next()) {
pw.println("<h1 style='color:red;text-align:center'>"+eno+" Employee Details are </h1>");
pw.println("<table border='1' align='center'>");
pw.println("<tr> <td>Empno:: </td> <td>"+rs.getInt(1)+" </td> </tr>");
pw.println("<tr> <td>Emp Name:: </td> <td>"+rs.getString(2)+" </td> </tr>");
pw.println("<tr> <td>Emp Desg:: </td> <td>"+rs.getString(3)+" </td> </tr>");
pw.println("<tr> <td>Emp Salary:: </td> <td>"+rs.getFloat(4)+" </td> </tr>");
pw.println("<tr> <td>Emp DeptNo:: </td> <td>"+rs.getInt(5)+" </td> </tr>");
pw.println("</table>");
}//if
else {
pw.println("<h1 style='color:red;text-align:center'> Employee Details are not found </h1>");
}//else


}//try
catch(SQLException se) {
se.printStackTrace();
pw.println("<h1 style='color:red;text-align:center'> Problem in DB Interaction </h1>");
}
catch(Exception e) {
e.printStackTrace();
pw.println("<h1 style='color:red;text-align:center'> Unknown Problem </h1>");
}
finally {
//close jdbc objs
try {
if(rs!=null)
rs.close();
}//try
catch(SQLException se) {
se.printStackTrace();
}
try {
if(ps!=null)
ps.close();
}
catch(SQLException se) {
se.printStackTrace();
}
try {
if(con!=null)
con.close();
}
catch(SQLException se) {
se.printStackTrace();
}
}//finally
//add home hyperlink
pw.println("<h1 style='text-align:center'> <a href='input.html'>home </a> </h1>");

//close stream
pw.close();
}//doGet(-,-)

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}//doPost(-,-)

}//class
3 changes: 3 additions & 0 deletions EmployeeSearchApp/src/main/webapp/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

22 changes: 22 additions & 0 deletions EmployeeSearchApp/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">



<servlet>
<servlet-name>search</servlet-name>
<servlet-class>com.nt.servlet.EmployeeSearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>search</servlet-name>
<url-pattern>/empdburl</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>input.html</welcome-file>
</welcome-file-list>

</web-app>
17 changes: 17 additions & 0 deletions EmployeeSearchApp/src/main/webapp/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@



<h1 style="color:red;text-align:center"> Html to Servlet interaction </h1>

<form action="empdburl" method="POST">
<table align="center" bgcolor="cyan">
<tr>
<td>Enter Employee no::</td>
<td><input type="text" name="eno"> </td>
</tr>
<tr>
<td><input type="submit" value="search"> </td>
<td><input type="reset" value="cancel"> </td>
</tr>
</table>
</form>
2 changes: 1 addition & 1 deletion LinksApp/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v10.0">
<attributes>
Expand Down
6 changes: 3 additions & 3 deletions LinksApp/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=16
org.eclipse.jdt.core.compiler.source=1.8
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
<fixed facet="jst.web"/>
<installed facet="java" version="16"/>
<installed facet="jst.web" version="5.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
</faceted-project>
Binary file modified LinksApp/build/classes/com/nt/servlet/LinksServlet.class
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class LcTestServlet extends HttpServlet {
static {
Expand All @@ -19,22 +19,42 @@ public LcTestServlet() {
}

@Override
public void init(ServletConfig cg) throws ServletException {
System.out.println("LcTestServlet: init(-) method");
public void init() throws ServletException {
System.out.println("LcTestServlet.init()");
ServletConfig cg=getServletConfig();
System.out.println("init params===>"+cg.getInitParameter("driverClass")+" "+cg.getInitParameter("dbuser"));

}

/*@Override
public void init(ServletConfig cg) throws ServletException {
System.out.println("LcTestServlet: init(-) method");
String driver=cg.getInitParameter("driverClass");
String user=cg.getInitParameter("dbuser");
System.out.println(driver+" "+user);
}*/

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("LcTestServlet.service(req,res)");
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("LcTestServlet.service(req,res) doPost(-,-)");


//get PrintWriter
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
//write content to response object
pw.println("<h1 style='color:red;text-align:center'> Date and time ::"+new java.util.Date()+"</h1>");
pw.println("<h1 style='color:red;text-align:center'> Date and time ::"+new java.util.Date()+"</h1>");

ServletConfig cg=getServletConfig();
System.out.println("init params===>"+cg.getInitParameter("driverClass")+" "+cg.getInitParameter("dbuser"));
//close stream
pw.close();
}

public static void main(String[] args) {
System.out.println("main(-) method");
}

@Override
public void destroy() {
System.out.println("LcTestServlet.destroy()");
Expand Down
8 changes: 8 additions & 0 deletions MarriageApp-LifeCycle/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
<servlet>
<servlet-name>lc</servlet-name>
<servlet-class>com.nt.servlet.LcTestServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
<init-param>
<param-name>dbuser</param-name>
<param-value>system</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
Expand Down
2 changes: 1 addition & 1 deletion MarriageApp/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v10.0">
<attributes>
Expand Down
6 changes: 3 additions & 3 deletions MarriageApp/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=16
org.eclipse.jdt.core.compiler.source=1.8
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<fixed facet="wst.jsdt.web"/>
<fixed facet="java"/>
<fixed facet="jst.web"/>
<installed facet="java" version="16"/>
<installed facet="jst.web" version="5.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
</faceted-project>
Binary file modified MarriageApp/build/classes/com/nt/servlet/HomePageServlet.class
Binary file not shown.
Binary file modified MarriageApp/build/classes/com/nt/servlet/LcTestServlet.class
Binary file not shown.
Binary file not shown.

0 comments on commit 054a79b

Please sign in to comment.