• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java backend, JavaScript front end project  RSS feed

 
sean cronin
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I've been tasked with creating a standalone JS application, that will be using Java in the back-end. I need to setup a server and DB (MySQL) and then need to retrieve and serve the database data to the JS client.

I've been doing a tonne of research, but it's hard to get a solid grounding on what to use, how to implement due to a number of options available. Can anyone direct me on what to look for, to build this sort of
application. I've never used Java with JavaScript so is all new.

Stress levels are hitting the roof at the moment so any help would be great.

I've been having issues replying to posts from work, so bare with me if i'm slow to respond.

Thanks,

Sean.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sean,

If the application is purely JavaScript on the client-side, you can create a REST API and communicate with it from from the JS application. If the application is more of a full blown web-application, you might want to find an MVC framework that allows you to return web pages from Servlets, using JSP views.

All of this can be done with a framework such as Spring MVC, but for simple REST APIs I've always had good experiences with Jersey.
 
Tim Moores
Saloon Keeper
Posts: 3512
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to start by telling us what a "standalone JS application" is. "Standalone" would normally mean a native app, not a web app, so Javascript would not be involved. (Unless the native app had a rendering engine built in, in which case it would actually be more of a web app.)
 
sean cronin
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I just need to build a little component that is just a simple input box. The user inputs an code, which is passed to the Java server, data is retrieved from the DB and passed back to the JS as JSON object.

They want to be able to take the code I write, dump it into another program and have it work away fine.

I'm thinking of using Jetty for the server, Ajax or jQuery then to create the JSON object. Maven seems to be good to use as well for this type of project but don't think I can use that.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should then set up a simple REST API on the server end. Using a JAX-RS implementation (such as Jersey) you can annotate your controller with @Path and then the action with @GET. You can also host a static HTML file containing the JavaScript that will communicate with this API.

You could use Maven to add a dependency on the JAX-RS implementation, but for a small project this might not be necessary if you just follow the installation steps of the implementation.
 
sean cronin
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I've been hammering this the last couple days and have some progress. I'm having trouble with my URL's though. My ajax URL's and Jersey just don't seem to be working. It's like the URL
is being by-passed.

Any ideas of where I'm going wrong with the URL's? This is being built using Eclipse Dynamic Web Project with jetty servlet. I had localhost:8080 before url in JS but took it when looking
stuff up on stackoverflow. So I have simple input form in html, which I'm trying to pass the data from the ajax to jersey, and respond to the same page that made the request. Should I be doing something
more in the servlet doGet method? I'm really confused about what I should be doing with it.








Folder structure:

 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sean.

Not a jersey expert by any means, but I think your web.xml is the culprit here.

You referenced this in your web.xml:


What is this SearchComponentServlet?
You mentioned a "Servlet doGet" method, but haven't included source code for it.

You HAVE included what looks like jersey rest service class (ErrorResource)

What I would expect to see in your web.xml file with that there is
- a reference to the standard Jersey Servlet
- configuratino for that servlet referring to which classes Jersey should look at to define your rest service.

Something like:
(taken from http://www.vogella.com/tutorials/REST/article.html)


you should then be able to access /rest/errors/search?errorCode=ABC and it should give you a response.

At least I think so....
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Ajax call url is missing the error code entirely, so I'm not sure it'll find the endpoint, which expects the code in the parameters.
You might find this out if you added a handler for an error result...possibly.
 
sean cronin
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The xml I didn't really do much with. When I created the project in eclipse it had put in those lines so just left them. Here's the servlet code.



Yes the ErrorResouce is my jersey REST.



Updated XML.





[img=http://s30.postimg.org/6o49hywu5/Capture.jpg]

I've been using this tutorial as a guide. I've made adjustments from what you've said but still not getting any results. Are these any better?

http://www.dreamjava.com/content/jaxrs-with-jquery-and-ajax.html
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You should be able to print that URL to the console.
It won't be what you think it is.


Since this is a GET you should be able to copy and paste it into a browser tab and have it work.
If you can't then it means you have the URL wrong.
 
sean cronin
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I went back to the drawing board with this and followed the tutorials on the vogella website to get a better understanding of how all this works. One of my main
issues was I didn't have the jersey jars in the lib folder which was essential and you were right, my URL was messed up.

I'm now getting communication from ajax to my REST resource, but am getting this error now when trying to convert the objects to JSON. It says I'm missing jackson files
which I've look up, but having trouble trying to pick the right ones, as most tutorials are from years ago using different jars.

Error:
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in the manual: https://jersey.java.net/documentation/latest/media.html#json.jackson
 
sean cronin
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million for the help lads, I finally got this going. Had to dig out all the dependent files, and fix some bloody spelling errors in my JS and it worked a treat. Must post my solution up somewhere
over the weekend as may help people in similar situation.

Thanks Again!
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got it to work!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!