0

Hi i trying to using my own classes in a jsp file and i cant just resolve the problem, i know there is some threads about it but still i cant get it to work.

i have this class Hej.java

public class Hej { String a; public Hej(String a){ this.a = a; } public String hej() { return a; } } 

and here are my jsp file Newfile.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="Hej" %> <html> <head> </head> <body> <%Hej a = new Hej(); %> <%=a.hej() %> </body> </html> 

my folders look like this

Projectname Java Resources src (default package) Hej.java WebContent NewFile.jsp 
2
  • What is your problem? Commented Jul 15, 2014 at 4:45
  • 1
    Does your deployment folder contains Hej.class under WEB-INF/classes directory? Commented Jul 15, 2014 at 4:46

1 Answer 1

2

First of all dont use scriptlet for any logic to be implemented and secondly your code

<%Hej a = new Hej(); %>

fails because you have a parameterized constructor in your class by you are initializing an object without an argument try ths

<% Hej a = new Hej("Hello World !"); %> 

One more thing instead of using default package create some package.

Example create a package named mypackage and drag the class inside it. then change the page import to something like this :

<%@ page import="mypackage.Hej" %> 
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, can not import without a package name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.