Issues deploying a Java application to Tomcat

只愿长相守 提交于 2019-12-13 04:19:04

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/48609468/issues-deploying-a-java-application-to-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!