Creating a Servlet+JSP framework and consuming it in other projects - java

I wanna develop a small Framework to help us on reusability. It will have some Java components to handle Permissions, DAO, Logging, etc, and some Servlet components for handling Authentication, request parameters, etc. And also JSPs with HTML snippets, header, footer, CSS, JS, etc etc.
What I can't find out, is how to deploy this Framework as a jar lib, to be consumed by other applications. Where should WebContent, JSP, etc should be, and how to export it in Eclipse.

Where should WebContent, JSP, etc should be
In /META-INF/resources folder of the Java source folder. Just create one if it doesn't already exist.
and how to export it in Eclipse
Just as JAR into /WEB-INF/lib of a web project. You can let Eclipse automatically do that by specifying the shared project in Deployment Assembly of the dynamic web project.
You can also create the shared project from the beginning on as Web Fragment Project. It'll prepare the folder structure for you and automatically be added as Deployment Assembly of an existing project.
See also:
How do I include a JSP file from a different project into my project

Related

What is the proper java app directory structure with Tomcat?

I started to learn JSP and Tomcat Container, and I know how the directories must be stored, but I'm confused by several things:
If we use only web app, why do we need the rest of the folders here? (highlighted blue). Can I just delete them and load my project with "web" as root?
How can I initialize Gradle/Maven or use any other framework inside Tomcat web application? (e.g Where to put pom.xml file?)
The folder .idea and the file Tomcat-web-app.iml are necessary for your IDE, configuration is stored there. If you delete them the project will fall apart in your IDE. You can delete the src folder as long as you're not using it.
pom.xml should be put on the root level of your project, so right under Tomcat-web-app/. Afterwards you can initialize it as a Maven project by adding framework support for Maven - right click on your project (Tomcat-web-app) and you should see it there. Refer to this guide if you're having issues. It is much more straight forward however to just create a Maven project from scratch and then copy in your web files.

Deploying GWT app with own external library

I have a GWT project [com.bob.gwt] that uses another library of my own, [com.bob.domain], which is used by both in the client and server. The domain project is exposed via gwt.xml project in com.bob.gwt.
When the server runs, it indicates that some Server classes from com.bob.domain could not be found in the web app, but were found on the class path and added. The WAR directory doesn't have the .java or .class files from com.bob.domain.
The com.bob.domain is added as a library in the Java Bulid Path Window's Projects tab, and also checked on the Order and Export tab in Eclipse. Do I have to set com.bob.domain as a jar and import that jar in com.bob.gwt? Can I just include the domain source in my gwt project so I can debug all at once without having to manually build a jar, and have everything in my WAR folder ready to go for deployment?
During development, it's convenient to use Eclipse's project reference mechanism instead of rebuilding "domain.jar" every time. In that case, it's safe to ignore GWT's warning.
When you perform the "real builds" outside of Eclipse (which I would strongly recommend), you usually can't make use of Eclipse's project reference (except maybe if you're using Ant4Eclipse), so you'll need to tell the GWT compiler (gwtc), and the server side compiler (javac), where to find everything they need, including the domain project's files.
Then, for deployment, you'll need to put "domain.jar" into the "WEB-INF/lib" of your war.

Multiple WebContent directories in a single eclipse dymanic web project

I have an existing project that I'm trying to convert correctly into an Eclipse Dynamic Web Project. I've mostly got it working, however there is on issue that I'm not sure how to fix.
The project has something like 2 modules that share the same Java classes, but need to be deployed as separate webapps into the app server (Tomcat 5.5).
So the folder structure is something like
/Webcontent_root
----/app1
..../WEB-INF etc...
----/app2
..../WEB-INF etc...
Is this something that can be controlled within eclipse?
Thanks
I guess, eclipse can't control project with more than one context.
Approach I.
I think you should create three projects: regular java project with classes, one dynamic web project for first webcontent and one for second. Than tune up "Java Build Path" and "Deployment Assembly" settings.
Approach II.
Or you can create two web projects, first with classes and webcontent, second with webcontent and link to first project ("Java Build Path" and "Deployment Assembly" settings).
Create 2 web project and 1 java project for the shared java source. Both web project can use the shared project as a jar file in their web-inf/lib
As #Nantipov says, use the deployment assembly screen to make web projects get the shared java project as a jar in the library filder.
Yes, it can--just not with the UI in WTP (deployment has always been a sore spot and ongoing effort to improve). The .settings/org.eclipse.wst.common.component in your project controls what Eclipse/WTP thinks of as web content root(s) in your project. Odds are you just need to create a new folder in the project and add it as a sibling to the existing element in that file, but with a correct source-path value.

Tomcat 6 conversion to Glassfish v3: servlet-api, el-api jars

I used ant when building my web app from eclipse to deploy to Tomcat 6 and referred to servlet-api.jar and el-api.jar within the Tomcat 6 release tree rather than pulling them in to my deployed library folder.
I am trying to deploy to Glassfish v3. I've pulled the jars from the www.java2s.com website.
Was this the right place to get them?
Did I need to do it at all? I searched within Glassfish and the jars weren't there
If I didn't need to do it at all, is there another course of action to follow to ensure the same functionality is available?
Seeing this question and the other questions you posted I have the feeling that you're doing things completely wrong. Here's just an answer which should get it all straight.
You should never have separate copies of servletcontainer-specific libraries wandering around in the classpath.
You should never put copies of servletcontainer-specific libraries in webapplication's WEB-INF/lib.
In an IDE like Eclipse, you should never add servletcontainer-specific libraries separately in project's build path.
In a nut: just do not touch servletcontainer-specific libraries at all. Don't even think of downloading them separately. That's plain recipe for trouble. Having separate libraries of different servletcontainer makes will only result in collisions in classpath. The servletcontainer should be downloaded and treated as its whole own.
In Eclipse, when integrating a servletcontainer (Tomcat or Glassfish), just add it in the Servers view.
To associate a dynamic web project with a specific servletcontainer (server) so that you can compile servlets and so on, you need to select it in the Targeted Rumtimes section of the project properties. Then everything will go well automagically, thanks to Eclipse smartness. That's also the place to change the servletcontainer implementation whenever necessary. When you're creating a brand new dynamic web project, you can just choose the desired servletcontainer implementation from the servers dropdown in the wizard.
When you want to create a WAR, simply rightclick the dynamic web project, choose Export and then WAR file. No need for a separate ant task or so.

Java Web Project referencing another Java project

I have a Java Project, for which I'm now creating a Web interface, using a Dynamic Web Project from Eclipse. The Web project consists of a single servlet and two JSP's. Something like this:
/JavaApplication
/src
/lib
/resources
/WebApplication
/src
/Servlet.java
/WebContent
/WEB-INF
index.jsp
other.jsp
Now, I need to reference JavaApplication from WebApplication, in order to use its classes to process web requests. What's the best way to accomplish this ? My idea is to create a .jar of the JavaApplication, containing all the .class files, /resources, and /libs. In this way, I could include the .jar in the web application, and I could have a single .war file that contained the entire application.
What do you think? How is this problem typically solved ?
Note: I don't want to convert the Java Project into a Web project.
In Eclipse project properties, add the project to the Java EE Module Dependencies (Eclipse 3.5 or older)
or Deployment Assembly (Eclipse 3.6 or newer) entry in the project properties.
This way Eclipse will take care about doing the right thing to create a WAR out of this all (it will end in /WEB-INF/lib). No other configuration is necessary, even not some fiddling in Build Path.
Under Eclipse, you can declare Project References for a given project, the web application in your case. To do so, right click on your web application project, then go for Properties > Project References and select the JavaApplication project. This should allow you to call code from the JavaApplication project from the WebApplication without having to build a WAR. This is a solution for development.
For standard deployment (outside the IDE), you should indeed create a standard WAR. To do so, you'll have to package your JavaApplication as a JAR including the .class files and the files under /resources but not the libraries it depends on (JARs under /lib). These dependencies will actually end up in the WEB-INF/lib directory of the WAR, beside the JAR of your JavaApplication. These steps are typically automated with tools like Ant or Maven.
Connecting java app to web app for development :
right click on web project :
properties>project references> add the java project you want to refer
Now in properties tab of web project go to
properties>deployment assembly> add the project manually and run the app
Consider moving up to EAR level, if your web container supports that.
The tricky part with shared code is where should the common code be put. A copy pr web application? A copy in the web container? Overdoing the "share these classes" might end up in class loader problems.
If you are creating two separate web applications refactor common java code into a separate Eclipse project and refer to it from both WAR projects.
EDIT: Apparently I have misread the problem description, and thought you asked about an existing and a new web application sharing code.
If you have an Eclipse project with your application, and another with your web frontend, then you can let your application export the necessary resources which the "Export WAR" in Eclipse Java EE can wrap up in a jar file and put in WEB-INF/lib for you. You need to say this explicitly with a checkmark in Properties -> Java EE Module Dependencies for your web project. Expect you have to experiment a bit - this took me a while to learn.
Typically you would create an API interface using remote service beans from the Java application that expose the methods that you want to invoke in the web application. You would include a proxy of the API interface with your web application that calls the remote service bean in the Java application. Remember that you will need to register the remote bean in the web.xml file.

Categories

Resources