• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
  • Carey Brown
  • salvin francis
  • Claude Moore

Code confusion with JSP page  RSS feed

Posts: 1
Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying this code for a while and I am not able to print it >please helpme with the code. This is a jsp page "Datapage.jsp"

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" ; import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Data Page</title>
<form action="DataServlet" method="post">
<table border="1" width="303">
<td width="119"><b>id</b></td>
<td width="168"><b>name</b></td>
<td width="168"><b>roll</b></td>
<td width="119"><b>company</b></td>
Iterator itr;
List data = (List) request.getAttribute("data");
for (itr = data.iterator(); itr.hasNext();) {

<td width="119"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="119"><%=itr.next()%></td>

package com.example.DataServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

* Servlet implementation class DataServlet
public class DataServlet extends HttpServlet {

private static final String Page = null;
private ServletConfig config;

public void init(ServletConfig config)

throws ServletException {

this.config = config;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();

ResultSet rs;
List dataList = new ArrayList();
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/message", "root", "admin");
Statement s = con.createStatement();
String sql = "select * from message";
rs = s.getResultSet();
while (rs.next()) {
} catch (Exception e) {
request.setAttribute("data", dataList);
RequestDispatcher dispatcher = request.getRequestDispatcher(Page);
if (dispatcher != null) {
dispatcher.forward(request, response);

Master Rancher
Posts: 4042
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add some debugging logging into your servlet to see what is being returned by your query, and also where you are forwarding to.
For example Page is null...at least, I can't see where it is set.

There are a few things I would suggest about the code.
First off, create a model class to represent your data, instead of adding each individual bit of data from a row as a separate entry in your List.
Second, when handling your exception, print the stack trace, not just the exception, otherwise you wont know exactly where the error occurred.
Third, you're not closing your Connection, so you'll eventually run out.  You ought to be using a connection pool in a web app, but you can look into that at your own pace.
Finally, the resources (connection, statement and resultset) all ought to be closed in a finally block, or you should use a try-with-resources.
That way you ensure you don't leak any resources (which database connections are).

Oh, and JSP pages shouldn't have code snippets in like that.  Again, look into EL (espression language) to see how you can print out a list of stuff, and format it, in a JSP.
Saloon Keeper
Posts: 20655
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised you weren't mauled by the Bear for that.

As you have now discovered, it's a really   task to try and debug Java code that's embedded in scriptlets on a JSP page. So take Dave's advice. Move the Java code to a java class source file and read up on how to do model/view/controller design with servlets and JSPs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!