hi
i create a jsp and a servlet to get data from the data to be displayed in the jsp page. the code is below

ListCustomer.jsp

  <table id="customerListTable" border="3">
<tr >
    <th bgcolor=>ID</th>
    <th bgcolor=>Name</th>
    <th bgcolor=>Address</th>
    <th bgcolor=>TelNo</th>
    <th bgcolor=>Req</th>
</tr>
<c:forEach var="customer" begin="0" items="${requestScope.customerList}">
<tr>
    <td>${customer.cusid}&nbsp;&nbsp;</td> 
    <td>${customer.cusname}&nbsp;&nbsp;</td> 
    <td>${customer.cusaddress}&nbsp;&nbsp;</td>
    <td>${customer.custelno}&nbsp;&nbsp;</td>
    <td>${customer.cusreq}&nbsp;&nbsp;</td>
</tr> 

</c:forEach>
</table>


</body>
</html>

ListCustomerService.servlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { try {
            int id = Integer.parseInt(request.getParameter("id"));
            String cname = request.getParameter("cusname");
            String address = request.getParameter("address");
            int telno = Integer.parseInt(request.getParameter("telno"));
            String req = request.getParameter("req");

           //customerService.createCustomer( cname, address, telno, req);

            customerService.createCustomer(id, cname, address, telno, req);
            request.getRequestDispatcher("ListCustomer").forward(request, response);
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

but the data from the database does not disply in the page? i don't get any error either
what could be the problem be

Recommended Answers

All 7 Replies

CustomerService.java

 @Override
    public List getAllCustomers() {
        return entityManager.createQuery("select c.cusid,c.cusname,c.cusaddress,c.custelno,c.cusreq from Customer c").getResultList();
        //return entityManager.createQuery("select c.cusid,c.cusname,c.cusaddress,c.custelno,c.cusreq from Customer c").getResultList();
    }

Customer.java Entity class

@Entity
@Table(name = "CUSTOMER")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
    @NamedQuery(name = "Customer.findByCusid", query = "SELECT c FROM Customer c WHERE c.cusid = :cusid"),
    @NamedQuery(name = "Customer.findByCusname", query = "SELECT c FROM Customer c WHERE c.cusname = :cusname"),
    @NamedQuery(name = "Customer.findByCusaddress", query = "SELECT c FROM Customer c WHERE c.cusaddress = :cusaddress"),
    @NamedQuery(name = "Customer.findByCustelno", query = "SELECT c FROM Customer c WHERE c.custelno = :custelno"),
    @NamedQuery(name = "Customer.findByCusreq", query = "SELECT c FROM Customer c WHERE c.cusreq = :cusreq")})
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "CUSID")
    private Integer cusid;
    @Size(max = 50)
    @Column(name = "CUSNAME")
    private String cusname;
    @Size(max = 50)
    @Column(name = "CUSADDRESS")
    private String cusaddress;
    @Column(name = "CUSTELNO")
    private Integer custelno;
    @Size(max = 50)
    @Column(name = "CUSREQ")
    private String cusreq;
    @OneToMany(mappedBy = "cusid")
    private Collection<Vehicle> vehicleCollection;

    public Customer() {
    }

sry here is the other code
that is related to the problem

Member Avatar for LastMitch

Are you connected to database?

I don't see anything wrong with your query.

You only provide a query but you didn't provide how you connected to the db.

Do you have this on to of your file:

<jpa:repositories base-package="example.borislam.dao" factory-class="example.borislam.data.springData.DefaultRepositoryFactoryBean/>

I assume you didn't connected correctly.

Read this link:

http://www.borislam.com/2012/07/customizing-spring-data-jpa-repository.html

If it is connected then will show this query works:

@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),

the db connection is fine i think because i can add data to the database.
db connection is in the persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="autodealerPU" transaction-type="JTA">
    <jta-data-source>autodealer</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>
Member Avatar for LastMitch

db connection is in the persistence.xml

This is an example (the <properties> is the one connecting to the db not <persistence>):

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

   <persistence-unit name="my-pu">
     <description>My Persistence Unit</description>
     <provider>com.objectdb.jpa.Provider</provider>
     <mapping-file>META-INF/mappingFile.xml</mapping-file>
     <jar-file>packedEntity.jar</jar-file>
     <class>sample.MyEntity1</class>
     <class>sample.MyEntity2</class>
     <properties>
       <property name="javax.persistence.jdbc.url"
                 value="objectdb://localhost/my.odb"/>
       <property name="javax.persistence.jdbc.user" value="admin"/>
       <property name="javax.persistence.jdbc.password" value="admin"/>
     </properties>
   </persistence-unit>
</persistence>

If it's connected you can fetch the data not add because so far all your query is fetch

hey how to i call a jsp from a servlet page?

and also how do i change the web page that loads fist when i run a web application?

appreciate a reply
thanks

Member Avatar for LastMitch

hey how to i call a jsp from a servlet page?

Read this (it has an example how a servlet code works and explain how it works):

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/

and also how do i change the web page that loads fist when i run a web application?

I don't understand your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.