hi, am working on a bigger project than this, only using this as an example because i do not want to waste a lot of your time.
i have this code, its supposed to write to the database, but have no idea as to what i should put under action= "" in the html code.
note: am using netbeans.
i am able to read whats in a table but not writing to it using the web page created.
package paus;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
@WebServlet(name = "EnteredDetails", urlPatterns = {"/EnteredDetails"})
public class EnteredDetails 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
// html code
out.println("<html>");
out.println("<head>");
out.println("<title>Entering details</title>");
out.println("</head>");
out.println("<center>");
out.println("<body>");
out.println("<h1> Your Details </h1>");
out.println("<form method=\"post\" action = \"\" > ");
out.println("Username:<input type=\"text\" name=\"usertext\">");
out.println("Phone :<input type=\"text\" name=\"phone\"> ");
out.println("Email:<input type=\"text\" name=\"email\"> ");
out.println("<input type=\"submit\" value=\"submit\">");
out.println("</form>");
out.println("</body>");
out.println("</center>");
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);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//connection to database
String connectionURL = "jdbc:mysql://localhost:3306/passport";
Connection connection;
try{
String Username = request.getParameter("usertext");
String Phone = request.getParameter("phone");
String Email = request.getParameter("email");
Class.forName("com.myql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
String sql = "INSERT INTO usersTwo VALUES (?,?,?); ";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, Username);
pst.setString(2, Phone);
pst.setString(3, Email);
int i = pst.executeUpdate();
if (i !=0){
out.println("<br record entered>");
}
else {
out.println("failed to enter data");
}
}
catch (Exception e){
out.println(e);
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}