3

All I'm trying to do is make calls to my Java code from my .jsp pages. I have written some .jsp pages for a webapp, but as the project grows I want to start putting some of the code into .java classes or servlets.

Why doesn't Eclipse "see" everything in my /src or /lib /web or /WebContent or /WebContent/src or /WebContent/WEB-INF/lib or /Webcontent/WEB-INF/src as usable? After all, it "sees my sql jdbc .jar files and I can use them as soon as they are in /WebContent/WEB-INF/src (and not any other folder). But I wrote classes which are in packages, my code isnt in .jar file form, so eclipse is not picking up on them.

Dustin
  • 322
  • 3
  • 10
  • 2
    you should maybe do some tutorials on jsp with eclipse before you create a large project – Philipp Sander Mar 07 '14 at 16:17
  • This has nothing to do with eclipse, necessarily, unless you don't have the correct "version" of eclipse installed for what you are trying to do. Eclipse JEE is what you would need to create a web project. – Matt Mar 07 '14 at 16:23
  • Also, what web server are you deploying to? This would have an impact on it (apache, tomee+, weblogic, websphere, etc) – Matt Mar 07 '14 at 16:25
  • There are certain rules by which you have to play. You can't just put your files randomly anywhere. try this website www.javatpoint.com it has nice tutorials and will help you do jsp prjects on eclipse – Saurabh Jain Mar 07 '14 at 16:27

2 Answers2

1

You need to have the Java EE version of Eclipse installed, and create a Java Web Application, which will allow you to set up a web.xml.

Eclipse JEE which I have installed to set up web projects is: http://eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplerr

Your web.xml will definte your web servlets, which can be JSP's or Java classes that extend a servlet implementation.

This has less to do with your IDE (eclipse) than it does with Java Web Applications.

You should read this documentation for starters from Oracle about web apps, and go from there on setting up an IDE:

http://docs.oracle.com/javaee/1.4/tutorial/doc/WebApp.html

Edit based on user feedback

Import should be to class level:

<%@ page import="fully.qualified.SomeClass" %> 

Then from your JSP code:

<%  
  SomeClass someClass = new SomeClass();  
  someClass.helloWorld();  
%>  

Edit 2 based on feedback

Try this link to do using page include: http://www.coderanch.com/t/286168/JSP/java/Calling-Java-classes-JSP-page

Alternative Approach

  1. Add a new class to your web.xml, where you want to send your request/form data to. So your JSP would be 1 servlet, your other java class would be your other servlet.

  2. On your JSP, create a form that has an action of your new "TestProgram" servlet

Matt
  • 477
  • 1
  • 5
  • 19
  • I am using Java EE and I created a Java Web Application already. my web app already works (it logs into different databases, resets password or runs queries,then returns the results in a nice table). Now Im just trying to add calls to my personal java code. When I <%@page import "com.microsoft.sqlserver.jdbc.*"%> i can start making calls to the classes inside. Why cant I just include my packages like that and use my personal code? – Dustin Mar 07 '14 at 18:41
  • Try to import the specific class(es) not just .*. See this link: http://stackoverflow.com/questions/239147/how-do-you-import-classes-in-jsp for example of how to import. e.g. <%@page import "com.yourpackage.YourClass"%> Then you would be able to call YourClass.yourMethod() from your JSP page. – Matt Mar 07 '14 at 19:19
  • I updated my answer based on our discussion; be sure to accept the answer if it helps you solve your problem! – Matt Mar 07 '14 at 19:24
  • <%@page import="com.dustin.Test_function"%> An error occurred at line: 22 in the jsp file: /VitalPath.jsp Test_function cannot be resolved to a type 22: Test_function test = new Test_function(); 23: test.printRequestInfo2(request); But in editor mode it resolves and in the "package explorer" view it resolves and will take me right to the method call in "Test_function" – Dustin Mar 07 '14 at 19:33
  • 1
    If you can help I will def accept!! Im at the point Im ready to blow away my project area and start over with a clean workspace – Dustin Mar 07 '14 at 19:36
  • Try this link: http://www.coderanch.com/t/286168/JSP/java/Calling-Java-classes-JSP-page – Matt Mar 07 '14 at 19:49
  • Nice link, that's my exact problem. Im convinced now that <%@page import="com.dustin.Test_function"%> should have worked, and I'll assume there is something wrong with my workspace. I will blow away and start over. – Dustin Mar 07 '14 at 20:08
  • Thanks for the help. Eclipse is def buggy. I started from scratch, and something was still wrong. When I include <%@page import="com.dustin.Test_function"%> it (again) appeared to work in editor mode but I got "can only include type, 'com.dustin.Test_function' resolves to a package" I stopped tomcat and restarted a few times because I was convinced it was correct and on the 3rd restart it worked. very strange – Dustin Mar 07 '14 at 20:48
  • <%@page import="com.dustin.* %> works as well. Includes dont have to be to class level, '*' includes all classes in the package. This was prob all caused by me messing up the java "build path" or something like that along the way – Dustin Mar 07 '14 at 21:25
0

This answer to this question has nothing to do with my setup or import statements. The correct answer is that eclipse doesn't make it clear what code it "sees" (or that its compiling) when it runs the project. On my JSP page, it showed it as recognizing the call to my java code. However, when I ran the page, it was still compiling the older version of my java code, that didn't have my latest changes. I've found the best way to guarantee that the newest version of my referenced java code is compiled when I run my .jsp program (that calls the java) is to go to "Project->Build All" then go to "Servers", right click "Publish". If Publish isnt an option, and it thinks its already synchronized, then you may have to change the .jsp page, save it, then "Publish" will become active again.

Dustin
  • 322
  • 3
  • 10