JSP to servlet and back again.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JSP to servlet and back again.

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Posts
    3

    JSP to servlet and back again.

    I have been having a fun time with a project I am working on. I'm looking at passing a var from the URL to the JSP taking that var and using it in a query to the database, taking the result set back to the jsp to display on the site. However I cannot seem to get the resultset back to the jsp. Not sure I am even doing this correctly.

    How does this look?

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;

    import java.sql.*;

    public class SomeClass implements java.io.Serializable {

    private int someid=5;
    private String var1 ="";
    private String var2 ="";

    public Category() {}

    public void setRequest(HttpServletRequest req) {

    Connection conn= null;
    try
    {
    Class.forvar1("org.gjt.mm.mysql.Driver");
    conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "pass");
    }
    catch (ClassNotFoundException e2)
    {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    catch (SQLException e1)
    {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    synchronized(conn)
    {
    try
    {
    Statement statement = conn.createStatement();
    ResultSet rs = statement.executeQuery("SELECT var1,var2 FROM table WHERE someid='"+someid+"'");
    setVar1(rs.getString("var1"));
    setVar2(rs.getString("var2"));
    if (rs !=null) rs.close();
    if (conn!=null) conn.close();
    if (statement !=null) statement.close();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    public void setvar1(String var1) {
    this.var1 = var1;
    }

    public String getvar1(){
    return var1;
    }

    public void setvar2(String var2) {
    this.var2 = var2;
    }

    public String getvar2(){
    return var2;
    }
    }



    JSP
    <jsp:useBean id="Some" class="com.fh.db.SomeClass" scope="page" />
    <%=Same.getVar1()%>
    <%=Same.getVar2()%>

    or <%=var%>

    or <%=rs.VAR1%>
    There is no such thing as a problem, unless the servers are on fire.
    Sometimes great opportunity comes brilliantly disguised as bad news.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    A resultset cursor is always in the 'crack' before the first (next) record. You will have to do
    at resultSet.next() to access the first (and the next...) record. If you fail to 'spool'
    to the end of a retrieved resultset the SQL server may get quite irate....
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center