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:
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.
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.
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.
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 inclu开发者_开发问答de statement or something? If yes, how to do it?
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
- How to avoid Java code in JSP?
- Hidden features of JSP/Servlet
- Beginning and intermediate JSP/Servlet tutorials
- DAO tutorial - JSP/Servlet
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.
... 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" %>
精彩评论