0
package mycode;
import java.sql.*
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {

private static final long serialVersionUID = -4546189621945422719L;

String URL = "jdbc:mysql://127.0.0.1";
String USER = "root";
String PASS = "1234";

@Override 
protected void doGet(HttpServletRequest req,HttpServletResponse res) 
        throws IOException,ServletException{

}

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    try {
        Connection con = DriverManager.getConnection(URL, USER, PASS);

        String sql = "INSERT INTO cloud.cloud_table (Username,Password)" +
                "VALUES (?, ?)";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, username);
        pst.setString(2, password);
        pst.executeUpdate(); 


        out.println("<html><body>");
        out.println("THANKS FOR CREATING AN ACCOUNT");
        out.println("</body></html>");
    }
    catch (SQLException ex) {
        ex.printStackTrace();
    }

   }
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Login</display-name>
<servlet>
  <servlet-name>Login</servlet-name>
  <servlet-class>mycode.Login</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>Login</servlet-name>
  <url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>


+----------+----------+
| Username | Password |
+----------+----------+
|          |          |
+----------+----------+

<form action="login" method="GET">
    <div>
        <h4>Please enter your password and username</h4>
    </div>
    <div>
        Username <input name="username" type="text" />
    </div>
    <div>
        Password <input name="password" type="text" />
    </div>
    <div>
        <input type="submit" value="Submit" />
    </div>
</form>

Using java and tomcat, I have a simple form that takes in a username and password. I want to be able to retrieve the username and password from the form and insert it into a mysql database. However, right now after clicking the submit button on the form, nothing happens and the database is not updated. The database connection is working fine so I am not sure what the problem is right now. Any help would be appreciated.

6
  • Can you show that simple form code? Commented Apr 10, 2017 at 15:36
  • If you click submit and nothing happens, your html form might not be working. Commented Apr 10, 2017 at 15:41
  • @alayor I have added the code for the form Commented Apr 10, 2017 at 15:44
  • 1
    @qwertyayyy your form does not POST the data, it uses GET. So the Servlet's doGet method is called, which does nothing. Change your html form to method="POST". Commented Apr 10, 2017 at 15:45
  • 1
    @qwertyayyy add mysql jdbc jar to your build path and lib directory in WebContent/WEB-INF directory. Commented Apr 10, 2017 at 16:45

1 Answer 1

3

You are calling your GET method from the HTML form.

Change your action to POST.

<form action="login" method="POST">
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply, cant believe i was making such a stupid mistake. Anyway I have corrected it now but i am getting "java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1" error
You need to add MySQL Driver to the classpath. I recommend you investigate on your own about this and open a new question of you can't find anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.