title: How to properly display Mysql tables using servlets and java?
body: <p>I am newbie here. I have an assignment that requires to connect mysql, servlet and java (because i want to separate java code and html code. Previously, i combined the codes to make it easier and was rejected)
  So, basically, in mySql i write this,</p>
  
  <pre><code>create table login2 (username varchar (30), password varchar(30), designation varchar(10));
  insert into login2 values('lala','123','A');
  </code></pre>
  
  <p>and i create loginDisp.java in the servlet using eclipse. This is my command</p>
  
  <pre><code> package Servlet;
  
  import java.io.*;
  import java.util.*;
  import javax.sql.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  public class loginDisp extends HttpServlet {
    public void service(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException{
      //  String username=request.getParameter("Username");
       // String password=request.getParameter("Password");
  
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("&lt;html&gt;");
    out.println("&lt;head&gt;&lt;title&gt;Servlet JDBC&lt;/title&gt;&lt;/head&gt;");
    out.println("&lt;body&gt;");
    out.println("&lt;h1&gt;Servlet JDBC&lt;/h1&gt;");
    out.println("&lt;/body&gt;&lt;/html&gt;");  
    // connecting to database
    Connection con = null;  
    Statement stmt = null;
    ResultSet rs = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    con =DriverManager.getConnection 
    ("url/tablename","uname","pssword");
    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT * FROM login2");
    // displaying records
    while(rs.next()){
    out.print(rs.getObject(1).toString());
    out.print("\t\t\t");
    out.print(rs.getObject(2).toString());
    out.print("&lt;br&gt;");
    }
  
    } 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.close();
    }
    }
  </code></pre>
  
  <p>When i execute, it is well displayed. Hence, i started to make the Login.jsp as i want to make a text.box for user to insert username and password. This is my code</p>
  
  <pre><code> &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;body&gt;
  &lt;center&gt;
      &lt;div class="wrapper"&gt;
      &lt;br&gt;
      &lt;br&gt;
      &lt;h2&gt;Doctor&lt;/h2&gt;
  
      &lt;form name="form1" method="post" action="loginDisp" &gt; &lt;!-- onsubmit="return validateForm()" --&gt;
          &lt;table width="326" border="1" align="center"&gt;
          &lt;center&gt; &lt;tr&gt;
                  &lt;th width="138" scope="row"&gt;Username&lt;/th&gt;
                  &lt;td width="142"&gt;&lt;input type="text" name="Username"&gt;&lt;/td&gt;
  
              &lt;/tr&gt;
              &lt;/center&gt;
  
  
              &lt;tr&gt;
                  &lt;th height="31" style="width: 162px;"&gt;&lt;span class="style2"&gt;Password&lt;/span&gt;
                  &lt;/th&gt;
  
                  &lt;td width="142"&gt;&lt;input type="password" name="Password"&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
  
  
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;p align="center"&gt;
                      &lt;input type="submit" name="Submit" value="Submit"&gt;
                  &lt;/p&gt; ${message}
      &lt;/form&gt;
      &lt;/div&gt;
      &lt;/center&gt;
  
  &lt;/body&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and I get the data from mySQL displayed. I add another log.java in servlet because i thought when we need a data fetched from jsp to databased and displayed when be called. This is code in log.java</p>
  
  <pre><code>package Servlet;
  
  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  public class log extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  
   //Get username and password from the JSP page
  
  
  String username=request.getParameter("Username");
  String password=request.getParameter("Password");
  
  //Print the above got values in console
  
  System.out.println("The username is" +username);
  
  System.out.println("\nand the password is" +password);
      }
  }
  </code></pre>
  
  <p>The username and password inserted in login.jsp does not inserted automatically in mySQL, hence when i try to executed loginDisp.java , it will display only the data i inserted manually in mySQL.</p>