Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay data from database in jsp - MVC
    primarykey
    data
    text
    <p>I am trying to come up with a code that complies with the mvc architecture and display the content of a database in the jsp page.. The connection and processing of data to be in java files and only the display of the data will be included in the jsp page.<br> I am using tomcat server. I have ojdbc6.jar and jstl-1.2.jar on my WEB-INF/lib folder.</p> <p>(Update) After changing my web.xml to point to index I got java.lang.StackOverflowError error.</p> <p>Is there something missing/wrong with the code? Also if I am not complying with the MVC design, let me know. Any idea would be appreciated. Thank you.</p> <p>Here is the code I am trying to run.</p> <p><strong>DBConn.java</strong></p> <pre><code> public class DBConn extends HttpServlet{ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Connection connection = null; Statement stmt=null; ResultSet rs=null; List&lt;Employee&gt; dataList = new ArrayList&lt;Employee&gt;(); try { // Load the JDBC driver String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName); // Create a connection to the database String serverName = "localhost"; String portNumber = "1521"; String sid = "xe"; String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid; String username = "hr"; String password = "hr"; connection = DriverManager.getConnection(url, username, password); stmt = connection.createStatement(); rs = stmt.executeQuery("select employee_id, first_name from employees"); while (rs.next()) { dataList.add(new Employee(rs.getInt("employee_id"), rs.getString("first_name"))); } } catch (ClassNotFoundException e) { // Could not find the database driver e.printStackTrace(); } catch (SQLException e) { // Could not connect to the database e.printStackTrace(); } finally{ if(rs!=null){ try{ rs.close(); }catch(Exception ex) { /* */ ex.printStackTrace();} } if(stmt!=null){ try{ stmt.close(); }catch(Exception ex) { /* */ ex.printStackTrace();} } if(connection !=null){ try{ connection.close(); }catch(Exception ex) { /* */ ex.printStackTrace();} } } request.setAttribute("data", dataList); String strViewPage = "index.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(strViewPage); if (dispatcher != null) { dispatcher.forward(request, response); } } } </code></pre> <p><strong>Employee.java</strong></p> <pre><code>public class Employee { private Integer id; private String name; //public constructors and //setter/getter public Employee(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre> <p><strong>index.jsp</strong></p> <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;JSP Page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hello World!&lt;/h1&gt; &lt;table border="1" width="303"&gt; &lt;tr&gt; &lt;td width="119"&gt;&lt;b&gt;Emp ID&lt;/b&gt;&lt;/td&gt; &lt;td width="168"&gt;&lt;b&gt;First Name&lt;/b&gt;&lt;/td&gt; &lt;tr &lt;form action="post"&gt; &lt;c:forEach var="employee" items="${data}"&gt; &lt;br/&gt; ${employee.id} ${employee.name} &lt;/c:forEach&gt; &lt;/form&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>web.xml</strong></p> <pre><code>&lt;servlet&gt; &lt;servlet-name&gt;DBConn&lt;/servlet-name&gt; &lt;servlet-class&gt;DB.DBConn&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;DBConn&lt;/servlet-name&gt; &lt;url-pattern&gt;/index&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p><strong>Error</strong></p> <pre><code> 09 6, 12 10:09:00 AM org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet DBConn threw exception java.lang.StackOverflowError at java.util.HashMap$KeySet.&lt;init&gt;(HashMap.java:872) at java.util.HashMap$KeySet.&lt;init&gt;(HashMap.java:872) at java.util.HashMap.keySet(HashMap.java:869) at java.util.HashSet.iterator(HashSet.java:153) at java.util.Collections$1.&lt;init&gt;(Collections.java:3382) at java.util.Collections.enumeration(Collections.java:3381) at org.apache.catalina.connector.Request.getAttributeNames(Request.java:1027) at org.apache.catalina.connector.RequestFacade.getAttributeNames(RequestFacade.java:300) at org.apache.catalina.core.ApplicationHttpRequest$AttributeNamesEnumerator.&lt;init&gt;(ApplicationHttpRequest.java:927) at org.apache.catalina.core.ApplicationHttpRequest.getAttributeNames(ApplicationHttpRequest.java:243) at org.apache.catalina.core.ApplicationHttpRequest$AttributeNamesEnumerator.&lt;init&gt;(ApplicationHttpRequest.java:927) at org.apache.catalina.core.ApplicationHttpRequest.getAttributeNames(ApplicationHttpRequest.java:243) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload