what are the steps in order to run java enterprise application - java

Sorry for this simple questions but i am too much confused with how to run java application. Whenever i ask some each one tells his own tools to proceed and i have learn that thing.
So provided i have simple basic eclipse with no plugin and i have downloaded the sample web application which uses spring , hibernate , mysql ,
The folder structure of app is
.setting
src--main,test
target
.classpath
.project
Now i want to run this using localhost in browser
what thing i need to do. i will tell from my knowledge and u guys can correct it
i don't want to use STS or install any plugin in eclipse.
I imported the project from eclipse
menu and i appeared on left window
As it uses spring do i have to add
the spring jar files in build path
of spring. or anything else
Same for hibernate jar files
Fior simple java app i used to
compile the class which contain the
void main function but i have no
idea which file to compile to run
this app
I added the mysql connector in build
path to connect with mysql in simple
java app. will same thing work here?
I knoow we need web server for that.
so if i want to install glass fish
server then how will i connect it to eclispe or that app. will tomcat be
ok than glass fish??? i know tthere
is eclispe ide with embedded glass
fish but i want integrate evrything
myself
IN browser i which url i need to use
to see that app
I don't know how did maven , appfuse fit in here. Can i run app without maven if yes then what does maven really do , i mean does it compile the java files or what. If i require maven then
Do i need to install it separately i
mean exe file or jar file
how to link with eclipse
I have read about building with
maven , what will ahppen after
building i mean what is the result
of building , will i see browser
after building or after building
there are some steps further. do
maven needs to link with web server
installed
sorry for basics questions but i am confused with all new trminology

Building a webapp is complicated. I will try and address your specific questions.
Utimately you don't need Maven or Appfuse, both can be very helpful.
Maven is a build tool. Maven and Eclipse do some similar tasks. Both can compile your code and manage a classpath. Maven handles a lot of things out of the box that Eclipse can't do by default. Maven can manage dependencies (i.e. download the spring jars for you) and create complex build processes.
If you are unfamiliar with Maven and creating a build file from scratch then it probably won't be much help. If you have a pom.xml (Maven build file) from somewhere else then Maven can be a big help. The result of Maven depends on how your build file is structured. The result is most often either a .war file (described below as step 5) or that your application is deployed directly to your web server (described below as step 6).
Appfuse is also not mandatory but can be useful. Appfuse will create a skeleton project for you. When it does this it will create a pom.xml (Maven build file) to automatically build your project. Appfuse by itself doesn't do anything other than help get projects started. Most people don't start building web apps from scratch anymore since getting the directory structure right and creating the build file can be a lot of work and it's easy to make mistakes. A tool similar to Appfuse is Spring Roo.
Tomcat, Glassfish, Jetty, and JBoss are web servers. They are also often called Servlet Containers which is just another name for a web server that hosts servlets in a certain fashion. Any of them will work for your project, they all have different learning curves. Integrating them into Eclipse may work for you, when I got started I found it was easier (although a little slower) to keep them separate.
In the JDK there is an interface named javax.servlet.Servlet. This is the interface that the entry class of your web application must implement. In particular the method service(ServletRequest req, ServletResponse res) is called every time there is a request for a URL. If you want your web application to respond to HTTP it may be simpler to extend the abstract class HttpServlet (which implements Servlet) instead. Most libraries (i.e. Spring web framework) have their own implementations of Servlet that are the entry point to the library.
I will now describe the basic process for building a web application. This is a complicated process and most people eventually automate it with Maven. I do not suggest trying to manually walk through the process yourself it can be very complicated but you can if you want. I am going to assume that you are placing all of your built files in a folder named $BUILD
Compile your source code. The compiled classes need to end up in a folder called $BUILD/WEB-INF/classes
Place all your jar files (external libraries) in a folder named $BUILD/WEB-INF/lib
Create a deployment descriptor, this is a file that tells your web server how to deploy your code. The most important thing in this file is a mapping from URLs to Java classes that implement Servlet. It should be named web.xml and put in $BUILD/WEB-INF
jar up the all this code with the root of the jar being at $BUILD. You could call this code application.jar
Rename the jar file extension to war. A war file is simply a jar file that has the required WEB-INF directory inside of it.
Deploy this war file to your web server (Tomcat/JBoss/Jetty/Glassfish/etc.), the process for doing this is different for each web server
That is the basics of web application deployment. Your web server will extract the war file and load all of the jars in the lib folder into the classpath. It will then take any URL requests it receives and send them to the appropriate Servlet implementations declared in your deployment descriptor.
As you can see this is not a simple process. This is the reason tools like Appfuse and Roo exist. They try and give you a starting point which does all of this basic stuff for you. If you are having trouble I would suggest trying again from scratch with Appfuse/Roo. As you start to get the hang of things I would also suggest learning more about Maven (or Ivy+Ant) to handle dependencies for you.

You should download the Java EE edition of Eclipse - it contains the code needed to work with enterprise applications.
You will also need an enterprise server (like JBoss or Glassfish) and the corresponding server adapter, which is a bit much for a beginner.
The easiest way to get started is to download Netbeans with Glassfish and use that instead - at least for now - as everything is configured correctly and it is very fast to get started! When you are more familiar with the way things work, you can switch back to Eclipse if you want to.

Related

New to J2EE - Hosting a Spring MVC based project using Tomcat 7 server on local dev machine (no Eclipse)

I am familiar with Java as a language but not JSP and Spring framework. I am trying to load an existing project and using vim as my IDE.
Originally, when working on it using eclipse, I installed openjdk7-jdk, downloaded the tomcat7 server from its website and extracted to a location, imported project in current workspace, set up the tomcat for server environment. I was able to see the application in browser.
I am looking for a way to use vim, installing tomcat from official repos and then either set up a new host for this application or adding a context for the same. I have tried both the ways but still not able to make it work. I get the tomcat standard 404 error page.
This answer here says that this may work if I create a .war file and place it in the webapps folder. Is this the only way to run a spring based application?
After working in ASP.Net, ROR, Django, I am assuming Java also has similar deployment structure.
One thing is that Tomcat is not required for spring based applications. But if you are implementing J2EE application on Tomcat, make sure you have Spring context defined in your webapps web.xml (inside WEB-INF directory).
The Spring Application context must be initialized through your ServletContextListener implementation.
Oups, seems there a long way to heaven ... More seriously you must pass by several steps (that a nice IDE like Eclipse or Netbeans can hide from you) :
create the source files for the application - you spoke of vim it can be used for that step
compile and build the application to a .war - this step is almost mandatory but i would not dare avoid it as I explain below - you can use low level tools like javac but you really should use ant or maven here ; if you are lucky, there is already a pom.xml (maven) or build.xml (ant) in the sources
deploy the war to a tomcat
In theory, it should be possible to individually compile the java file into .class, build a full hierarchy by hand and put that manually in webapps folder of tomcat. But never do that - or at least never say that I adviced you to do it :-)

How to automate the process of deploying a servlet using Eclipse and Tomcat?

I'm developing a web service application, and although the services are operating like they should, I think the time I take to develop, test and fix my code are being wasted. Here is what I'm doing:
I have a normal java project in Eclipse, where I have all my code
for the web service. I added a javax.servlet.jar library to the
build path, and receive the requests using
service(HttpServletRequest req, HttpServletResponse resp) from
HttpServlet, and print the result in JSON format. I use the main
method to test and fix some part of the code
When I feel confident that my code is fine I navigate to the
workspace, copy all the class files, stop tomcat server, delete the
old files deployed at web-inf, paste the new class files and start
the tomcat server.
My question is: Is there a faster way to do (ii) so I can iterate faster in the process of programming/testing/fixing? I have seen that I can achieve what I want using Dynamic Web Project, but I fear it might be difficult to migrate and cause problems with SVN.
Thank you!
There a lot of ways to achieve that.
You could for example configure your java project to output .class files in to the WEB-INF\classes of the docbase of the web-app hosting your web services.
right click on your project > Build path > configure build path > source
an then browse to the folder you want.
Start your tomcat from eclipse using tomcat eclipse plugin. It starts it by default in debug mode.
Or you could add - at least - the facet Dynamic Web Module to your project under
right click on your project > properties > Project Facets
and choose tomcat as runtime (click on new... to create a new one if not already done). In that case you can remove the extra jar you have for HttpServlet because the new runtime adds all the libs you need. After doing that you can just run project on the server using
right click > run as > run on server
and then choosing a server on which you want your project to run on. You can run in debug mode using debug as instead of run as.
In both cases you can take advantage from the Hot swap abilities of the jvm. The server will only need to be restarted if it is not possible to hot swap the code, i.e whe you change a method signature or add some fields to class.
I like to use Sysdeo tomcat plugin for eclipse. http://www.eclipsetotale.com/tomcatPlugin.html it lets you point tomcat to use your target directory in your project as a webapp. Allowing for hotswap code replacement and easy start, stop and restart. Start by downloading the plugin from the link above then making a file ROOT.xml in this sub directory of your tomcat instillation conf\Catalina\localhost\
The content would look something like this
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:\workdir\myapp\src\main\webapp" path="/"/>
Then you can add that projects library dependencies to tomcats classpath as well by going to eclipse->properties->tomcat->advanced also eclipse->properties->tomcat->source path and checking the box next to your project

Eclipse: Is it possible to publish Javascript edits to an external Tomcat instance

I am converting an application from Flex to Javascript. My workflow within Eclipse for Flex was to use Maven to start my Java web app in Tomcat and then have Eclipse configured to compile edited Actionscript files to a SWF and save it to my exploded WAR directory (that Maven/Cargo uses).
It worked very well for a long time allowing me to edit actionscript source code, flip over the browser, refresh the screen and see the changes.
I am new to Javascript however, and am struggling to get the same workflow up and running. The part I don't understand is how to tell Eclipse that I would like my edited Javascript files to be written out to a particular directory (that contains the exploded WAR). In my WAR project (a WTP dynamic web project) there is something that looks like a Javascript build path called "Javascript resources", but there is no output directory.
I would really like to continue to run Tomcat and Jetty via Maven if at all possible. I realize I can do what I want via WTP (M2E-WTP), but would prefer to use Maven/Cargo.
Denis's suggestion to create custom builder is probably best solution if you want to continue using pure Maven/Cargo approach with Eclipse.
If you are deploying to an exploded war directory, then another similar idea would be to use a File Synchronization plugin. These will automatically copy modified files to configured folders. See:
http://andrei.gmxhome.de/filesync/
https://wiki.onehippo.com/display/CMS7/Use+Filesync+Eclipse+plugin+for+faster+turn+around
-------------
FWIW, I don't think Maven:Tomcat/Cargo plugins are ideal for real-time web development, especially on the frontend side of things. They are useful mainly for controlled deployments or bootstrapping a server without initial setup. My thoughts:
Eclipse WTP used to be great for real-time web development, but I stopped using it a few years back as it just got way too hard to make it work correctly in a Maven environment. Fwiw, my preferred approach these days looks like this:
Do not install or use Eclipse WTP.
Use m2eclipse to integrate Maven with Eclipse.
Use Maven to do clean builds and generate exploded WAR directory in target folder.
Setup independent Tomcat server to load webapp from the exploded target folder.
I suspect the tomcat setup/startup could be integrated into Maven. It's not worth the extra complexity to me though.
Then, I configure JRebel (automatically via Maven) to handle java and web resource file changes. With this setup, I almost never have to redeploy or restart Tomcat. All changes (java, html, js, etc.) are seen immediately.
I think the same setup could be used without JRebel (for non-java files only) by configuring the web source folders as source folders in Eclipse with custom target output path being the corresponding directory in the exploded war directories. If that didn't work, then it would definitely work by using the custom builder or file synchronization solutions mentioned above.
Eclipse introduces the concept of "builder" to build a project. It comes with hardcoded builders such as the java compiler or the war builder of WTP.
But eclipse also enable to setup your own Builder using ant files : on your project, right click the project properties, go to section Builders, click on the new button.
You can use arguments to your ant file and use variables defined in by eclipse to build them
Do not forget to fill the refresh tab if you want eclipse to by notified of the produced files.
Do not forget the fill the Build options tab, section "Specify working set of relevant resources" in order to have your builder called each time a source used by the build file is changed inside eclipse.
Also go to the "targets" tab to specify during which type of build phase your ant file is called and which target is called.
I knwo this solution may not be the best for you since your build process will be described more than once but it may help you achieve your goal.

How do I properly deploy and structure projects in Eclipse against Tomcat?

I am relatively new Java developer that's been thrown in the deep end, my usual skillset lies in Microsoft products and C#.
However I have managed to write a nice web service using Restlet and incorporating Toplink and doing some database CRUD stuff, etc etc.
I developed this in Eclipse against Tomcat 5.5. I followed tutorials and examples and managed to throw something together that works.
I had all my jar files in WebContent/WEB-INF/lib directory of my project - I soon realised that when I export this as a WAR file and deploy in Tomcat, it takes these jar files with it and stores them local to the application.
Fine, but then when you try to undeploy the app, it only does it partially as Tomcat "holds on" to some of the jars it was using i.e. the Oracle JDBC, and Toplink ones. This makes sense as the web service was using these jars as there was a live Oracle connnection going on.
So then I thought I should have all these jars in a common place where all deployed apps can access them, that folder I believe is:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib
(in my case). This made total sense, it's logical as you don't want loads of apps on Tomcat all referencing the same jars. So I removed all the jars out of my projects lib folder and put them in the common\lib directory.
Now my app will not work properly - it can't find my source code in src, the custom bespoke code that I have programmed against these jars. It will only work if I jar up my source code and put in the directory stated above. Odd.
I am obviously very confused with class paths and build paths and all of that stuff, and must have got this sort of stuff all wrong as I am no Java expert, as I readily admit I have pretty much hacked this together - so can anyone explain to me in laymans terms how I should structure my project to get it working with jars held in a common folder in Tomcat. Or are there any good resoureces on the web to help explain to me what I should do.
Hope this all makes sense...
Here is a pic of my current project:
Sounds like you are not using WTP web project.
If you've installed 'Eclipse IDE for Java EE Developers' flavor of Eclipse, the WTP is already bundled in there. Otherwise here its update site -> http://download.eclipse.org/webtools/updates
Once you've installed WTP you should create 'Dynamic Web Project'.
In general I would recomment to put only your presentation layer here ( JSPs, CSS, HTML ) and put all pure java projects into standard 'Java' project that you later add as a dependency to 'Dynamic Web Project'.
WTP can run your web application under a number of containers, fortunately Tomcat is supported.
You will need to configure it through Window->Preferences->Server->Runtime Environments.
Once runtime envirnonment is configured, you can create your server runtime:
Right click in 'Servers' view.
Choose New->Server
Select server type: 'Tomcat v.5.5 Server'
(Optional) Change server name to whatever makes sense for you
Select server runtime environment: This is the Runtime Environemtn that you have configured previously
Hit 'Next' button
Add your 'Dynamic Web Project' project to 'Configured projects:' panel
Hit 'Finish' button
After the server is configured you can just run it, or you can put it in Debug mode. All source code referenced by dependent projects will be available for debugging.
NOTE:
From personal experience, I would not recommend using common/lib. Put all the jars that your web application relies upon into its WAR file. If you are worried about dependency tracking then start looking into Maven and m2eclipse.
Usually I put my jars in $CATALINA_BASE/shared/lib/
Please read this documentation for more clarification and specially Class Loader Definitions section.
Also restart the server after you copied the jars.
Go to Window > Show View > Other > Server > Servers, a servers tab will appear below with console tab. Click on your server and then F3, this will open server configuration.
Check Server Locations, and make sure you checked Use tomcat installation(takes control of tomcat installation) then click Modules tab below and it will show your installed modules, make sure the module is present or add it.

Eclipse: Have multiple Dynamic web projects contribute to a single war file?

I am in a situation where I basically want to be able to have a web project in Eclipse where the WebContents folder is merged from multiple projects instead of only a single dynamic web project.
If I have "a.jsp" in project A, and "b.jsp" in project B, I would like to end up with a single web application in the web container where "a.jsp" and "b.jsp" sit next to each other in the same folder. It would be perfect if all files, not just the jsp-files, could be merged like this.
This is to be able to have a core version of our application but being able to handle customer specific changes easily.
I know I can do this with suitable ant magic, but we want to have something that works well for our current Eclipse based development process. We will use JSR-330 dependency injection on Java classes, and essentially I'd like something along the lines of dependency injection but just for any resource and not just classes.
Can Eclipse do this?
If Eclipse cannot, would an EAR deployment be suitable perhaps? I currently have experience with WAR's only.
If using Maven is an option, then Maven overlays would be perfect here and it should theoretically be supported by the m2eclipse plugin. But I don't have any experience with that and there might be some issues (see MNGECLIPSE-599) so this would require some testing.
Nevertheless, the comments of MNGECLIPSE-599 are pretty interesting, especially this one:
Any love for this issue? Our entire team has moved to Netbeans for WAR development because of this. We are basically waiting for Servlet 3.0 to solve this issue for us (Servlet 3.0 would effectively negate our need to do overlays in Maven) Our company is big on reducing copy-paste so we use overlays to manage WAR media that must be common in our apps.
The way Java EE 6 would make overlays obsolete is not crystal clear for me (through Web Fragments?) but the fact is that Eclipse's WTP release with Java EE 6 support has been delayed to June 2010. So, until then, you'll need extra tooling (e.g. maven overlays) or should maybe consider switching to NetBeans.
I had a similar use case which I successfully resolved by using (as Pascal suggested) Maven.
I have a root web project (which also works standalone) and for each client I have a separate web project which is configured to overlay with the root web project. Furthermore, since each client has several environments I created a maven profile for each environment (test, prod, local, ...). I documented this a bit so if ever you are interested I don't mind mailing you the doc.
you don't need to change your "eclipse based development process" to use ant. Just register an Ant builder (right click project > Properties > Builders) and integrate the ant script with your eclipse process
you can use maven's multi-module options. (The maven plugin for eclipse is very good as well)
use can also use FileSync - not industry-standard but pretty powerful. It's used for developing on localhost, of course.
Here are a few tips for using FileSync:
setup which files/file patterns/dirs to copy to a target directory (Tomcat's webapps/application in your case). So as soon as you press "save", the files are copied.
make all absolute paths in the FileSync.xxxx.prefs relative by introducing a Linked Resource (preferences > workspace > linked resources), and using the link resource variable in the prefs file (lets call it WEBAPP_HOME)
commit the FileSync.xxxx.prefs file
tell each developer on the team to configure the WEBAPP_HOME variable. Thus the setup will not be valid for only one machine, but for each machine in the team.
I recommend to use the servlet 3.0 "Resources in bundled jar files" feature.
With this feature you can include web resources (html/xhtml/css/js/jpg/etc) in jar files (along with java class files, of course) and the web server will search the "META-INF/resources" folders of the jars for the resources.
More details here:
http://alvinjayreyes.com/2013/07/28/servlet-3-0/

Categories