Standalone/Embedded Web Server to make a portable Web app [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I come across the following options to make a portable web application in java
Spring Boot with embedded tomcat - Not sure how to build for windows, linux and mac
Jetty embedded server - http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html
Tiny - https://github.com/NanoHttpd/nanohttpd
JLHttp - https://www.freeutils.net/source/jlhttp/#whatsnew
Undertow - https://www.stubbornjava.com/guides/embedded-java-web-server and http://undertow.io/blog/index.html
Not sure which one is the best to go..Please advise.
Thanks.

I think the first choice(Spring Boot with embedded tomcat) is easy according to my experience. I have used the Spring boot embedded tomcat for the deployment of both Linux and Windows. I will explain how to deploy using embedded tomcat. Still then, the decision is yours to choose.
Ubuntu
1. Create executable jar/war file: You can create an executable war/jar by adding the following configuration in your pom.xml file or you can change the permission of the jar/war file using the Linux commands.
Add the following configuration in your plugin session of the pom.xml file as follows:-
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
Then open the terminal and navigate inside the project folder and run the following command:-
mvn clean install
As soon as you run the above command, you will get an executable war/jar(as you mentioned in the pom.xml) /target folder inside the project.
(or)
You can skip adding the above configuration and use Linux commands to make the war/jar as executable one. To do so, just open the terminal and navigate to the project folder. Then run the following command:
mvn clean install
You will get the war/jar in the /target folder which is not executable. You can make it executable using the following Linux commands:-
chmod 777 file_name
As soon as you get the executable jar/war, then you are ready to deploy it as a Linux service now. Because when you run it as a service, you don't need to restart it time after time whenever the server gets restarted. By default, the spring boot jar/war file has the ability to support init.d service commands like(status, start, stop, restart). To run it as a Linux init.d service, use the following steps:-
Copy your jar/war file to the /var folder(No problem even if you keep the jar/war in some other locations) using the following command.
cp file_absolute_path destination_path
e.g cp /opt/test/test.jar /var/test/
then create a symbolic link to the jar or war file as follows in /etc/init.d folder using the following command.
ln -s file_path symbolic_link_path
e.g ln -s /var/test/test.jar /etc/init.d/test
So the service has been created now. You could check the status of the service using the following command:-
sudo service service_name status
e.g sudo service test status
if you get an error like Unit test.service not found, then execute the following command to enable the service.
sudo systemctl enable service_name
e.g sudo systemctl enable test
and then execute the status command, you will get the status of the service.
In a similar way, you can use other commands also to start, stop and restart the service.
sudo service service_name start/stop/restart
Windows
Even though there are more ways, I would like to explain about running the tomcat as a service which is quite simple. Use to following steps to do so:-
Disable the embedded tomcat inside the spring boot application by adding the following command in application.properties
spring.main.web-environment=false
(or) add the following configuration in your spring boot application main class:-
#SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,
WebMvcAutoConfiguration.class})
Then build the war file using the Maven command mentioned above:
mvn clean install
Copy the war file from the /target folder.
Download a tomcat 7 or above version for windows from the official site tomcat.org
Copy your war file and paste it to the tomcat/webapp folder.
Open Command prompt and navigate to the tomcat's /bin folder
Execute the service.bat as follows to create a service.
service.bat install service_name
Open Services manager and you could see a service running with the service_name as you created.
when you select the service, you could see the options like start/stop/restart on the left side.
You can also make the service to run automatically whenever the system gets restarted. Select service -> Right Click -> Properties -> Startup Type -> Select Automatic

If you talk about java, Spring is the hottest and most demanding framework of all. Undoubtedly go for option 1. Few reasons to use Spring are as below
Less coding, more productivity
Integration with database simpler using Spring JPA
Template made easy by Spring Thymeleaf
Rest calls made concise with Spring Rest Template
Form validation modularized by Spring Validator.
Secure application using Spring Security
Define workflows using Spring State Machine
Numerous articles and example available over internet
Community and developer support easily available
.. and many more

Related

How to run a Spring Boot maven project in VSCode and how to config the base url of a spring boot web application

I have a Maven project which I need to run from VSCode. Right now the way I do it is:
Open the project folder in VSCode. Edit the java, js, html files etc.
Start my tomcat by running bin/startup.sh; tail -f logs/* ; in the apache tomcat's directory.
Open terminal in the project directory and run mvn clean install -DskipTests.
Then run cp /Users/path-to-my-project/target/myWebApp.war ~/apache-tomcat-8.5.23/webapps/ to copy the war file into the tomcat's webapp directory.
After which I can access my web application at localhost:8080/myWebApp.
Is it possible to do all of this in one click (or command) in VSCode. I know it can be done in Eclipse or IntelliJ but I want to work with VSCode.
I have installed the Spring Boot Extension Pack and Java Extension Pack in VSCode. I am just confused on how to setup the path to my tomcat, build the project and then copy the war file to the tomcat webapps folder.
Is it possible to do all of this in one click (or command) in VSCode. I know it can be done in Eclipse or IntelliJ but I want to work with VSCode.
To make your webapp visitable, just type:
Ctrl+`
in the VS Code to get a terminal, and then type:
mvnw spring-boot:run
in the application there is a suffix after the locahost:8080. So something like localhost:8080/mywebapp/...rest of the url. With the above method everything works but I loose that web app name suffix. Any idea how I can get it back?
In your application.properties file(in the src/main/resources folder in your spring boot project), add below line(for Spring boot v2.0.5):
server.servlet.context-path=/mywebapp
For older version, you may need:
server.contextPath=/mywebapp
See these links for reference:
How to set base url for rest in spring boot?
What is the purpose of mvnw and mvnw.cmd files?
how to run springboot app in visual studio code? is it possible or not?

How to Install & Deploy a java app (.jsp file) in apache tomcat

I have successfully installed apache tomcat & verified it to be working using the curl command
curl http://localhost:8080/
Tomcat is installed in
/var/lib/tomcat8/
webapps/ROOT/index.html
webapps/ROOT/META-INF
lib
I have received following structure of the java app in a zip file
App/
build.xml
deploy.sh
run.sh
www/
*.jsp files
WEB-INF/lib/*.jar files
I want to install and run this app on my server. How can I do that. Specifically I want to know where to place these folders. Do I need to run any of the deploy or build files. Do i need to install the jar files? If I directly run one of the .jsp files from curl command, it crashes due to missing java class.
Perhaps I am missing something obvious but I am new to java env and could not find it from google.
If you haven't tried it yet, consider using Maven to build your projects.
This will automate and simplify a lot of similar work for you, like creating a .war file and deploying your project to Tomcat. With Maven, you have to set the <packaging>war</packaging> in pom.xml (a project configuration file) and run mvn tomcat7:deploy command.
For details look at the above link to Maven docs or for instance check out this tutorial.
You can also use the Tomcat Manager under (default) http://localhost:8080/manager to deploy the .war file manually.

How can I deploy a spring-boot application on Windows Server 2012 R2?

I have a Springboot application that I've more or less completed and is running smoothly on my local host.
I also have a Windows server that I can now begin to play with. I've figured out that when I hit this server with its IP address in my browser it shows a Hello World page from Windows IIS at this location:
C:\inetpub\wwwroot\iisstart.html
Can I simply copy and paste the entire contents of my current springboot application into this folder and expect it to work? Can I copy and paste a JAR file? I have been unable to find a clear answer as to how to deploy my application to my server so that other users can access it. What should I do?
I have found that the instructions above do not work on my virtualized server. Whilst running the 'uber-jar' will start the Tomcat instance and serve the page up, it will only do so whilst the user is logged into the VM. If your VM has any auto-timeout rules set for users then the site will no longer be live once you are kicked. As far as I can tell it is necessary to setup your spring boot application as a windows service so that it persists.
Spring Boot first create a ‘fully executable’ jar with Maven use the following plugin configuration:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
With Gradle
springBoot {
executable = true
}
Go to application directory (Where you have to install web app in system )
Paste Spring boot bundled jar (build using mvn package)
Go to current path via command prompt and just type java -jar ...jarName.jar.(eg. java -jar custstomerService.jar)
Note:
When a fully executable jar is run, it uses the jar’s directory as the working directory.
This is only for spring boot with bundled web server jar deployment not for war
More details

Spring boot application in windows server

I am planning to deploy a web application built on spring boot in windows server.
I want to use tomcat container.
Can I deploy the spring boot fat jar directly or is it recommended to deploy the war file.
please suggest how to deploy and the preferred method?
As Josh Long likes to say "Make Jar not War!" It really allows an application to have flexibility on where it can be run and allows for everything to be packaged as one artifact. Windows has no issue running the embedded Tomcat that is part of Spring Boot and that is exactly what it is doing when running it in your IDE. The one edge case to this is keeping the process running on the server. Normally in Windows you would do that by setting up a service and having that service run java -jar myapp.jar. I haven't personally seen it done so might take some playing around but it is possible.
A simple way to run a spring application in Windows Server is to run it as a service. You can do it using the winsw, that you download its .bin file here
winws download
Then, rename it to something like my-app.exe and create a XML file like this:
<service>
<id>my-app-service</id>
<name>my-app-service</name>
<description>Back end service for app</description>
<env name="HOME" value="YOUR_JAR_FILE_PATH"/>
<executable>java</executable>
<arguments>-Xrs -Xmx256m -jar "YOUR_JAR_FILE_PATH\YOUR_JAR_FILE.jar"</arguments>
<logmode>rotate</logmode>
</service>
Then, using the terminal, run:
my-app.exe install service
Your application is now a windows service and you can start\stop it in the tasks manager on the services tab.
Starting from the latest Windows versions, you could also deploy your Spring Boot app inside a Docker Windows Container. I wrote a complete guide: https://blog.codecentric.de/en/2017/04/ansible-docker-windows-containers-spring-boot/ (as already mentioned, Tomcat is already embedded in Spring Boot).
Spring boot internally has a tomcat server.
If you want to deploy it on tomcat then while building with maven build it as war.
If you want to deploy it has inependent application then build has jar and then place it in some folder and run it using below commands java -jar yourjarname.
Apache tomcat is a web container you cannot deploy a jar in tomcat server. If you created a web application then export your application as war file and put it in tomcat webapp directory, start the server and your war will be deployed.
How to deploy created .jar file in Apache Tomcat server in Eclipse IDE?

Deploy Java application with Rest web services in Openshift server

I´m trying to deploy java application in OpenShift server. My application is divided in four projects: BBDD, Bussiness, Web Services and Web. When I create the application with openshift, it´s created this structure: src(this one has java, resources and webapp folders), webapps and pom.xml. I don´t know how to organize my projects into that structure to upload to the server.
I have put my web structure on webapp folder inside src. Then, I put the other projects on java folder. When I executed the application I can see my web pages and I can navigate for all of them but, when I call to a webservice I have the following error:
Http/1.1 404 not found
Thanks in advance,
Iban
Openshift is expecting to compile the application on their server using the pom.xml then run the application it built. To do that your project needs to be a maven webapplication project. Only when you have tested it locally would you expect that committing the code to your openshift server (using git) would it be able to compile and run the app successfully.
This means that you should not be uploading files using an SCP upload tool; you should be committing your source using git to your openshift server for it to compile then run your app.
The way I typically work with maven and openshift is to add a fragment of xml into the pom.xml to enable the jetty-maven-plugin to be able to use mvn jetty:run to build and launch the project to test it locally. If-and-only-if it works locally do I try to deploy it. That command is 'zero install' as maven downloads the jetty jars and runs them over your project.
Redhat openshift tends to promote redhat jboss AS application server as a Java solution so if you go down that route you should try mvn packageto make the war file and test it against a local jboss install before expecting it to work on the server. There is an approach where rather than committing code for the server to build and run you can build an EAR file locally and have that pushed to the server.
At the bottom of this answer I have a link to a demo I wrote which shows my preferred approach. I create my apps as a DIY cartridge which is an empty shell then customise the scripts in the .openshift folder to start the Java server of my choice. I use maven to build my webapp which I run using the jetty-maven-plugin to debug locally in Eclipse (maven IDE plugin lets me "debug as... > maven > "jetty:run"). Then I configure the pom.xml to build my whole app plus the jetty Java webserver into one huge runnable jar. Then I edit the start script to use "java -jar" to run my full app.
If you are using a DYI cartridge you don't need to use maven; I have used sbt as the build tool to create a runnable jar. You simply have to modify the scripts in the .openshift folder to download and run the tools you choose.
The demo I made GitHub at the link below has instructions on how to deploy it on openshift. So you may want to get that running then after you can both debug it locally and push it to your openshift server then rip out my code and add in all yours:
https://github.com/simbo1905/zkmongomaps

Categories

Resources