user3788653
user3788653

Reputation: 3

Eclipse using Classes in jsp

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

Upvotes: 0

Views: 327

Answers (1)

SparkOn
SparkOn

Reputation: 8946

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" %>

Upvotes: 2

Related Questions