0

I just started to write java and jsp program and I don't understand - why do I have to keep my all .class in WEB-INF directory?

Can I put them in the directory that I prefer but I still want to have the access to all Java default classes such as java.util.*? For instance,

app/
  class/
    Foo/
      Hello.class
      Hello.java
index.jsp

Is that possible?

index.jsp,

<html>
<head>
<title>Custom Class</title>
</head>
<body>

    <%@ page import="java.io.*,java.util.*" %>
    <%@ page import="javax.servlet.*,java.text.*" %>
    <%@ page import="Foo.Hello" %>

    <%
      Date date = new Date();
      out.print( "<p>" +date.toString()+"</p>");

      SimpleDateFormat ft = new SimpleDateFormat ("EEEE");
      out.print( "<p>" + ft.format(date) + "</p>");
    %>

    <%
    Hello hello = new Hello();

    hello.setMessage("Hello There!");
    out.print(hello.getMessage() + "<br/>");
    out.print(hello.sayHello());
    %>

</body>
</html>

EDIT:

app/
      WEB-INF/
       classes/
        Foo/
          Hello.class
          Hello.java
    index.jsp
Run
  • 54,938
  • 169
  • 450
  • 748

1 Answers1

1

No, If it's a webapp, all your classes must be in WEB-INF/classes directory and all libraries must be in WEB-INF/lib directory. This is the standardized structure of a webapp supported by container vendors.

There might be web containers supporting various non-standardized structures but if you don't stick to the standard you will lose the portability of your webapp.

Tomcat has some good documentation on deploying webapps.

Amila
  • 5,195
  • 1
  • 27
  • 46
  • then how can I put all my .class in the live server? – Run Aug 11 '15 at 07:12
  • Just place them inside WEB-INF/classes directory and if you use any third party libraries, place them inside WEB-INF/lib. You can use java default packages like java.util without adding any dependencies. – Amila Aug 11 '15 at 07:14
  • does a live server let you access WEB-INF/? – Run Aug 11 '15 at 07:17
  • WEB-INF directory is inside your webapp (one WEB-INF per webapp). You can either deploy the complete directory structure or you can construct a single WAR file. I've edited the answer with a link to documentation on this. – Amila Aug 11 '15 at 07:20
  • Do you mean that as long as I have a WEB-INF in my app then it should be ok? See my edit. – Run Aug 11 '15 at 07:24
  • I know what actually I should do now - http://stackoverflow.com/questions/8823290/how-to-run-different-apps-on-single-tomcat-instance-behind-different-ports. – Run Aug 11 '15 at 07:45