DCoder
DCoder

Reputation: 3488

How do I import my own java classes for use in JSP pages? In what directory should they reside? How do I put them on the CLASSPATH?

I am using eclipse and I have imported dynamic web project created in maven. Name of my application is StudentManagementSys. I have created a java class "info.java" with some methods in it and I want to use this methods in index.jsp page. Please tell me how to do import this java class in index.jsp.

Location:

\StudentManagementSys\src\main\resources\info.java

\StudentManagementSys\src\main\webapp\index.jsp

I am naive programmer so please provide detailed steps.

Upvotes: 1

Views: 2450

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

First of all, the convention is to start class names with an uppercase letter: Info and not info.

To be able to import a class, it must be in a package. It's a very bad practice to put classes in the default package. And Maven expects Java source files to be in src/main/java, not in src/main/resources (which is used for resources like properties files, XML files, etc., loaded from the classpath). So, create a folder tree under src/main/java (like com/mycompany/myproject), and put your Java file there. It should start with the following line:

package com.mycompany.myproject;

Then you can import this class as any other class.

But JSPs shouldn't have to import classes anyway. They should only use the JSP EL, the JSTL and other custom tags. No Java code in JSPs.

It looks like you don't master the basics of Java development yet. Start by learning them with simple, console programs before trying to develop a webapp, which is quite a complex task.

Upvotes: 1

Related Questions