1

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.

DCoder
  • 3,486
  • 7
  • 36
  • 68
  • Since you're a naive programmer, you're naively trying to add Java code directly in your JSP. **This is a MUST NEVER DO by any means**. It is widely and better explained here: http://stackoverflow.com/q/3177733/1065197. – Luiggi Mendoza Oct 04 '13 at 20:44
  • I am trying to do the same thing with no success. I accept that this is not a good practice but I'm curious is it even possible? What is the syntax in the JSP file to import your own packages? – Jonathan Elkins Oct 14 '16 at 22:29

1 Answers1

1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255