import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; import java.io.IOException; import java.io.PrintWriter; import java.sql.DriverManager; import java.sql.ResultSet; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * * @author ignite178 */ public class view extends HttpServlet { private ServletResponse response;
/** * 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(); try { /* * TODO output your page here. You may use following sample code. */ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet hit</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet hit at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } // <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 {
// try {
// try {
// Class.forName("com.mysql.jdbc.Driver").newInstance();
// } catch (InstantiationException ex) {
// Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
// } catch (IllegalAccessException ex) {
// Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
// }
// } catch (ClassNotFoundException ex) {
// Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
// }
// Connection con = null;
// try {
// con = DriverManager.getConnection("jdbc:mysql://localhost:3306/location", "root", "root");
// } catch (SQLException ex) {
// Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
// }
// String id=request.getParameter("id");
// String query = "select * from posting where ignite_id='"+id+"'";
// Statement s = null;
// try {
// s = con.createStatement();
// } catch (SQLException ex) {
// Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
// }
// ResultSet rs = null;
// try {
// rs = s.executeQuery(query);
// } catch (SQLException ex) {
// Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
// }
// request.setAttribute("data", rs);
// RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
// rd.forward(request, response);
// }
}
/**
* Handles the HTTP
* POST
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String Id = request.getParameter("id"); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/location", "root", "root"); String id=request.getParameter("id"); String query = "select * from posting where ignite_id='"+id+"'"; Statement s = null; s = (Statement) con.createStatement(); ResultSet rs = null; rs = s.executeQuery(query); request.setAttribute("data", rs); RequestDispatcher rd = request.getRequestDispatcher("view.jsp"); rd.forward(request, response); } catch (Exception e) { out.print(e.toString()); } finally { out.close(); } } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold>
}
import com.mysql.jdbc.PreparedStatement; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * * @author ignite178 */ public class hit 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(); try { /* * TODO output your page here. You may use following sample code. */ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet hit</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet hit at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } // <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 */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); String Id=request.getParameter("id"); String Name=request.getParameter("name"); String Home=request.getParameter("home"); String Location=request.getParameter("location"); String Gender=request.getParameter("sex"); String Choice=request.getParameter("choice"); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/location", "root", "root"); String sql="INSERT INTO posting (ignite_id,name,home,location,gender,choice) VALUES('"+Id+"','"+Name+"','"+Home+"','"+Location+"','"+Gender+"','"+Choice+"')"; PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql); ps.executeUpdate(); ps.close(); con.close(); } catch(Exception e) { out.print(e.toString()); } finally{ out.print("Submit Successfully"); out.close(); } } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold>
}
Ads