Announcement Announcement Module
Collapse
No announcement yet.
Call java file from jsp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Call java file from jsp

    I have main.jsp which is the page I will run.
    Code:
    <%@ page import="java.util.*" %>
    
    <jsp:useBean id="customer" scope = "application" class="hibernateconfig.hibernate.Customers" />
    <jsp:useBean id="customer1" scope = "application" class="hibernateconfig.hibernate.Class2" />
    
    <%
    List list = customer1.getmessage();
    out.println(list.get(0).getCustfirstName());
    
    %>


    I have Class2.java

    Code:
    package hibernateconfig.hibernate;
    
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import java.util.Iterator;
    import java.util.List;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import hibernateconfig.hibernate.Customers;
    
    import org.hibernate.*;
    
    public class Class2 extends HibernateDaoSupport{
        public Class2()
        {}
        
        public List getmessage() {
            Session session = null;
    
    
    
              SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
             session = sessionFactory.openSession();
              String SQL_QUERY ="from Customers";
        
        
              Criteria crit = session.createCriteria(Customers.class);
              List lst = crit.list();
           
    
              for(Object obj : lst){
             // Object[] row = (Object[]) it.next();
                     System.out.println("Name: " + ((Customers)obj).getCustfirstName());
    
              
              }
    
                 session.close();
    
                  sessionFactory.close();
    
              
            return lst;
        }
    }

    Customers.java

    Code:
    package hibernateconfig.hibernate;
    
    
    public class Customers {
        public Customers() {
        }
            public int CustomerId;
            public String custfirstName;
            public String custlastName;
            public String custEmail;
            
            
            public int getCustomerId(){
              return CustomerId;
           }
        
    
            public void setCustomerId(int CustomerId){
              this.CustomerId = CustomerId;
           }
        
            
        public String getCustfirstName(){
          return custfirstName;
        }
        
    
        public void setCustfirstName(String custfirstName){
          this.custfirstName = custfirstName;
        }
        
        public String getCustlastName(){
          return custlastName;
        }
        
    
        public void setCustlastName(String custlastName){
          this.custlastName = custlastName;
        }
        
        public String getCustEmail(){
          return custEmail;
        }
        
    
        public void setCustEmail(String custEmail){
          this.custEmail = custEmail;
        }
        
    }

    Customers.hbm.xml

    Code:
    <?xml version="1.0"?>
    
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="hibernateconfig.hibernate" auto-import="true" default-lazy="false">
      <class name="Customers" table="Customers">
            <id name="CustomerId" column="CUSTOMERID" />
            <property name="custfirstName" column="CUSTFIRSTNAME" />
            <property name="custlastName" column="custlastname" />
            <property name="custEmail" column="custemail" />
      </class>
    </hibernate-mapping>

    hibernate.cfg.xml

    Code:
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
            
    <hibernate-configuration>
    
        <session-factory>
            <!-- Database connection settings -->
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@ahmad:1521:orcl</property>
            <property name="connection.username">training</property>
            <property name="connection.password">training</property>
    
            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.OracleDialect</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            
            <property name="current_session_context_class">thread</property>
           
            <mapping resource="hibernateconfig/hibernate/Customers.hbm.xml"/>
        </session-factory>
    
    </hibernate-configuration>

  • #2
    the problem is in the jsp file. How can I call class2 function getmessage

    and then read from customers class using getcustfirstname in the jsp

    What is the syntax? thank you

    this one is wrong which is mine

    <%@ page import="java.util.*" %>

    <jsp:useBean id="customer" scope = "application" class="hibernateconfig.hibernate.Customers" />
    <jsp:useBean id="customer1" scope = "application" class="hibernateconfig.hibernate.Class2" />

    <%
    List list = customer1.getmessage();
    out.println(list.get(0).getCustfirstName());

    %>

    Comment


    • #3
      Is it no it isn't scriptlets... Shudder... Scream...

      The code you post here is so wrong in so many levels.

      1) Why on earth rebuild your SessionFactory each time, if you want a sloooooooooooow application that is the way to go
      2) You should be using the MVC pattern not calling methods in your jsp.
      3) Don't extend HibernateDaoSupport, heck you don't even use it.

      Judging from the code and your other thread(s) you are finding your way at once in a load of frameworks. I suggest:

      - The Spring Framework Reference Guide
      - The Hibernate Reference Guide

      If you can please zip your project (without jars) and post it (judging from the code you don't have much yet). I'll take a look and repost.

      Comment


      • #4
        thanks for help man. I am still in learning process. I am experimenting..

        I figured it out the problem. I need to import..
        <%@ page import="hibernateconfig.hibernate.Customers" %>

        Code:
        <%@ page import="java.util.*" %>
        <%@ page import="hibernateconfig.hibernate.Customers" %>
        
        <jsp:useBean id="customer" scope = "application" class="hibernateconfig.hibernate.Customers" />
        <jsp:useBean id="customer1" scope = "application" class="hibernateconfig.hibernate.Class2" />
        
        <%
        List list = customer1.getmessage();
        out.println(((Customers)list.get(1)).getCustfirstName());
        
        %>



        I will do it in another way tho. create a java file, create the table in it..and return a string..the jsp file reads the string which is a table

        for daosupport i removed it..

        the first part, i didnt get it well.. the building of sessionfactory.

        Comment


        • #5
          Code:
          SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          This creates a new SessionFactory each and every time you call the method. In general create it once, basically let spring do it for you.

          Your jsps, what you are doing there is basically not recommended anymore. It gets unreadable, unmaintainable and way to complex. That is why there is a MVC pattern and with spring it is quite easy to implement.

          That is why i said your code is "wrong on so mainy levels", basically every best practice you ignore . Also that is why I asked for the code so that I could give you the spring-hibernate way in achieving the same goal with proper design and configuration.
          Last edited by Marten Deinum; Sep 3rd, 2009, 06:19 AM.

          Comment


          • #6
            for now I will be using jsp to call .. jsp-hibernate way

            I am working with hibernate. When I get very good in it, I will use ADF to work.. and let jsp... its then where i will achieve same goal with spring - hibernate

            but now my focus is more hibernate.. understand well..


            I will open the sessionfactory in the constructor... call a function init() in it..


            thx for help

            Comment


            • #7
              I will open the sessionfactory in the constructor... call a function init() in it..
              Which still isn't the correct way . (I suggest you read the hibernate reference guide they explain how to do it correctly 1 instance of the SessionFactory for the WHOLE application).

              Regarding ADF that still doesn't mean that you can dishonor proper desing and best practices. With ADF you do basically the same (and if you use ADF all the way you won't even use Hibernate....).

              Even better if you do it correct now it becomes quite easy to drop jsp in favor of the ADF screen components (if that is all you want to use) by simply changing your configuration.

              Comment


              • #8
                If you follow best practices and use spring you will get something like the project attached. It is a maven based build (saves you a lot of hassle).

                Comment


                • #9
                  Hi

                  Do u have complete big web application project that has been developed using latest spring version and hibernate version.

                  Many Thanks - Kumar

                  Comment


                  • #10
                    Also do we have web application project cognos sdk project that uses spring and hibernate technologies.

                    Comment

                    Working...
                    X