Accesing java objects in a jsp.

I've got the following problem: I have a java class that contains a connection to a jdbc database, and several functions that manipulate that database( insert ,delete,select..) I create an instance of this class in a servlet, and I need to be able to access this instance in a jsp, in order to dynamically create the response page based on what it is in the database !
Any ideas ?

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    List<RowObject> rowObjects = yourDAO.list();
    request.setAttribute("rowObjects", rowObjects);
    request.getRequestDispatcher("page.jsp").forward(request, response);
<table>
    <c:forEach items="${rowObjects}" var="rowObject">
        <tr>
            <td>${rowObject.value1}</td>
            <td>${rowObject.value2}</td>
            <td>${rowObject.value3}</td>
        </tr>
    </c:forEach>
</table>

Similar Messages

  • Using Java Objects in my JSP page... objects are in a different directory

    Hi,
    I would like to call and use an object from a JSP page. The java object is in one directory, the jsp is in another. When I call my object, I get a JSP error that says it can't find the object and lists the directory name as part of the file.
    For example, here are my two files:
    /java/MyObject.class
    /web/page.jsp
    When I make the call to new MyObject(), I get an error message saying _web.MyObject cannot be found.  Any ideas on this?
    Thanks!

    Okay, I figured it out. In case anyone else needs it, here is what I needed to do.
    1) I created my object in a package. The JSP page was looking for the object in _dir directory because a package wasn't specified.  I created a subdirectory in one of the directories in my classpath and named it the same thing that the package was named.
    2) Import the package
    3) Make sure all methods in the classes are public, since only public methods can be accessed from outside the package.
    Thanks for the help, it got me going in the right direction.

  • Share Java Object Instance between JSP's

    Hi@all!
    Can i share a Java Object Instance between JSPages?
    Example:
    Class x
    Public Attribute txt
    Object creation in x.jsp
    set public Attribute txt to "HelloWorld"
    goto y.jsp
    get value from txt
    or...
    a global package
    thnx for help

    Here is a tutorial that explains how to use jsps and javabeans together.
    http://www.roseindia.net/jsp/usingbeansinjsp.shtml
    Quick question is sounds like you are pretty early in your development phase. Have you considered adopting JSF. You may find it a bit easier.
    Regards.

  • Empty strings when passing a Java object to a Stored Procedure

    Hi,
    I'm using the interface SQLData to pass Java objects to StoredProcedures. All the object's attributes 'arrive' to the Stored Procedure ok, except the strings, which are empty.
    Here is my Oracle object:
    TYPE OBJ_ASJFF_OBJ1 IS OBJECT (
    ARG1 CHAR(3),
    ARG2 NUMBER(4),
    ARG3 CHAR(4),
    ARG4 NUMBER(7),
    ARG5 NUMBER(13,2),
    ARG6 CHAR(1));
    The nested table of that object:
    TYPE TAB_ASJFF_OBJ1 AS TABLE OF OBJ_ASJFF_OBJ1;
    The procedure declaration:
    PROCEDURE Pup_Instaura_Processo (                              x_crCert IN TAB_ASJFF_OBJ1,
    x_cResult OUT CHAR(4)
    My SQLData implementation:
    public void writeSQL(SQLOutput stream) throws SQLException {
                   stream.writeString(getArg1());     
                   stream.writeInt(getArg2().intValue());
                   stream.writeString(getArg3());          
                   stream.writeLong(getArg4().longValue());     
                   stream.writeBigDecimal(getArg5());
                   stream.writeString(getArg6());
    Can anybody help me?
    Thanks in advance
    Rui Gonçalves

    not exactly what you wanted but ingredients can be found at
    - JPublisher's docuemntation (especially "Type Mapping Support Through PL/SQL Conversion Functions")
    - http://otn.oracle.com/sample_code/tech/java/jsp/Oracle9iJSPSamples.html (Best Hotels PL/SQL Sample )
    - http://otn.oracle.com/tech/xml/xdk_sample/xdksample_093001i.html
    hope this helps
    Kuassi
    I have a Java Stored procedure which takes an instance of a different java object as its parameter.
    I need to do this from a pl/sql package - can anyone point me to a sample etc (looked on the website but don't see one) ?
    Andrew

  • Java.lang.ClassCastException in JSP page

    My JSP page:
    <%@page contentType="text/html"%>
    <HTML>
    <HEAD>
    <TITLE> JDBC Servlet/JSP Example </TITLE>
    </HEAD>
    <BODY>
    <%@ page import="myBeans.memoryBean" %>
    <%@ page import="java.util.Vector" %>
    <H1> JDBC Servlet/JSP Example </H1>
    <H2> <%= session.getValue("message") %>
    </H2>
    <UL>
    <%
         Vector vData = (Vector) session.getValue("res");
         myBeans.memoryBean mb;
         Object o;
         for (Enumeration e = vData.elements() ; e.hasMoreElements() ;) {
              o = e.nextElement();
              mb = (myBeans.memoryBean) o;
    %>
        <LI> <%= o.getClass().getName() %>
    <%      } // end for
    %>
    </UL>
    </BODY>
    </HTML>Notice that I don't even use the object I cast but I still get the error message:
    Exception:
    java.lang.ClassCastException
         at _memory._search._jspService(_search.java:66)
         at oracle.jsp.runtime.HttpJsp.service(HttpJsp.java)
         at oracle.jsp.app.JspApplication.dispatchRequest(JspApplication.java)
         at oracle.jsp.JspServlet.doDispatch(JspServlet.java)
         at oracle.jsp.JspServlet.internalService(JspServlet.java)
         at oracle.jsp.JspServlet.service(JspServlet.java)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)
         at org.apache.jserv.JServConnection.processRequest(JServConnection.java:314)
         at org.apache.jserv.JServConnection.run(JServConnection.java:188)
         at java.lang.Thread.run(Thread.java:534)When I comment out the line that castes my object my browser displays:
    JDBC Servlet/JSP Example
    Records Found:
        * myBeans.memoryBean
        * myBeans.memoryBean
        * myBeans.memoryBean Notice that the three objects that are returned are exactly the type that I caste to.
    Also, I did a getClass().getClassLoader() when I create the objects in my servlet code and again on the JSP pages for each object I pull out of the vector and the class loader matched.
    I even changed the package on my bean class and recompiled everything to make sure it wasn't a old .class file floating around.
    Could this have something to do with my classpath or where my classes are being placed? I found a similar problem here: http://forum.java.sun.com/thread.jsp?forum=33&thread=380437&start=0&range=15&hilite=false&q=
    but the explanation of what was done wasn't clear
    Anyone have any idea what's going on here?
    I am using:
    Oracle 9i
    Oracle HTTP Server Powered by Apache/1.3.12 (Unix)
    ApacheJServ/1.1
    Thanks in advance.
    - Linus

    Is ti at all possible that you have another jar / zip file with the same class file in it, seemingly away from the Server classpath ? In which case this could happen even though logically it shouldn't !!!

  • Unable to import a java class in a jsp file

    Hi,
    I am trying to import a java class in my jsp.
    ------------------jsp---------------
    <jsp:useBean id="form" class="com.company.portlets.searchApps.object.SearchOBJ" scope="request" />
    <jsp:setProperty name="form" property="*" />
    <input type="TEXT" name="projectID" value='<%= form.getProjectID() %>'>
    When I run the above JSP, it says that class 'com.company.portlets.searchApps.object.SearchOBJ' cannot be found.
    Why?
    My project structure looks like this:
    Project
    +Application Sources
    ++com.company.portlets.searchApps.object
    +++SearchOBJ.java
    +Web Content
    ++htdocs
    +++searchappsportlet
    ++++SearchAppsPortletShowPage.jsp
    ++WEB-INF
    +++index.jsp
    +Resources
    ++SearchApps.deploy
    Please advice...thanks!

    The way that JDeveloper organized the files was like this:
    C:\jdev1012\jdev\mywork\WksSearchApps\Project\classes\com\company\portlets\searchAPPS\object\SearchOBJ.class
    I am assuming that JDeveloper puts the file where they are suppose to be...if not, how do I change that?
    thanks,
    hussain

  • How i can make  my own connection in java source of a jsp page

    How i can make my own connection in java source of a jsp page (How to get connection from JNDI datasource address) ?
    imagine that i have a rowset in a web page , now i want to do some operation using
    plain JDBC , so i will need a connection object.
    I tried to get one of my rowsets connection but it return null ?
    what is best way to retrive a connection from JNDI datasource that we define for our project?
    for example if i have
    myRowSet.setDataSourceName("java:comp/env/jdbc/be");
    in web page constructor
    now i want a pure connection from the same datasource ? JNDI
    Thank you

    It is not hard to get your own connection from datasource.
    in your case you need to do like the the following code.
    i provide sample to show you how to catch the exception and create an statement .
    Connection con =null;
    try{
    InitialContext ctx = new InitialContext();
    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/be");
    con = ds.getConnection();
    java.sql.Statement st =con.createStatement();
    }catch(SQLException sqlex){
    sqlex.printStackTrace();
    sqlex.getNextException().printStackTrace();
    catch(NamingException nex){
    nex.printStackTrace();
    hth
    Masoud kalali

  • Retrieving an ArrayList object in a JSP page and loop it

    i have these statements in the java servlet class:
    request.setAttribute("searchresults",searchResults);
    request.getRequestDispatcher("/searchStaff.jsp").forward(request, response);
    return;the searchResults is the ArrayList object.
    i would very much like to know how i can retrieve this object in a JSP page and iterate through this ArrayList using JSTL. I know i will need to use the <c:forEach> tag but i don't know what i should include in the body of this tag.
    thanks a lot~~~~~

    <c:forEach var="columnName" items="${requestScope.searchResults}">
    ...

  • Invoke Java 3d from a JSP?

    Hi,
    I have a sample java code that invokes a Canvas3d and then invokes a 3D object.
    Our project goal is to create an online application-- A JSP page which will invoke the java 3d object.
    I am working on Oracle JDeveloper and OC4J server.
    Currently , the application runs thru a .java file.
    I want to know if it is technically possible to create a jsp page and transfer the 3d object generated by this java file onto the JSP?
    I tried renaming the main method of the java file to say testMain (made it static method).
    Then thru the JSP , i tried to call this testMain method.
    It doesnt work.
    thanks

    HI
    I was able to add a canvas3D to a webbrowser via an applet. I suppose you can always invoke an applet from a JSP.
    Venkat

  • Y in java objects are created on heap only n not on stack ?

    Hi ,
    I need a answer to the question " why all the objects are created on heap only in java and not on stack " ?
    In java a object is created when we specify new i.e. for example,
    consider the following cases.
    1.
    Vector v = new Vector();
    here object is created and the reference to this object is assigned to variable v. okie.
    2.
    when i write something like
    Vector vec ;
    Here i am creating reference variable for Vector. No object is created and no seperate memory is allocated for it. Here this reference is placed on the java stack along with other primitive data types.
    My question is, in C++ by writing case 2 i can create an object and it is creating this object on stack. So why in java object is not created on stack while i do like this ? Why in java objects are created on heap only ?
    If my question is out of interest of this forum i apologize for it. But if somebody can throw some light on it, it would be of great help.
    Cheers !!
    Dipesh

    Cross post!
    http://forum.java.sun.com/thread.jsp?forum=32&thread=260589

  • Is it possible to send Java Object as parameter to an applet

    Is there a way to communicate to the applet using java objects instead of String parameters.
    can we do the following while invoking an applet
    <applet code=some.class>
    <param name=name value=object/>
    </applet>
    in applet
    Object obj = (Object) getParameter("name");
    Please le me know if you have any suggestions.
    Thanks!

    in the applet:
    URL url = new URL("mypage.jsp?somearg=somevalue");
    URLConnection conn = url.openConnection();
    conn.connect();
    ObjectInputStream in = new ObjectInputStream(conn.getInputStream());and in the JSP (although servlets would be better for this):
    <%@page import="java.io.*, java.util.*"
    %><% // This is important... if you are going to use JSP, you can't allow any newlines,
    // which is why servlets are better for this type of thing...
    String somearg = request.getParameter("somearg");
    ObjectOutputStream objOut = new ObjectOutputStream(response.getOutputStream());
    objOut.writeObject(someObject);
    objOut.flush();
    objOut.close();
    %>

  • Displaying Complex Java Objects using netui

    I have a java object with the following structure.
    obj1
    int name;
    String ID;
    ArrayList obj2;//coniatns multiple obj2 objects
    obj2
    int name;
    String ID;
    ArrayList obj3; //coniatns multiple obj3 objects
    obj3
    int name;
    String ID;
    How and what netui tags can I use to display this kind of structure for obj1?
    I tried repeaters and it is not working for me.
    Also if I have a structure like this
    ArrayList obj4; //contains arrays's of multiple items.
    obj4
    String[] eventTime;
    String[] eventDate;
    How can I display this in a grid fashion using netui? The netui:gid tag only
    wirks with RowSet and I have an ArrayList?
    If I want to use simple jsp and not use netui tags what is the context for accessing
    my form that I have in my page flow from the jsp? e.g. if I have a form in my
    page flow called listForm which has gets and sets for
    String[] eventTime;
    String[] eventDate;
    what is the context I use in my jsp to print values of eventTime and eventDate
    in a grid like fashion without using nteui? I tried 'request' context but it
    does not work. In netui we can use actionForm or pageFlow etc but what about
    simple jsp context?
    Please reply soon. Thanks

    Try the <netui-data:getData> tag in the workshop documentation, It gives an example
    "Kush" <[email protected]> wrote:
    >
    I have a java object with the following structure.
    obj1
    int name;
    String ID;
    ArrayList obj2;//coniatns multiple obj2 objects
    obj2
    int name;
    String ID;
    ArrayList obj3; //coniatns multiple obj3 objects
    obj3
    int name;
    String ID;
    How and what netui tags can I use to display this kind of structure for
    obj1?
    I tried repeaters and it is not working for me.
    Also if I have a structure like this
    ArrayList obj4; //contains arrays's of multiple items.
    obj4
    String[] eventTime;
    String[] eventDate;
    How can I display this in a grid fashion using netui? The netui:gid
    tag only
    wirks with RowSet and I have an ArrayList?
    If I want to use simple jsp and not use netui tags what is the context
    for accessing
    my form that I have in my page flow from the jsp? e.g. if I have a form
    in my
    page flow called listForm which has gets and sets for
    String[] eventTime;
    String[] eventDate;
    what is the context I use in my jsp to print values of eventTime and
    eventDate
    in a grid like fashion without using nteui? I tried 'request' context
    but it
    does not work. In netui we can use actionForm or pageFlow etc but what
    about
    simple jsp context?
    Please reply soon. Thanks

  • Java Object in javascript

    How can i call a java object and use it inside javascript?
    For example if i instantiate an integer in a java class, i want to pass it to javascript, do some work on it like for example incrementing it, and then i want to return its value.
    Can i do this?

    why don't you write javascript code using jsp and then pass on the value to javascript variable.

  • Write to java object using netui:select tag

    Hello,
    I am iterating through a hashmap of java objects using a netui-data:repeater.
    <netui-data:repeater dataSource="{pageFlow.MyHashMap}">
    <netui-data:repeaterItem>
    <netui-data:repeater dataSource= "{container.item.HashMapOfEmbeddedObjects}">
    <netui-data:repeaterItem>
    <netui:select dataSource= "{container.item.type}" optionsDataSource= "{container.item.typeChoices}" /> </td>
    <netui:select dataSource= "{container.item.default}"
    optionsDataSource= "{container.item.defaultChoices}" /> </td>
    </netui-data:repeaterItem>
    </netui-data:repeater>
    </netui-data:repeaterItem>
    </netui-data:repeater>
    The java object has a field "HashMapOfEmbeddedObjects" that is itself a hashmap of java objects. I want to be able to make a selection and have it be written to the
    "type" and "default" fields of the embedded objects, but dataSource="{container.item.default}" does not save the value of the selection.
    Is there anyway to use the netui:select tag to write to the field on a java object? Any help is greatly appreciated.

    I have done it in the past. The java object need not be actionForm. The following write-up I found in edocs will help you achieve this.
    Page Flow-Scoped Form Beans
    Page Flow-scoped Form Bean instances have the same life-cycle as the Controller file instance. They are created and destroyed when the Controller file instance is created and destroyed. This makes Page Flow-scoped Form Beans useful for storing data that has been accumulated across many different JSP pages.
    To create a Page Flow-scoped Form Bean instance, construct a public member variable of the Form Bean in the Controller file.
    public class myController extends PageFlowController
    public MyFormBean pageFlowScopedBean = new MyFormBean();
    Once you have created a Page Flow-scoped instance of a Form Bean, you can pass the instance to action methods by using the @action form="form_bean" annotation.
    public class myController extends PageFlowController
    public MyFormBean pageFlowScopedBean = new MyFormBean();
    * @jpf:action form="pageFlowScopedBean"
    * @jpf:forward name="success" path="displayData.jsp"
    protected Forward submit( MyFormBean form )
    return new Forward( "success" );
    Each time the submit() method is invoked, it is passed the same instance of the Form Bean, namely, pageFlowScopedBean, the instance that was created when the Controller file instance was created.
    For more info go to http://e-docs.bea.com/workshop/docs81/doc/en/workshop/guide/netui/guide/conReqScopedVsPageScopedBean.html
    Good luck.
    --SJ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • HELP! Saving a JAVA OBJECT to database...

    good morning,
    I use POSTGRESQL.
    I wonder, is it possible to save a JAVA OBJECT (java.util.ArrayList) into
    database? So I can retrieve it in the save form as before.
    Thank you.

    http://forum.java.sun.com/thread.jsp?forum=31&thread=271751&tstart=0&trange=15
    Please DO NOT SHOUT it your subject line. (Sorry.)
    see wat i mean? it feels uncomfortable.
    as for your question, Yes!Create a java.io.ByteArrayOutputStreamObject and wrap it with an ObjectOutputStream. Call ObjectOutputStream's writeObject method to save your object, get the array from the ByteArrrayOutputStream and save in db. Reverse the process to read.

Maybe you are looking for