I have a better sollution...maybe...have a jsp file get username and password. The information is then passed to the servlet for processing and data retrieval from mySQL database...Store data into arrayList and pass it to a jsp file for display. Here is some code...Servlet File.
public class myActorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String name;
private String user;
private String pass;
private String given_table;
private String tid;
private String firstname;
private String lastname;
private String action;
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PrintWriter out = response.getWriter();
name = request.getParameter("screenName");
user = request.getParameter("username");
pass = request.getParameter("password");
tid = request.getParameter("tid");
firstname = request.getParameter("firstname");
lastname = request.getParameter("lastname");
action = request.getParameter("action");
given_table = request.getParameter("tableName");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet JDBC</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello, " + name + " </h1>");
out.println("<h1>Servlet JDBC</h1>");
/////////////////////////
// init connection object
String sqlSelect = "SELECT * FROM `" + given_table + "`";
String sqlInsert = "INSERT INTO `" + given_table + "`(`firstName`, `lastName`) VALUES ('" + firstname + "', '" + lastname + "')";
String sqlUpdate = "UPDATE `" + given_table + "` SET `firstName`='" + firstname + "',`lastName`='" + lastname + "' WHERE `id`=" + tid + "";
String sqlDelete = "DELETE FROM `" + given_table + "` WHERE `id` = '" + tid + "'";
//////////////////////////////////////////////////////////
out.println(
"<p>Reading Table Data...Pass to JSP File...Okay<p>");
ArrayList<Actor> list = new ArrayList<Actor>();
// connecting to database
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javabase", user, pass);
stmt = con.createStatement();
rs = stmt.executeQuery(sqlSelect);
// displaying records
while (rs.next()) {
Actor actor = new Actor();
actor.setId(rs.getInt("id"));
actor.setLastname(rs.getString("lastname"));
actor.setFirstname(rs.getString("firstname"));
list.add(actor);
}
request.setAttribute("actors", list);
RequestDispatcher view = request.getRequestDispatcher("myActors_1.jsp");
view.forward(request, response);
} catch (SQLException e) {
throw new ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e) {
throw new ServletException("JDBC Driver not found.", e);
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
}
}
out.println("</body></html>");
out.close();
}
}
The display is done in the JSP File that is dispatched by the servlet and viewed...
<html>
<%
//Actor actor = new Actor();
ArrayList<Actor> list = new ArrayList<Actor>();
list = (ArrayList<Actor>) request.getAttribute("actors");
%>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Actor</title>
</head>
<body>
<h2>This is Actor Class</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<% for(int i = 0; i < list.size(); i++) {
Actor actor = new Actor();
actor = list.get(i);
//out.println(actor.getId());
//out.println(actor.getFirstname());
//out.println(actor.getLastname());
%>
<tr>
<td><%=actor.getId()%></td>
<td><%=actor.getFirstname()%></td>
<td><%=actor.getLastname()%></td>
</tr>
<%
};
%>
</tbody>
</table>
</body>
The input from the user is enter using a standard input form done in html in a JSP File....
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MySQL Database Website</title>
</head>
<body>
<h1>Hello World!</h1>
<!DOCTYPE html>
<html>
<head>
<title>Entry form</title>
</head>
<body>
<h1>Entry Form</h1>
<form name="InputForm" action="myActorServlet" method="Post">
<table>
<tbody>
<tr>
<td>Table:</td>
<td><input type="radio" name="tableName" value="product"/>Produce
<<input type="radio" name="tableName" value="customer" />Customer
<input type="radio" name="tableName" value="actor" checked="checked" />Actor</td>
</tr>
<tr>
<td>Enter your name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Enter screen name:</td>
<td><input type="text" name="screenName" /></td>
</tr>
<tr>
<td>
Database Username:
</td>
<td>
<input type="text" name="username" size="30"/>
</td>
</tr>
<tr>
<td>
Database Password:
</td>
<td>
<input type="password" name="password" size="30"/>
</td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="clear" value="Clear" />
</form>
</body>
</html>