Results 1 to 8 of 8

Thread: jsp help

  1. #1
    syedabu is offline Member
    Join Date
    Feb 2010
    Posts
    23
    Rep Power
    0

    Default jsp help

    the below program i store the total values corresponding to sum,sum1...
    how can i display like sum - sum1 into jsp tag?
    how to display that..please help me.....

    program

    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
    <%@page import="java.lang.*" %>

    <html>

    <head>
    <title><center>Reports</center></title>
    </head>
    <br>
    <body>

    <%
    try {
    float sum = 0, c = 0, sum1 = 0, d = 0, e = 0;
    ResultSet rs = null;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:sip");
    PreparedStatement ps = con.prepareStatement("select sum( total) tot,vname from artissue group by vname");
    rs = ps.executeQuery();
    %>
    <center><h1>The Artisian Issue's</h1></center>
    <TABLE cellpadding="5" border="1" style="background-color: #ffffcc;" ALIGN="center">
    <thead>
    <th>twt</th>
    <th>name</th>


    </thead>
    <%
    while (rs.next()) {
    c = rs.getFloat(1);
    sum = sum + c;
    %>

    <!%
    while ( rs1.next()) {
    d = rs1.getFloat(1);
    sum1 = sum1+d;

    %>

    <tbody>
    <TR>
    <TD><%out.print(sum);%><!%=rs.getFloat(twt)%></TD>
    <td><%=rs.getString(2)%></td>
    <% sum = 0;
    }%>

    </TR>
    <tr><td>
    </td></tr>
    </tbody>
    </TABLE>
    <%

    rs.close();
    ps.close();
    con.close();
    } catch (Exception e) {
    %>
    <%
    out.println("Unable to connect to database.");
    }
    %>



    <%
    try {

    float sum1 = 0, d = 0, e = 0;

    ResultSet rs1 = null;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con1 = DriverManager.getConnection("jdbc:odbc:sip");
    PreparedStatement ps1 = con1.prepareStatement("select sum(total),vname from artreceipt group by vname");
    rs1 = ps1.executeQuery();
    %>
    <center><h1>The Artisian Receipt's</h1></center>
    <TABLE cellpadding="5" border="1" style="background-color: #ffffcc;" ALIGN="center">
    <thead>
    <th>twt</th>
    <th>name</th>
    </thead>
    <!%
    while ( rs.next()) {
    c = rs.getFloat(1);
    sum = sum+c;
    %>

    <%
    while (rs1.next()) {
    d = rs1.getFloat(1);
    sum1 = sum1 + d;

    %>

    <tbody>
    <TR>
    <TD><%out.print(sum1);%><!%=rs.getFloat(twt)%></TD>
    <td><%=rs1.getString(2)%></td>

    <% sum1 = 0;
    }%>

    </TR>
    </tbody>
    <%

    rs1.close();
    ps1.close();
    con1.close();
    } catch (Exception e) {
    %>
    <%
    out.println("Unable to connect to database.");
    }
    %>

    </TR>
    </TABLE>
    <br><br>
    <a href="blank.html"><h2>Back To Home</h2></a>
    </body>

    </body>
    </html>

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Don't mix Java into JSP pages.
    Most of that should be in a servlet, with the database stuff being in its own access class.

    Then, you might be able to figure out what you need to do.

  3. #3
    syedabu is offline Member
    Join Date
    Feb 2010
    Posts
    23
    Rep Power
    0

    Default

    in this jsp program i print two values like sum using the tag
    <%=out.print(sum)%> the same i print sum1..
    but my doubt is how can i do like this?
    <%=out.print(sum-sum1)%>
    it is possible?please rly me?

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Have you tried it?

    And, again, you should not have Java code in a JSP page.

  5. #5
    syedabu is offline Member
    Join Date
    Feb 2010
    Posts
    23
    Rep Power
    0

    Default

    sorry...
    but i developed that for all....
    u know how to get value of sum and sum1 outside the tag <% %>...
    how to differentiate that....
    if u know u help me....
    after that i didnt use java code into jsp page...

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    You need to write a servlet that does your work.
    That's the proper way to do things like this.
    And, since you are accessing a db, you should have a db layer (ie class) that does that part.

  7. #7
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    yes, you can do that as value is already stored in sum and sum1. So you can manipulate.
    But, JSP is only used for presentation logic not for business logic. So, use servlet for writing java codes.

  8. #8
    javanar is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default assign other variable

    assign another float variable which is result = sum - sum1,

    then print it to the page like out.write(result+"");

    or <%= result+"" %>

    dont forget to concetanate with empty string as some browsers show incorrect values if it is not the case.

    by the way some IDE's (for example Netbeans) automatically show generated servlet for the jsp file at run time.

    You can use that servlet for servlet programming.

    And as said before dont include java code in jsp. at least create a class with static methods and variables and call them in jsp.

Posting Permissions

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