Results 1 to 2 of 2
  1. #1
    shyamgsk is offline Member
    Join Date
    Sep 2013
    Location
    Hyerabad,India
    Posts
    20
    Rep Power
    0

    Default Default need help in displaying data from servlet,jsp

    Hello JavaProgrammers,
    i encountered with a problem in my web application
    when i try to display the records by using JSP page



    when i try to display the records by using JSP page it is throwing the below exception:

    HTTP Status 500 - java.util.NoSuchElementException

    type Exception report

    message java.util.NoSuchElementException

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: java.util.NoSuchElementException
    org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:393)
    displayrecord.doGet(displayrecord.java:52)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:393)
    root cause

    java.util.NoSuchElementException
    java.util.ArrayList$Itr.next(ArrayList.java:794)
    org.apache.jsp.displayrecord_jsp._jspService(displ ayrecord_jsp.java:108)
    org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:393)
    displayrecord.doGet(displayrecord.java:52)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:393)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.

    Apache Tomcat/7.0.34

    this is my display record servlet

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;

    public class displayrecord extends HttpServlet {

    List lst= new ArrayList();
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {
    Driver d= new org.apache.derby.jdbc.ClientDriver();
    DriverManager.registerDriver(d);
    Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/emp","root","root");
    PreparedStatement st=con.prepareStatement("select * from emp");
    ResultSet rs= st.executeQuery();
    while(rs.next()){
    lst.add(rs.getString("id"));
    lst.add(rs.getString("ename"));
    lst.add(rs.getString("des"));
    lst.add(rs.getString("sal"));
    }
    rs.close();
    }catch(Exception k)
    {
    out.print("error"+k);
    }
    finally{
    request.setAttribute("empData",lst);
    RequestDispatcher rd= request.getRequestDispatcher("/displayrecord.jsp");
    rd.forward(request,response);
    lst.clear();
    out.close();
    }
    }
    }




    this is my displayrecord jsp page:
    <%--
    Document : displayrecord
    Created on : 9 Sep, 2013, 7:35:23 PM
    Author : Sravan
    --%>

    <%@page import="java.util.*"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <script type="text/javascript">
    function editRecord(id){
    url="EditRecord";
    window.location.href = "http://localhost:8084/myweb/"+url+"?id="+id;
    }
    function deleteRecord(id){
    url="DeleteRecord";
    window.location.href = "http://localhost:8084/myweb/"+url+"?id="+id;
    }



    </script>

    <table align="center">
    <%Iterator itr;%>
    <%List data = (List) request.getAttribute("empData");
    for(itr = data.iterator(); itr.hasNext();){
    %>
    <tr>
    <% String s = (String) itr.next();%>
    <td><%= s %></td>
    <td><%= itr.next() %></td>
    <td><%= itr.next() %></td>
    <td><%= itr.next() %></td>
    <td><input type="submit" value="edit" name ="edit" onclick ="editRecord (<%=s%>);"/></td>
    <td><input type="submit" value="delete" name ="delete" onclick ="deleteRecord (<%=s%>);"/></td>
    <%}%>
    </tr>
    </table>
    </body>
    </html>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Default need help in displaying data from servlet,jsp

    When posting code, please use [code] tags [/code] in order to reatin formatting and make it easier for us to read.

    Java Code:
    public class displayrecord extends HttpServlet {
    
    List lst= new ArrayList();
    ... snip...
    while(rs.next()){
        lst.add(rs.getString("id"));
        lst.add(rs.getString("ename"));
        lst.add(rs.getString("des"));
        lst.add(rs.getString("sal"));
    }
    ... snip ...
    First off, you should never give a servlet state. That is it should not be given attributes.
    If two people accessed this servlet at the same time it is quite likely that the lst attribute would be corrupted.

    Second, you should really have a class that represents the data in a row in your result set, so something that simply represents the id, email, des and sal values. Then you would create one of those for each row, and add that object to the lst.

    But, and this is the cause of your problem, count the number of things you are adding to the list for each row.
    Now count the number you are iterating over to display them below:
    Java Code:
    <td><%= itr.next() %></td>
    <td><%= itr.next() %></td>
    <td><%= itr.next() %></td>
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. need help in displaying data from servlet,jsp
    By shyamgsk in forum Advanced Java
    Replies: 1
    Last Post: 09-16-2013, 11:43 AM
  2. Replies: 3
    Last Post: 11-24-2011, 01:18 PM
  3. Displaying results from the servlet to jsp web page
    By rushhour in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-17-2010, 12:13 PM
  4. Displaying Data
    By skiing in forum New To Java
    Replies: 4
    Last Post: 11-15-2008, 02:56 AM
  5. getting the default browser+MIME data
    By waleed_cs2000 in forum Networking
    Replies: 1
    Last Post: 04-08-2008, 01:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •