Issues deploying a Java application to Tomcat - java

I built a simple Java web application. It provides a series of RESTful APIs for the user to carry out certain operations on a Java DB through a web interface. I used NetBeans environment during the development, and Glassfish for testing.
Now that I finished it, I would like to be able to deploy it on another machine using binaries (although as for now I use the same machine until I learn how to do it).
I installed Tomcat 7, and moved the .war file into Tomcat's webapp folder. The application deploys. Thereafter I try to read some data from the databse using a button I created just for this, but get the following error
I am not sure what went wrong, but I have two theories.
1) The web application cannot connect to the database. Yet when I attempted to run the application again, after starting JavaDB from NetBeans, there was no difference.
2) Somehow, the application cannot reach the Node service. I assumed that there will be no need to change the API links while moving the app, but perhaps I was wrong.
Or maybe there is some other issue I did not consider? I will be grateful for any advice about how to properly deploy such an application.
EDIT: The issue was solved by using TomEE.

The error is come from your application server of choice.
TomCat is only a servlet container (means it only support Servlet/JSP).
Any other feature (JAX-RS, CDI etc) require a Java EE certified server e.g. GlassFish, WildFly,Payara, WebLogic, OpenLiberty or TomEE.
TomEE could be your best bet if you want to use TomCat in your production or test environment, it is basically TomCat + Java EE other feature.
EDIT:
TomEE don't have a GUI for JNDI datasource configuration like GlassFish, you need to edit conf/tomee.xml
<Resource id="myDataSource" type="javax.sql.DataSource">
jdbcDriver = org.apache.derby.jdbc.ClientDriver
jdbcUrl = jdbc:derby://localhost:1527/dbname
userName = app
password = app
</Resource>
And in your java code:
#Path("resources")
#Stateless
public class MyResources{
#Resource(name="myDataSource")
DataSource dataSource;
#GET
public Response SomeMethod(){
//Do stuff here
}
}
You can check here for more detail configuration on data source.

Related

Is it possible to make a Spring MVC web app run as a "standalone executable" with Java and Tomcat embedded?

I have a web app built with Java, Spring MVC, and JDBC. The result is a WAR file.
To run it, the user has to install Java 8 JDK and Tomcat, and deploy the WAR file to the Tomcat server.
It would be great if they could just download the one file run it as a standalone application.
That is, run "the WAR file" and just browse to http://localhost:8080/myapp
Also, on Windows it would be great it was setup as a Server (like Tomcat is when installed with the installer).
Is there any way to do this? Maybe with Spring Boot or something new like that?
Yep, Spring boot is the way to go.
It allows you to build an executable Jar with all dependencies and a Tomcat (by default, can be changed) embedded.
But users will still need to download a JRE to execute the Jar, and a database if it's required, but you can use en embedded database like H2, HSQLDB..., depends what is your needs.
Yes . you can use spring boot to achieve your results. Kindly refer the below link for sample code
https://mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/
You can use embedded jetty server using maven but that would require you to setup few things your app and may have align your existing app, please check this article for more information.
Jetty is similar to tomcat server in terms of running spring application, there are not much difference in terms of development. Tomcat is just more famous.
Other option as others said, is to migrate your app to spring boot which would be easy if you already have app written in spring (But that depends how much code you have and how much time you have)

How to create a Payara user account in an Arquillian functional test?

I'm using Arquillian to deploy a Java EE WAR on Payara embedded for functional testing with Graphene. Everything works fine.
However, I'd like to know whether it's possible to test user authentication for #RolesAllowed protected RESTful methods. Afaik there's no way around creating users on the Java EE server and in GlassFish and Payara that's done in the web admin UI:
I didn't find anything about a Payara RESTful or similar API for Payara admin tasks.
There seems to be nothing created on the filesystem by the drone driver so that there's no access to the asadmin command.
According to sudo netstat -tupln | grep java the Payara embedded process only opens port 8181 (HTTP) and 8182 (can't be opened in browser).
My question is similar to How to test login with Arquillian - Java EE 7 whose answer doesn't work because CommandRunner isn't injected and a comment says the interface has changed - whatever that means.
The approach in Embedded Glassfish, security and Arquillian questions doesn't work because it's unclear which Server class is used and none of the available ones match all method names (I tried all that pop up in NetBeans autocompletion).
I'm aware of the possbility to replace the embedded with a remote instance, but that's not part of the question.
I'm using Payara 4.1.2.174.
The preferred way to configure the embedded GlassFish/Payara is to configure a standalone Payara Server instance interactivey first (using Admin console or asadmin commands), copy the configuration from the domain (domain1) into test resources and then point the Arquillian container to the configuration.
The documentation of the Arquillian GlassFish embedded container describes that you can use the property instanceRoot to point to the whole domain directory or configurationXml to just point to the domain.xml in that directory.
In fact, the answer in your last link (https://stackoverflow.com/a/24967309/784594) explains precisely this scenarios and provides a ste-by-step guide. You shouldn't be using any Server class to configure the server, you can skip that step - if you secure your REST endpoint using basic HTTP authentication, you just need to send user and password in request headers.

Java Web Container implementation independant development

One of the most important non-functional requirements of any project is the build process in my opinion and that's where I kinda get stuck in my java project, which has just one external dependency, a jdbc compatible database. All the tutorials about spring and deployment out there; I've read them all, but they either say:
run it with gradle bootRun applications.properties (yes works but on a webserver I'm not going to have any properties-files, but JNDI resource for example) or
build a deployable war file with JNDI resources (yes it works on the webserver, but not in my embedded webserver or I'm doing it wrong, but I cannot find any doc about how an embedded tomcat loads a context.xml from outside the jar file).
Now I tried to use the same setup as my server and installed tomcat7 and the #Asyc #Scheduled services run, but no servlets, like a simple /status page should return "OK" just for checking. catalina.out does not show any errors. My /manager from tomcat7-admin says deployment ok, and when I click start: "FAIL - Application at context path /xyz-0.1.0 could not be started
FAIL - Encountered exception org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/xyz-0.1.0]]"
And now I want to make a cut here and throw every "bootstrap" stuff away and start from zero.
So my question is, am I wrong when I say, that the big deal of my deployment is a jdbc JNDI resource provided from the web container, which is called 'jdbc/xyz' and everyone who wants to deploy my war needs that JNDI. That means so matter if you're using wildfly, jboss, tomcat, glassfish or any embedded server: your server impl has to provide that web container and jndi resource to make it run. And I don't want to configure any tomcat, glassfish or whatever implementation in my war.
Is that wrong?
It would be great, if you could help me. I'm playing around with that many weeks now :( and if A works B does not and vice versa. Would it be easier to use JEE?
Thank you.

Difference between an Enterprise application project and a Web project application in Netbeans IDE

Please I would like to know the major difference between a Jave EE project and a Java Web project in netbeans IDE with respect to the EJB. In fact you can create a web application based on EJB, JPA and JavaServer Faces in Netbeans IDE if you choose the Java Web category.
On the other hand you choose the project category as Jave EE project, NetBeans IDE will create 3 sub-projects, e.g: StoreApp (Enterprise Application project), StoreApp-ejb (EJB project), and StoreApp-war (Web project).
The first one will be packaged as a single .war file, the second one will be packaged as an .ear file, containing the web .war and the EJB .jar.
The difference between these is a bit broad to handle here, but I wouldn't advise on creating EARs unless you know that you want/need to.
I think you should learn firstly about web server and a application server..
well i'll happy to help you.
1) we cannot run enterprise application in web server.. i.e. if u are using EJB etc. to run this EJB project you have to use Application Server like JBoss and many more.
2) while using web server it cannot load any heavy application means if your application having multiple users , in that case connection pooling will come into the picture. for connection pooling Application server much better then web server.
For more you can visit below link:-
What is the difference between application server and web server?
I hope it'll helps you
Thanks!!!
There are 3 kinds of project/application:
Standalone Java application and sometimes call it as swing application. Bundle it in the form of Jar and you can run this application without any web or application server.
Web-application: Basically we have to create a war which is combined with html/jsp,jar etc. This war file has to deployed into webcontainer.
Enterprise Application: We have to create a EAR file which is combined with war, EJB & jar. This has to deployed into application server.
A very helpful tutorial https://www.youtube.com/watch?v=6vMB3rzN_Xw
Hope this helpes... cheers!!!

Spring Security not working under Windows 7

I just installed Windows 7 with NetBeans 6.5.1 and JDK 6u16. I've checked out the Web application project with SVN, which is working with Spring security. Libraries are imported, no reference problems, the same configuration worked with XP.
Here's the beginning of the exception:
WebModule[/db3]PWC1275: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
Full version Here.
Please if you have any suggestions, write below!
Thanks in advance!
It looks like the issue is that Spring can't find a DataSource resource named "db3" that is supposed to be defined in JNDI. Are the necessary JNDI resources configured in the application server you're using?
If you just installed your operating system, chances are you also just installed your application server. Your web application depends on a database definition in the application server (JNDI). I may be wrong, but it looks like you are using GlassFish as the application server, if that is the case, you can setup a JNDI datasource from the admin console.

Categories

Resources