permaculture playing cards*
The moose likes Servlets and the fly likes need direction on creating java app from existing html/jsp site Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "need direction on creating java app from existing html/jsp site" Watch "need direction on creating java app from existing html/jsp site" New topic
Author

need direction on creating java app from existing html/jsp site

Jerry Renn
Greenhorn

Joined: Nov 05, 2013
Posts: 7
Since my company couldn't hire anyone else that has experience in J2EE, I was tasked with the job of creating a simulator for a current "app" we have on an embedded device. This config app is pure html/Angular JS. All submits on the pages POST/GET (with the possible PUT/DELETE) to /api (some to /api/x/y, etc).

So overall it's fairly simple right now - on the board, it's only ever single use - they login and then can make updates to the board configuration; but I have to create a simulator that will allow upwards of 150 (or more) customer service reps to simulate the steps they're telling the customers to do - so I'll have the app start with a default configuration every time they log in and track the changes - probably in session as they don't want me using a DB.

As I have to set aside what I'm doing today as this is now the next hot thing, I wanted to just verify a few assumptions before I go to far and then realize that I should have taken a different route - my thought is just using something like Servlets 2.5 as that shouldn't be a problem handling the POST's/etc and managing the session. I was thinking of using Spring, not MVC or webflow, but it would probably be overkill just to use a few features...

I've started several projects from scratch that were much more complicated than this, but I struggle a little with this as I can't make changes to the html/JS that is already there - I just have to provide the interface (my question) and the back-end (this is mostly done as my other current project needed a simulator before the board became available to all of the devs).

Is Servlet 2.5 sufficient? Anything else I should consider?

thanks
-Jerry
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

If you already have the HTML and Angular front end, you already have the interface. I'd say from your description that you just need to create a faux backend that exposes the same API as the "real" backend, and "point" the front-end to access your new API.

Servlets should be fine, you just need to do a lot of mapping to route the URLs to the correct servlets.

It'd likely be easier to use a framework such as SpringMVC or Play! than using raw servlets. But your call.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jerry Renn
Greenhorn

Joined: Nov 05, 2013
Posts: 7
Bear Bibeault wrote:If you already have the HTML and Angular front end, you already have the interface. I'd say from your description that you just need to create a faux backend that exposes the same API as the "real" backend, and "point" the front-end to access your new API.

Servlets should be fine, you just need to do a lot of mapping to route the URLs to the correct servlets.

It'd likely be easier to use a framework such as SpringMVC or Play! than using raw servlets. But your call.


This type of response is just what I was looking for. The final summation was a bit poor - I do already have the user-interface (the HTML/JS) and I have most of the back-end simulator (written in pure-java), I just need the plumbing - the code that maps the requests that the Angular calls will make to the simulator (pure-java).

So I have one quick question as it's been a year or so since I had to do the mapping - for the Servlets, wouldn't I just place the servlet that was needed in the war's directory path? for example, if the Angular POST's to /api/wireless/a/b,I would put the servlet for this in /api/wireless/a/b?

thanks
-Jerry
Marshall Blythe
Ranch Hand

Joined: Feb 26, 2013
Posts: 32
Jerry Renn wrote:
So I have one quick question as it's been a year or so since I had to do the mapping - for the Servlets, wouldn't I just place the servlet that was needed in the war's directory path? for example, if the Angular POST's to /api/wireless/a/b,I would put the servlet for this in /api/wireless/a/b?


Servlet mappings have nothing to do with the physical location of the servlet class within the WAR. They are logical mappings declared in the web.xml deployment descriptor. For example, let's say you wrote a FooServlet and you want it mapped to the URL /foo. You would declare the servlet and it's mappings in your web.xml file:



Note that this URL is context-relative, so if your web application's context root is myApp then the fully qualified URL used by clients would be in the form of http://myDomain/myApp/foo. Multiple mappings can be declared for a single servlet, so if you wanted FooServlet to handle requests for /foo and /bar you could declare the mappings like this:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Yes, the physical location of the servlet is not its mapping.

You can map servlets directly in the deployment descriptor as Marshall describes. But in order to simplify the mappings, many people use a Front Controller (see this article), or framework.

The Play framework, for example, provides a "routes" file that maps URIs and HTTP methods to specific methods in controllers.

Also note that with SpringMVC or a Servlets 3 container, some amount of config can be done with annotations rather than in the deployment descriptor.

So many choices --- isn't engineering fun!
Jerry Renn
Greenhorn

Joined: Nov 05, 2013
Posts: 7
Thanks guys, I was hacking around on it last night and have something rudimentary working. I was gonna get good foundation laid today.

In the end, I went with Spring 3.0 because I could use annotations to map the URL's to the servlets as the current HTML/JS interface POST's to approximately 45 different URL's - I figured it would be easier to do this programatically versus XML.

I wanted to go just with servlet 3.0 but I have to stay with tomcat 6 and most of my experience with J2EE has been with struts and Spring and as this is another fire that has to be put out, I decided against what I wanted to do - take a little time and experiment!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Very cool. Thanks for posting back with your approach.
Jerry Renn
Greenhorn

Joined: Nov 05, 2013
Posts: 7
So the (embedded software) architect didn't want me using Spring or any "complicated to learn" frameworks, so I talked him in to letting me use tomcat 7 and servlet 3.0.

Anyways, I've created a few servlets to simulate the backend and using annotations, it hits the servlets just fine, but my question is using the static content I already have. My web.xml has only one real entry:



but when I hit the tomcat server: localhost:8080/myApp it gives a 404 in logs:

"GET /myApp/ HTTP/1.1" 404 963

index.html is located here in the myApp.war (which is deployed to tomcat7/webapps):

myApp.war/WEB-INF/index.html


How I'm looking to handle things is the static pages will work the same as they do, so they html/js will decide where to go as they already contain all of that logic and when the angular JS POST's/GET's it will be handled by the servlet's
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Anything inside WEB-INF is hidden and cannot be accessed via URL.

I'd move the HTML file to the context root.
Jerry Renn
Greenhorn

Joined: Nov 05, 2013
Posts: 7
Thanks - I had finally found the documentation that I couldn't find the hour searching prior to posting that said the same things: https://developers.google.com/appengine/docs/java/gettingstarted/staticfiles

I was gonna delete the previous post so as not to look too amateurish and then you graciously responded!! ;-)

Edit: it is coming up now!
Jerry Renn
Greenhorn

Joined: Nov 05, 2013
Posts: 7
Getting close - So if I navigate directly to the Servlet it works, but when I use the static web pages, I'm running in to a context issue.

going directly to the servlet, I see the following in the logs:

"POST myApp/api/login HTTP/1.1" 500 409

The 500 is expected as I'm not actually posting any of the expected data

When I do it through the HTML/AngularJS app, here is what I see in the logs:

"POST /api/login HTTP/1.1"

So it's not posting to the context. I can't change the JS files to add in the context and I can't deploy it to ROOT as there's already something there.

thanks for looking!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Without adding the context path to the URLs, you will not be able to address the resources in the web app.
Jerry Renn
Greenhorn

Joined: Nov 05, 2013
Posts: 7
I was afraid someone was gonna say that as I couldn't find anything in the documentation to address this...could this be handled by apache - anything to /api/logout gets redirected - something like a proxyPass?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need direction on creating java app from existing html/jsp site
 
Similar Threads
Chapter 6(Session Management) notes (HFSJ) for revision
Design choice for dispatching request to view
creation of dynamic chessboard with user interactivity using javascript.
The death of the heavyweight business object?
Spring jQuery form submit request.getSession(false) returns session but with a new session id