user2089440
user2089440

Reputation: 1

Not able to pass values from Java code to JSP

I am trying to connect to a database and display the values read from the database column in a JSP table. For this I created a Java class that can connect to the database and read the values I need in a local class variable. Now on the JSP side, I am creating an object of the class and trying to retrieve the values from the database. On JSP side I am not getting the values in the Java class variable. However when I run the Java class standalone, I am able to display the database values. Just that I am not able to effectively pass the values to the JSP. Here is my code:

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="com.mypckg.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
    <%
        DBConnect dbCon = new DBConnect();
        String[] Codes = dbCon.getCode().split("##");
    %>

    <table>
        <tr>
            <td>Name</td>
            <td>Code</td>
        </tr>
        <%
            for (int i = 0; i < Codes.length; i++) {
        %>
        <tr>
            <td>
                <%
                    dbCon.getName();
                %>
            </td>
            <td>
                <%
                    dbCon.getCode();
                %>
            </td>
        </tr>
        <%} %>
    </table>
</body>
</html>

Upvotes: 0

Views: 493

Answers (2)

1and0
1and0

Reputation: 102

if you want a statement to executed you can use <% java command %> if you want a value to be printed use <%= java command %>

using java commands in that wat is not a good practice ... refer jstl tags ...that is more efficient and secure way :) ...

Upvotes: 0

Crollster
Crollster

Reputation: 2771

Instead of using

<%
   dbCon.getName();
%>

try using

<%= dbCon.getName(); %>

(and of course the same for dbCon.getCode())

This is the tag used when you wish to simply output the return value of a method.

Upvotes: 1

Related Questions