0

I have written some java code that can download some table data from an sql server. It works fine when it is loaded as just a java project and consists of 4 classes: Server.java, Publication.java, Volume.java, SQLPublicationMapper.java. (The third isn't used.)

I am trying to use these custom classes in a jsp project. I have gotten the jsp project to work with tomcat and it works in the browser, but I can't find a way to make it use my custom classes. Where are they supposed to be located? Do they need to be imported?

I have tried placing the class files in: src/jsp, WebContent, WebContent\src. None of these appear to work.

I have spent multiple hours being stuck on this issue and searching but didn't find anything applicable, so I come here.

The index.jsp file is:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Dynamisk jsp side</title>
</head>
<body>
<h1>En test af java til at starte med</h1>
Klokken er (eller var sidste gang du trykkede): <%= new java.util.Date() %><br>
Jeg tæller til 20: <br> <% 
int x = 1;
while( x <= 20 ) {
%>
<p>X er lig <%= x %></p>
<%   x++;
} %>

<h1>Jeg outputter også hele databasen:</h1>

<%@ page import="dk.au.hum.imv.persistence.db.*,
java.sql.*,
java.util.ArrayList,
com.googlecode.totallylazy.numbers.Numbers.*"
%>

<%
//fetch all publications
System.out.println("All publications");
ArrayList<Publication> publications = SQLPublicationMapper.getAllPublications();
System.out.println("First 5 publications are:");
for (Number idx : range(1,5)) {
    System.out.println(publications.get((int) idx).title+" by "+publications.get((int) idx).author);
}
System.out.println("Out of a total of "+publications.size());
%>
</body>
</html>

Eclipse EE gives the following errors: line 32: Publication cannot be resolved to a type Multiple annotations found at this line: - SQLPublicationMapper cannot be resolved - SQLPublicationMapper cannot be resolved line 34: The method range(int, int) is undefined for the type __2F_jsp_2F_WebContent_2F_index_2E_jsp

As far as I can tell, the first error is due to Publication.class not being available. The second the same for SQLPublicationMapper (twice?). The third because of some error importing the functional java (totallyLazy) library. This error does not occur when I just run the java itself.

The appropriate jar files are included in the buildPath settings.

Any ideas?

CoderGuy123
  • 6,219
  • 5
  • 59
  • 89

2 Answers2

2

Try importing your custom class into the jsp page.

From my understanding it is bad to use java Code in jsp but If I wanted to I would import the custom class and use it.

<%@ page import="com.whatkai.framework.custom.StackController" %>
whatkai
  • 179
  • 1
  • 2
  • 13
0

You should use an import statement:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.whatever.Publication, com.whatever.Server"%>
brso05
  • 13,142
  • 2
  • 21
  • 40
  • It doesn't work. Using e.g. `<%@ page import="dk.au.hum.imv.persistence.db.*, java.sql.*, java.util.ArrayList, com.googlecode.totallylazy.numbers.Numbers.*, Publication.java" %>` just gives an error "Multiple annotations found at this line: - The import Publication cannot be resolved - The import Publication cannot be resolved" I have also tried just "Publication", "src/Publication", and "src/Publication.java". – CoderGuy123 Apr 24 '15 at 14:10
  • What package do you have it in? Is it in your project's source folder? Make sure to specify whatever package it is in... – brso05 Apr 24 '15 at 14:15
  • @Deleet make sure it is in a package. – brso05 Apr 24 '15 at 14:16
  • @Deleet make sure it is not in the `default` package you must create a package for it http://stackoverflow.com/a/2030159/4028085 – brso05 Apr 24 '15 at 14:17
  • Package? It isn't in any package. It is just some files I wrote. As I wrote in the question, I have tried placing them in different folders, none of which worked. Right now they are placed in all of them, just in case it would randomly start working at some point. – CoderGuy123 Apr 24 '15 at 14:17
  • @Deleet they must be in a package for the import statement to work...In your source folder you should create a package `com.deleet.classes` then put your classes in the package. `com.deleet.classes` is just an example you should choose a name that makes sense... – brso05 Apr 24 '15 at 14:19
  • @Deleet look at above link. – brso05 Apr 24 '15 at 14:20
  • Really? I have to create a jar file just to use .class files from the same project? This is not necessary when not using jsp. Here's a screenshot of the folder overview: http://postimg.org/image/65m7ybuyh/ – CoderGuy123 Apr 24 '15 at 14:21
  • @Deleet you don't need to create a jar file just a package. – brso05 Apr 24 '15 at 14:22
  • `import="jdbc.Publication"` I would recommend changing the package to something more unique like `com.deleet.jdbc` – brso05 Apr 24 '15 at 14:23
  • @Deleet nevermind that is in your regular program you should create a package the same way `right click source folder->new->package` then move your classes in the package and reference them that way in your import statement. – brso05 Apr 24 '15 at 14:25
  • Also you usually want your import statement at the top with all your other declarations look at my example answer. – brso05 Apr 24 '15 at 14:26
  • I don't normally use java, so my apologies for not understanding all the terminology. In python/R, a package is some library you import, not a project as it apparently is in Java. I solved the type problem by adding "jdbc.*" to the import statement. The range() function/method still does not work. – CoderGuy123 Apr 24 '15 at 14:36
  • @Deleet it's alright man don't worry about it...I'm glad you got it working! – brso05 Apr 24 '15 at 14:37