views:

49

answers:

2

I just learnt basic Servlets and JSP technology and am designing a simple website using it.

The aim of the website is to sell products. The details of the products are stored in the database. I want to retrieve the data from the database and display the dynamic pages. I am using the MVC approach and trying to make it as OO as i can.

I am facing problem with the category page (which is intended to act as an index for the various products...i want to retrieve the categories stored in the DB and display them).

The details are as follows:

  1. I have created a simple java class which represents the table. The table's name is "Categories" in the DB...This class is named CategoryTable and contains instance fields representing various attributes of the table.

  2. A POJO named CategoryRetriever acts as my Model. it inserts the data of a particular row from the table into an object of CategoryTable and finally creates an ArrayList of the various CategoryTable Objects. This ArrayList contains all the retrieved data.

  3. The Controller of the Design is a Servlet names CategoryController. it creates and object of the CategoryRetriever and passes this to a JSP named CategoryDisplayer.

  4. All the things are getting compiled fine. Except the JSP. After deploying with WEBLOGIC. the jsp gives the following error.

G:\bea\weblogic81\server\bin.\myserver.wlnotdelete\extract\myserver_MiniProject_build\jsp_servlet__categorydisplayer.java:173: cannot resolve symbol symbol : class CategoryTable location: class jsp_servlet.__categorydisplayer CategoryTable tp = (CategoryTable)categoryContent.get(i); //[ /CategoryDisplayer.jsp; Line: 35] ^

From this what I can infer is that the JSP which is directly under the root project directory cannot find the CategoryTable class which is inside root>>WEB-INF>>source.

Does my JSP require an include statement or something? If yes, how to do it?

A: 

... cannot find the CategoryTable class which is inside root>>WEB-INF>>source...
I think you mean the WEB-INF/classes folder.

Does my JSP require an include statement or something...if yes, how to do it?
Yes. To import your missing class:

<%@ page import="CategoryTable" %>
Saheed
Do i have to specify the path?
Shahensha
As long as you have your class files (e.g CategoryTable.class) directly inside WEB-INF/classes then it should be available in the application classpath used by WebLogic.Naturally, if CategoryTable is in the package com.example.CategoryTable then you need to make sure:(1) Your import becomes: <%@ page import="com.example.CategoryTable" %> (2) The file: WEB-INF/classes/com/example/CategoryTable.class exists.
Saheed
the class files are directly inside WEB-INF/classes...so u used <%@page import="CategoryTable"%>I am getting the following error...G:\bea\weblogic81\server\bin\.\myserver\.wlnotdelete\extract\myserver_MiniProject_build\jsp_servlet\__categorydisplayer.java:18: '.' expectedimport CategoryTable; //[ /CategoryDisplayer.jsp; Line: 8] ^
Shahensha
Can you run the example here?: http://www.roseindia.net/jsp/ImportAttribute.shtml
Saheed
Be careful with roseindia.net. [It's cluttered of bad practices](http://balusc.blogspot.com/2008/06/what-is-it-with-roseindia.html).
BalusC
+1  A: 

At first sight, this is because you didn't put the class in a package. Packageless classes are not visible/importable from other classes inside a package on. JSP files are namely compiled and converted to a class extending JspServlet which is been placed in a servletcontainer-specific package. From there it cannot see/import packageless classes.

Whenever you want to be able to reuse/import a class somewhere else, always put it in a package.

E.g.

package com.shahensha.model;

public class Category {}

package com.shahensha.dao;

public class CategoryDAO {}

package com.shahensha.controller;

public class CategoryController extends HttpServlet {}

Note that packageless servlets will work on specific servletcontainers of specific versions in specific configurations (e.g. Apache Tomcat), but this isn't a valid execuse to refrain from placing classes in a package.


That said, this indicates that you're using scriptlets inside a JSP file. This is a bad practice. Rather use a servlet to prepare the data for JSP, use taglibs like JSTL to control flow in JSP page and use EL (Expression Language, those ${} things) to access backend data.

See also


Update: since you mentioned to use notepad/cmd, I would only emphasize the answer of Saheed more: you should not keep class (*.class) files in the same folder as source (*.java) files, but in /WEB-INF/classes. When using the above package examples, the classes should be placed in the following locations:

  • /WEB-INF/classes/com/shahensha/model/Category.class
  • /WEB-INF/classes/com/shahensha/dao/CategoryDAO.class
  • /WEB-INF/classes/com/shahensha/controller/CategoryController.class

If you were using an IDE, it will take care about compiling and building automagically.

BalusC