Question

I have a very beginner question as I'm just learning to work with DBs. I have a very simple HTML form with a textField for a name, an add button to add a name to a MySQL database, and a sort button to sort the names in the database and display them. I almost have it working, however when I hit the "add" or "sort" buttons I get the same messages that appear after a user clicks those buttons along with the sorted list of names. It looks like this when clicking the add button: Please go back and add a name or sort Bill Jones David G Debbie Downer Jane Doe

Would someone be able to advise me on what I have to change on my code to make just the list show up by itself when the user clicks sort? I am not a programmer just someone trying to learn.

Servlet code:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

 @WebServlet("/Main")
public class Main extends javax.servlet.http.HttpServlet implements
    javax.servlet.Servlet {
static final long serialVersionUID = 1L;

public Main() {
    super();
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException     {
    PrintWriter out = response.getWriter();
    String name = request.getParameter("name");
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection con = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root");
        Statement stmt = con.createStatement();
        try {

            //stmt.execute("DROP TABLE simple");
            stmt.execute("CREATE TABLE IF NOT EXISTS simple( name char(30))");
        } catch (Exception e) {
        }
        if (name == "" || name == null) {
            out.print("<h1>Please enter a name.<h1>");
        }

        else if (name != null) {
            String s = "Insert into simple values(\'" + name + "\')";
            stmt.execute(s);
            out.print("<h1>Please go back and add a name or sort.<h1>");
        }

        ResultSet rs = stmt.executeQuery("Select * FROM simple ORDER BY name");
        while (rs.next())
            out.println("<h1>" + rs.getString(1) + "</h1>");

    } catch (Exception ex) {
        System.out.println(ex);

    }
    System.out.println("Program terminated with no error.");
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException      {
    // TODO Auto-generated method stub
}

}

HTML code:

 <!-- Main.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DB Sort</title>
</head>
<body>
  <form action="http://localhost:8080/DB_Sorts/Main">
<center>
<br><br>
User Name:
<input name="name" type="text" value="">
<br><br>
<input name="add" type="Submit" value="Add">
<input name="sort" type="Submit"    value="  Sort  ">    
</center>
  </form>
</body>
</html>
Was it helpful?

Solution

Each submit button calls the same servlet with slightly different parameters.

If you wish to have different behaviour based on the button that is pressed you need to test which button was pressed.

If you click the add button the parameters name=???? and add=Add will be submitted. So if you add some code along the lines of.

if (request.getParameter("add") != null){
    // do add
} else if (request.getParameter("sort") != null){
    // do sort 
}

you should get what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top