Eclipse and JSP programing. Where to store classes

2019-08-16 00:55发布

问题:

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

回答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.

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).



回答2:

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.