import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
*
* @author Esimo
*/
public class NewServlet extends HttpServlet{
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
}
/*
int i=ps.executeUpdate();
if(i>0)
{
out.print("Success");
}
}
} catch (Exception e2)
{
System.out.println(e2);
}*/
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String pn=request.getParameter("name");
String pa=request.getParameter("age");
String pe=request.getParameter("email");
String pm=request.getParameter("mobile");
String pad=request.getParameter("address");
DataSource ds=null;
PreparedStatement ps =null;
try
{
Context mycontext = new InitialContext();
ds = (DataSource)mycontext.lookup("jdbc/mypo2");
if(ds==null)
{
out.println("JNDI context could not be found");
}
else
{
out.println("JNDI conext found...");
Connection con = ds.getConnection();
ps = con.prepareStatement("insert into telephone(pname,page,pemail,pmobile,paddress)values(?,?,?,?,?)");
ps.setString(1,pn);
ps.setString(2,pa);
ps.setString(3,pe);
ps.setString(4,pm);
ps.setString(5,pad);
// String query = "insert into telephone values() ";
//Statement stmt = con.createStatement();
int i=ps.executeUpdate();
if(i>0) {
out.println("Not Inserted");
} else {
out.println("Values Inserted");
}
} }catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}