0

I am trying to learn some JSP programming with Eclipse and I am having some issues:

Here is what I have:
1) Have test.jsp file inside WebContent folder that simply displays "Hello World!". I selected file and clicked Run As. Asked me to restart Tomcat server. Clicked Yes and page opened inside Eclipse showing "Hello World!".
2) I wrote Car class and stored it inside Java Resources/src
3) Tried to reference Car class in my test JSP and everything seemed right. (code below)

<%@ page import="ws.example.*" %>
<%  
    car myCar = new car("dodge", "neon");
    out.println(myCar.getMake());
&>

4) Clicked "Run As" again and I got Hello World message. Clicked refresh, error showed up that car MyCar = new car("dodge", "neon"); cannot be resolved to a type.
5) Clicked Refresh again 2 times and second time message "Hello World!" showed up.
6) Kept clicking Refresh button and sometime I would have error, sometime Hello World. I understand that Hello World is coming from cash, but how can I avoid this? Very annoying.

Questions:
1) When I make change in my project, how can I make sure those changes are included when I start the project?
2) Where is the good place to store your classes? Obviously what I try didn't work.

UPDATED:

Here is my class:

package ppp;

public class MyCar {

    String make;
    String model;

    public MyCar(String make, String model) {
        this.make = make;
        this.model = model;
    }

    public String getMake()
    {
        return make;

    }
    public String getModel()
    {
        return model;
    }
}

here is my JSP code (NewFile.jsp):

<%
    ppp.MyCar car = new ppp.MyCar("", "");
%>

Project Structure:

 TestProject  
     - Deployment Descriptor: TestProject  
     - Java Resources  
         - src  
             - MyCar.java   
         - Libraries  
             - ...  
     - JavaScript Resources    
     - build  
     - WebContent  
         - META-INF
         - WEB-INF
         - NewFile.jsp

I just don't see any room for error here.

thanks

Rhangaun
  • 1,430
  • 2
  • 15
  • 34
bobetko
  • 5,019
  • 14
  • 58
  • 85

2 Answers2

1

1) When I make change in my project, how can I make sure those changes are included when I start the project?

Republish the server. It's an rightclick menu option of the server in Servers view. If in vain, rebuild the project and clean the server. You can configure automatic publishing in the server's properties.

enter image description here

How servers act on this depends on the server make/version and the plugin. For example, Tomcat is a poor publisher, you'd rather like to just restart the server. Glassfish with its plugin is a tremendous publisher, it basically happens realtime.

By the way, I never use Run as option nor Eclipse's builtin browser. It's known to be a poor one. I just start the server and then open the page in my own favourite webbrowser (e.g. Firefox, Chrome, etc).


2) Where is the good place to store your classes? Obviously what I try didn't work.

Just in a package somewhere in the runtime classpath the usual way.


Unrelated to the concrete problem: try working on your naming conventions (classnames should start with uppercase) and try to avoid putting Java code in JSP (it's a very poor practice).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1) Tried "Publish" and it behaves the same. On referesh sometime it shows error sometime shows "Helo World". 2) Can you explain that "in package somewhere in the runtime classpath"? Can I keep my class in Java Resources/src and reference it from jsp file? – bobetko Aug 22 '11 at 18:43
  • 1) What server make/version? Did you try a real webbrowser? 2) That's what I said. Just store your classes in a package somewhere in the runtime classpath. Yes, inside the `src` folder is perfectly fine. It can for example also be a subproject which is deployed as JAR of the WAR. It really doesn't matter, as long as it ends up in a package in webapp's runtime classpath. – BalusC Aug 22 '11 at 18:44
  • Tomcat 6.0. Yes I tried in real browser. It is the same. Refresh either shows an error or shows hello-world message. And, I can't reference my class. Any idea why the call (car myCar = new car("dodge", "neon");) cannot be resolved to a type. I think I properly referenced it. – bobetko Aug 22 '11 at 18:56
  • Oh. And you confused me with statement that I shouldn't put java code into JSP code. I thought the whole point of <% and %> is to execute java code on server side. What am I missing? – bobetko Aug 22 '11 at 18:58
  • Well, perhaps Tomcat is just still dirty (as said, it's a poor publisher when it comes to Java code). Try rightclicking it and then *Clean* or just restart it everytime you change the Java code. As to Java code in JSP files, just click the link and carefully read it. – BalusC Aug 22 '11 at 19:09
  • Uh. Just saw the post from somebody with the same problem: http://www.velocityreviews.com/forums/t297929-jsp-help-needed.html They don't specify what is the problem. They just say that there is no error in code and that eclipse has to be reinstalled. I already reinstalled Eclipse 5 times last week. I am so tired of this... – bobetko Aug 22 '11 at 19:18
  • As said, Tomcat is a poor publisher. It takes lot of time for this. Have patience. You should be able to read in the Tomcat server log in Eclipse's console to see if it has republished the webapp or not. Reinstalling Eclipse for this is pure nonsense. – BalusC Aug 22 '11 at 19:19
  • It is not published. It said that line 12 cannot resolve to a type. Line 12 is this: ppp.MyCar car = new ppp.MyCar("", ""); Name of my package is "ppp" I tried to reference it this way to avoid using Import. – bobetko Aug 22 '11 at 19:39
  • Oh, it's just a compilation error? Well, make sure that you didn't typo'ed the package and class name (case sensitive!) and that it's present in the buildpath (the `src` folder by default already is). This is not related to JSP, you would have exactly the same problem when doing so inside a normal Java class in the same project (a servlet, for example). – BalusC Aug 22 '11 at 19:41
  • You didn't put the class in a package. Didn't Eclipse give an error marker on top of the `MyCar.java` source file at the `package` line? You should put the file in a package. Create a package `ppp` in `src` folder and put the file in there. Eclipse can automatically do it if you choose the 1st fix option of the error marker. – BalusC Aug 22 '11 at 20:01
  • Mystery solved. In Eclipse, I had one project I created last week when I was attempting to create web service. I noticed that Eclipse would get stuck on validating this project, it would get stuck at 64%. I deleted that project and everything works fine now. As I suspected there were no errors in my code. The question is what that old project had to do with my new project? Should Eclipse keep my project separated? I wish I didn't see this. – bobetko Aug 22 '11 at 20:21
  • I have no idea. Perhaps you just created it the wrong way. Read the Eclipse manual for details how to do it properly. – BalusC Aug 22 '11 at 20:27
  • Thanks for sticking here with me. It eases the pain. :-) – bobetko Aug 22 '11 at 20:28
0

It turned out I had one project in my workspace that was corrupting the whole Eclipse. Not sure why and not sure how, but after I deleted that project my code started working. How I knew which project was bad? If I would do "Build All" (under Project) Eclipse would show "Validating MyProjectName" message in the Eclipse status line and it would get stuck on 60% and it would just hang on there for 10 or 15 minutes.

bobetko
  • 5,019
  • 14
  • 58
  • 85