Deploy Struts2 WebApp Within An Eclipse RCP Application Via Jetty - java

Now here's a mess for you...
I am currently working on an Eclipse RCP application plugin. The goal of this plugin is to serve up a webapp for users to interact with within an Eclipse ViewPart (SWT browser control).
I would like for this webapp to be powered by Struts2 and I already have Jetty at my disposal for serving things up.
Is there any way at all I can deploy a Struts2 WebApp in this way? If more info is needed please ask!
EDIT #1:
Some more detail. The only way I know how to deploy a struts2 application currently is via a war file (hosting on tomcat, that is). This is not something I can do in this case. I need to somehow deploy struts2 internally in an embedded way using the Jetty server provided with the Eclipse RCP framework.
RESULT:
As it turns out you can deploy a WAR file (struts2 app in this case) with embedded Jetty. I found Joakim Erdfelt doing it here: Embedding Jetty as a Servlet Container

Struts2 web application can run in the servlet container like Jetty. Jetty also have an embedded option. Eclipse RCP uses Eclipse platform for extension and customization with plugins. There's an article of using Eclipse RCP with embedded Jetty server:
First, let’s add the jetty plugin to our dependencies. Open the tab
Dependencies in your plugin configuration. Then add these six
plugins to the Required Plug-ins:
javax.servlet
org.eclipse.equinox.http.jetty
org.eclipse.equinox.http.regstry
org.eclipse.equinox.http.servlet
org.mortbay.jetty.server
org.mortbay.jetty.util
In the list of plugins included at the launch of application you need
to change the Auto-Start value for three plugins to true (if you are
lazy, you can turn the default behavior to auto start but this is
another concern):
org.eclipse.equinox.http.jetty
org.eclipse.equinox.http.regstry
org.eclipse.equinox.http.servlet
Now if you run the application you can check if your server is
correctly running by accessing http://localhost. This should work
flawlessly except maybe if you are not allowed to run server in port
80 or there is already a server running in port 80.
You can change the port by adding an argument to the VM arguments in
Run Configurations. Add this value:
-Dorg.eclipse.equinox.http.jetty.http.port=8888. Change 8888 to
whatever port you want the server to be running.
Now if you are running the application, you can access it from the
port you mentioned before.
The next task is to define one (or several) servlet(s) that will serve
any request the server gets. To do this, you need to open the
Extensions tab from your plugin configuration and add a new
extension named org.eclipse.equinox.http.registry.servlets. After
that add new servlet. You need to mention the class name of the
servlet, and an alias for that. One note here is you need to add slash
in front of the alias. For example, if you want to make the servlet
accessible from http://localhost:8888/webserviceInterface, then the
alias value is /webserviceInterface. Of course, you need to
implement a servlet which will do the work you want.

Related

How to enable Hot deployment in JBoss using Redhat server connector extension in VSCode

I have made a small maven based web application in VSCode and trying to deploy it on JBoss using the Redhat Server Connector Extension.
But the hot deployment of the class files does not work in simple running JBoss server.
But Hot deployment does work in debug mode as 'Hot Code Replace' by setting the property 'java.debug.settings.hotCodeReplace' to 'auto'.
My inputs are from below links:
https://devblogs.microsoft.com/visualstudio/hot-code-replacement-for-java-comes-to-visual-studio-code/
and other SO links like:
How do I get Java "hot code replacement" working in JBoss?
Hot deploy on JBoss - how do I make JBoss "see" the change?
But it couldn't help.
Can you suggest something more about how it is simply possible in running JBoss?
(PS: Auto Build feature in VSCode is already enabled.
And It works fine in eclipse).
Hot deployment of web applications in VSCode's integrated RedHat Server connector can occur in following ways:
Exploded war folder: A folder is extracted from the original war and deployed to JBoss using the Exploded option. Here, the static changes like change in jsps and HTML are automatically reflected in the running application.
Hot code Replacement: Hot code replacement (HCR) is a fast debugging technique in which the Java debugger transmits new class files over the debugging channel to another JVM.
HCR only works when the class signature does not change; you cannot remove or add fields to existing classes when using HCR. However, HCR can be used to change the body of a method.
(source: https://devblogs.microsoft.com/visualstudio/hot-code-replacement-for-java-comes-to-visual-studio-code/)
Maven Install/build: It is actually not a preferred technique which someone will want but it is useful also. In this technique, you run maven: install which builds the project again and install it onto the server again.
Currently, VSCode for java and the server connector extension are new as compared to already available IDEs like Eclipse. So, we might expect more powerful functionality in coming future.

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

What happens behind the scenes of deploying a Spring MVC application to tomcat outside of eclipse?

I guess a drawback of using such an awesome IDE like eclipse is that you miss the point for what happens behind the scenes of an application. I'm a ruby developer so not a java veteran. So I've been coding a project in java and using the spring framework for IOC and MVC. Can someone explain to me what is going on when I select run on server in eclipse? Because eventually I will be deploying this masterpiece of an application to a Linux server. Here is my setup. I am using Spring MVC 3 and the maven plugin in eclipse. In the pom.xml file, I have stuff like latest spring release version, log4j, spring mvc, spring context etc.
I have been testing my application on localhost using the handy option of run on server in the eclipse IDE. The server configuration in eclipse is pointing to the tomcat directory location for where I have installed tomcat 7. Please demystify what happens behind the scenes and what I will need to do if I want to deploy this application on a production server. The more detail the better. Thanks a ton in advance.
Deploying a web application to Tomcat is as simple as this (assuming Tomcat is installed)
Bundle your application in a .war with the correct format.
Move the generated .war file to the /webapps directory of your Tomcat installation folder.
Run the /bin/startup.[sh|bat] script in the Tomcat installation folder.
Note that there are intermediate steps you can do to configure the deployment, like changing your context path. Go through the Tomcat documentation for details.
In step 3, Tomcat will extract the .war contents to a directory in the /webapps folder with the same name as your .war file. It will use this as the context path. The script itself launches a java process by putting the WEB-INF/[class|lib|...] onto the classpath along with some Tomcat libraries.
So Eclipse basically does all the steps above for you.
Ultimately you are deploying an web application that means you are deploying a war file to the server. Regardless of using frameworks like spring, struts anything.
SO a web application request starts from web.xml file. SO for spring mvc application, you are mapping all request coming from browser to DispatcherServlet and then this guy is responsible to manage whole life cycle of your application.
For more details of how MVC works please see
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html
So in order to deploy your application (a war) on server first of all you have to create a war from your source code. You can go to traditional approach to use java given utility like using jar from command prompt or you can use ANT, GRADLE, MAVEN and such build tool that creates war for you in automated way.
Spring is not doing anything extra for you. I believe you to research a bit more on how these tools works.
Once a war is ready for you, you can simply go to tomcat UI and there you will find options to deploy your war.
I hope it helps you.
All the majic happens in two places.
The first is your 'Servers' directory in the root of your Eclipse Package Explorer. These are your server configuration files that Eclipse will use (mostly) when it creates a new server instance.
The second is in the ./metadata/.plugins/org.eclipse.wst.server.core/ file system directory in your Eclipse workspace. This is where the tomcat application is actually deployed by eclipse.
The Tomcat Documentation is pretty good actually and helps explain how to do deployments. FYI, I do not know many people that use the Manager, from my experience most people deploy their applications by hand.

Debug GWT app locally/remotely while running on Tomcat

All of the tutorials/links/articles I have found for debugging GWT involve:
Using the Google-Eclipse plugin; and
Running GWT in Development Mode from inside Eclipse (with Eclipse as the web container)
I would like to know how to debug a GWT app while it runs from a non-Eclipse web container; specifically a full-blown Tomcat instance that is either running on the same machine (localhost:8080) or on a remote machine somewhere else.
Ideally, I'd be able to access my app normally, via browser, but as I interact with the app (clicking buttons, etc.), Eclipse would be running in Debug mode, and would hit breakpoints that I set ahead of time (before the deploy).
To my surprise, there isn't much out there on this that I could find, and certainly nothing definitive. So I was wondering if any battle-weary GWT veterans could shed some light on how I could do this.
If at all possible, I'd prefer not to install the Google-Eclipse plugin, unless that is the only option for achieving this. Thanks in advance!
I am not sure if you missed this documentation. I used to do remote debugging in my dev mode where my server is running in the VM. I can suggest you two solutions.
If you are using a version prior to GWT 2.5 you have to give the noserver argument in launch configuration providing the url for the host page in the server (Tomcat etc). I have extracted the steps from above documentation.
Configure your server however you need to; note the URL which contains the host page for your GWT application.
Arrange all your static content files (such as the host HTML page, images, CSS, etc.) on the server however you like.
Edit your development mode execution script (such as your Eclipse run configuration or the ant development build target generated by the GWT webAppCreator) and add or update the following options:
• Add the -noserver command line argument.
• Change the URL at the end of the argument list to match the URL you recorded in step #1.
Compile your application once using the ant build target. Ideally, you can use GWT's -war option to generate output files directly into your external server's static content folder. Otherwise, you'll need to copy the the GWT output folder from war/ to your external server's static content.
If you are using 2.5 or above you can use super dev mode. In super dev mode you can debug using the browser so no need of eclipse plugin. I have tried this and worked fine. However I was unable to load the java sources using source maps. Here is the documentation for superdevmode configuration.
It's really easy, just follow these steps:
Get google plugin for eclipse
The in eclipse, right click on your project and choose Debug as -> Web Application (running on external server)
Enter URL of your web app on tomcat (like
http://localhost:8080/YourApp/YourApp.html
and eclipse will give you new one - it will add stuff like ?gwt.codesvr=127.0.0.1:9997
And that's it. Just set up your breakpoints and debug!
Also take a look on this

Trying to use tomcat with eclipse

I am trying to use tomcat with eclipse. I have installed it all and got the tomcat plugin installed. I have put a .war file in the webapps folder of tomcat and have made sure autodeploy is set to true. The problem I am having is that I can't get tomcat to run this .war file. The tomcat server is started and running as it tells me under the servers tab at the bottom of eclipse. It also shows the name of my .war file under it and says synchronized meaning it at least knows its there. But I can't get it to work at all. Even when I go to the local host page it shows the title of apache tomcat but does not show the file. I want it to show me the .war file. It has the correct xml files and everything in it. Any help is much appreciated.
You may try to check whether the application is deployed or not. You can directly go to the Tomcat directory to see this.
Another possible thing could be, you might be typing the incorrect url.
http://localhost:8080/[your_application]
where 8080 is the port you have specified for tomcat.
You need to let Eclipse publish your web project for you when using the Eclipse Tomcat plugin instead of trying to manually deploy it youself.
The Tomcat plugin for Eclipe supports hot deployment whereby you can change jsp's etc without having to manually redeploy your web application.
If you have a valid web project in eclipse then you can check that it is being deployed by looking at the Server View.
Window, Show View, Other, Server
This window should contain details of your Apache server. (If no server is listed then you'll need to right click on the list and set one up.)
To check that your application is being deployed right click on your server and select Add/Remove projects. Check that your web project is listed. If it is not listed under Configured Project then add it.
If your project does not appear in the list then it probably wasn't setup as a Dynamic Web Project. You can fix this by by right clicking on the project to bring up project properties and then clicking on facets and then enabling Dynamic Web Project.
It is also worth having the Console view visible when starting/stopping Tomcat so that you can see the server output.
Start tomcat from the command line (not eclipse) and see if your webapp shows up. It should if you have indeed created a valid war file. If it doesn't work, check the logs.
You shouldn't be manually deploying the war file if you are using eclipse to launch tomcat. Running tomcat from eclipse does not necessarily use the same default workspace as the standalone tomcat. Check the configurations for the 'server': it may be that the eclipse launched server's webapp folder is empty.
If for some reason your webapp failed to initialize properly (error in the descriptor, an uncaught exception in a context listener, ...) tomcat will unload it and you won't see anything at http://localhost:8080/yourwebapp.
Is it there in the list of applications in tomcat manager?
if you havent done this...then follow the steps...
Go to http://localhost:8080/
Go to Tomcat manager and check if your application is there in the list of deployed applications. Try redeploying or starting the application if running=false. It usually tells you what is wrong when you do that.

Categories