Guidance Creating a "Basic" Java Web App - java

I have limited exposure in creating a Java web app from scratch, and was hoping that some of you folks could help provide me with some insight.
Basically the flow of the application is as follows:
1.) User logs in (Authentication + session handling)
2.) Requests containing XML are passed to the server
3.) Responses are in XML format also and parsed by the client side
Are there any type of frameworks I can use to help me out with adhering to the "best practices" for what I want to accomplish? I'm comfortable with being able to handle the XML/DB interactions, however I'm not too comfortable with the auth and session handling.
So far I've gotten to the point where I'm using Jetty as a servlet container, and coded basic functions to handle hitting the DB. I'm sort of lost on properly implementing the rest.
I was thinking about using SOAP but it seems like overkill?
Any advice would be greatly appreciated. My apologies if this is a silly question to ask, but after searching for hours.
thanks!
-bp

This is a very broad question, so I'll just give a partial answer. As far as using SOAP, its main practical advantage is for interoperability.
However, it would also be easier as a developer than creating something from scratch: both Axis2 and the Java Web Services Development Pack come with very easy to use code generators (the latter made easier w/ Netbeans or Eclipse). You would basically mark up the code you want to expose as a web service by using annotations (examples abound on the web) and then run it through a code generator and run it. This will produce both the client and the server code. Do some research on Axis2 and Java WSDP.
Implementing security with this solution is trickier. But using headers you can exchange encoded login information.
Using a web framework might actually be overkill if all you need is a webservice as opposed to a human-accessible app through a browser.
EDIT: I should add that the web services solution above, as an added bonus, handles all of the xml for you, although implementing security headers will require some knowledge of SOAP and maybe security standards. It's probably best to make these high-level decisions and ask more specific questions here as you go along.

Related

How to create Web Services in Java (with Eclipse)

I am really really having hard time to create simple "Hello World" Web Service in Java. If you exclude several mobile Android mobile apps I have done, I am pretty new to Java environment.
First I tried Axis2 and that simply doesn't work out of the box. Fresh installation of Eclipse, Tomcat 6.0 and Axis2. Tried sample test app and it failed miserably. You can read about it here. My conclusion is that Axis2 simply doesn't work (maybe it used to work). I will try to go back to install older versions, maybe one will magically start working. I have to modify some old project at work that was using Axis2, so that's why I have to stick with it. I would hate to have to migrate to some other tool.
Then I ran on Oracle article Getting Started with JAX-RPC and I was thinking, cool, let's try that. Well, I just managed to get pissed off. After getting half way through the article and trying to write some simple test web service I realized that their code examples are poorly written. Missing brackets, wrong references, missing explanations, etc...
First of all, an interface should extend not implement. Am I right?
public interface SunRegPort implements java.rmi.Remote {
Also, this doesn't exist:
import java.xml.rpc.server.ServiceLifecycle;
But this does:
import javax.xml.rpc.server.ServiceLifecycle;
Well, I don't feel competent to criticize too much (cause of my level of knowledge about this subject of course), but after all I am suspicious that this article is complete JUNK and therefore I am not able to follow it.
Please somebody tell me that I am wrong and if anybody has some advice or link to some How-To page that talks about web services, I would appreciate.
Thanks.
The terminology Web Services is pretty vague. In Java, the modern and reasonably easy way to do this is by annotating classes.
I would recommend that first, you decide whether you want to implement:
SOAP Web Services -> look at JAX-WS
REST Web Services -> look at JAX-RS
Once you have chosen the "type" of Web Services, choose the library that implements the specification.
The Wikipedia entries list a few of these implementations. Apache CXF (complete but a beast), Jersey (popular) and Restlet are very common choices. I personally like the JBoss implementations as well for JAX-WS.
For JAX-WS with Apache CXF, here is a quick start tutorial that looks pretty good (untested)
For JAX-RS with Jersey, try this
Unless you have strong reasons to do otherwise (for instance you need to call an existing system), use REST which is simpler and leaner.
I would recommend you to check out the CXF project by apache. It's quite easy to use and should help you to set up a webservice.
There is a nice guide to Hello world set up.
http://cxf.apache.org/docs/a-simple-jax-ws-service.html
If you're still interested in giving Tomcat a shot, here is a tutorial on youtube of making a very basic "Hello world" example using Eclipse. It leaves a lot to learn; but it looks like it will at least get you started.
FYI, the version of Eclipse used is slightly outdated. So some of the menu options may not be exactly the same:
http://www.youtube.com/watch?v=EOkN5IPoJVs
Also, the meaning of "Web Service" can be subjective. So, to be specific; this just shows you how to get a basic HTTP endpoint started on your local machine.
If you are not tied to Eclipse, give NetBeans a shot. I think it's much better for Web Services in Java.
Web Services are most widely implemented examples of Service Oriented Architecture (SOA). A service contract is defined with the help of SOAP and Web Service Definition Language (WSDL) and that is published for other applications to use.
In this article we will see how we can create web service and web service client in java.
http://www.opencodez.com/java/how-to-build-and-deploy-web-service-and-client-in-java.htm

Java MVC and Rest Server and JavaScript Heavy Client

I am in the process of beginning development of a web-based user interface and am looking for some expert advice on the right direction for my project.
Since our server-side is to be developed using Java, I am planning to use scriptless-JSP and an MVC framework called FrontMan. FrontMan is a lightweight Java-based MVC framework similar to Struts. I have played around with it and like it. The plan is to use scriptless-JSP assisted by JSTL and JSP tag files. I worked out some prototypes using these technologies and I liked what I had.
Now I am in the process of planning out the client-side stuff. My plan is to use JavaScript, jQuery, jQuery UI to build the front end. During my investigation I came across a very nifty JavaScript MVC framework named JavaScriptMVC. I have to say I really like it. It is not just a client-side MVC framework, but also includes tools for testing, documenting and deploying.
The more I engage in figuring out the client-side plan, I realize that I think this is going to be more of a JavaScript (client-side) app. I am leaning towards doing as much of the work in JavaScript rather than on the backend via Java. The backend will exist mostly for communicating with the database and responding to requests from the client (frontend) for manipulating or retrieving data.
I am rather new to RESTful web services, but in the little I have read about them, I think this might be a good direction to take this project.
I am wondering how I can employ both an MVC framework and REST framework together? Do they make sense together?
I am using Jetty as my web server, so I think that lends itself to using Restlets...?
Anyway, I am not sure how much sense I am making in this post, so I will stop it there for now.
As the previous poster mentioned, it makes perfect sence. MVC let's you separate concerns such that you can keep your application logic separate from your presentational logic.
Your REST controller serves the request you need, populates the model, and exposes it in a REST like manner, typically by returning an xml view to the user. This can be aided by a number of libraries such as xstream, jaxb, castor, xmlbeans, which all provides routines to marshall an object into xml.
Exposing a REST API on the server-side will be a perfect fit for your JS front-end. Your resources will have to expose JSON representations. You will even be able to reuse this web API from other clients such as mobile phones.
As the lead for the Restlet Framework, I can only recommend it for this purpose. Integration with Jetty is very stable :)
Cheers,
Jerome
A quite extensive discussion on this can also be found here:
Separate REST JSON API server and client?
For me it was very helpful.
Yes! It makes sense as Johan Sjöberg said! You can use Spring MVC for example. I like it a lot and think it is very simple and easy to learn. You have an example here.
By the away, for the frontend maybe you would like to check this presentation.

Which Java web development framework for an AJAX application?

I read through a few threads (simple web framework, java web development, etc). Very informative. However I have not seen a focus on the AJAX side of things. For the app I am trying to create, most of the client side will be written in Google GWT, and JSON will be used to communicate with the server side. In that case, all templating is pretty much useless.
For my purposes, which framework would be the simplest to setup and easiest to learn?
Thanks.
To clarify, I want a server side framework. GWT is great for client side, but I need something to generate json responses on the server side.
None at all, most likely: GWT is framework enough, given that's what you're using. The only reason I can think of to add anything else might be if there was some special effect you absolutely craved (but if you've chosen to go GWT, my recommendation would be to give up on such special effects),
Ajax/JS by nature functions mostly on client side and you want to use it but want to run it on server side? Are you sure you're not trying to hammer a square block into a round hole?
Reading through your question it seems to me what you're really asking is for a way to abstract a layer which provides JSON for your client side UI. Most web frameworks such as GWT deliberately abstract this part away but still include it in their inner workings: it would indeed be rather stupid for an Ajax web framework not to support data transfer between the view layer and the rest of the system!
However, if you really want to create your own custom component for serving JSON, then I suggest you take a good look at Servlets and mix that with any of the gazillion available JSON libraries listed at JSON.org.
And if you still really, really want to run your client side view logic on server, Vaadin could do the trick for you. In practice Vaadin really runs GWT on the server side and just serves static stuff generated by the server side GWT but from what I've understood the difference has been abstracted away.
A huge word of warning though, for me it sounds like you really want to reinvent the wheel here while you shouldn't. You really should reconsider your architecture and/or deepen your knowledge of GWT and web frameworks in general, the "framework" part usually hints that it's not just the V from MVC Model 2 but at least V and with M bindings.
I've had pretty good luck with the Dojo Toolkit. Make sure to download the full toolkit (Dojo, Dijit and Dojox) which you'll find at the downloads page.
Their 'Hello World' tutorial is pretty useful for getting started.
You could could give IceFaces a try. It's a Ajax framework based on faces technology. Works really great and magically, also comes with a good documentation and tutorial.
You should look at one of the REST based frameworks, like Jersey, Restlets, or RESTEasy.
The main reason is that these frameworks make binding and working with JSON easy. Most of the other systems are designed for HTTP POST encoded data, which is not JSON.

Create a webservice that keeps data secure?

I want to create a webservice that allows users to enter the longitude and latitude for a particular building. Buildings will belong to a broader category called a Region. If a specific building does not exist in a region the user could add the longitude and latitude data through the webservice. Similarly if a user finds data to be inaccurate, they could update it.
This webservice would not need to have a web front-end only a RESTful API that would only be accessible to my application(s). I want the webservice to be able to accept new and updated data (as described above) and also print out relevant data (in xml format) based on the appropriate API calls.
I need to ensure that this data would be secure - i.e. nobody would be able to gain access to the data i have accumulated.
What is the best way to do this? I have familiar with client side Java and not much server side technology (not familiar with server-side Java, rails, django, etc...). What language/frameworks should i use that would accomplish my needs with the shallowest learning curve?
You can just use basic authentication (username/pw) with ssl enabled. That way you are encrypting the connection and providing a broadly supported means of authentication. I believe apache Axis will take care of most of it for you.
I've used the Restlet framework to deploy web services that are password protected. It supports basic authentification and several others out of the box. You can also set up your services behind an https "server connector".
Another approach is to run your application in a Java EE application server which supports JSR 196 (eg, Glassfish or JBoss). You would then use the server's facilities to establish the authentication.
Here is the Glassfish security page.
If you have never programmed server-side code you will be facing a pretty steep learning curve, I'm afraid. If you are comfortable with Java then the Restlet framework mentioned by another commenter is a good choice. It is easy to use, includes both client and server tools, and has pretty decent documentation. The video screencasts are very good.
Another option is Ruby on Rails. I am currently implementing something very similar to what you are planning and Rails has worked extremely well. Rails has built-in support for XML output through both the ActiveRecord class and XML Builder templates. I used Atom Authentication (http://www.xml.com/pub/a/2003/12/17/dive.html) between the client and server and it is working beautifully. There is a learning curve for both Ruby and Rails but the power of the framework makes it worth it. I am partial to the Ruby and Rails books at The Pragmatic Programmer but O'Reilly has some good ones, too.

Expose a web service (wsdl) as a webform

Just wanted to ask if anyone knows of a reliable & easy way to expose a webservice (wsdl) as a webform to end-users.
Our team develops a lot of software for external contacts, which often involve creating a web service and exposing it as both a wsdl and a website. We'd like to automate this last step as much as possible (especially for the mindless data-pumping projects) so we can concentrate on developing web services instead of websites.
I recently came across Enunciate, but have not found the time yet to play with it in detail. I was wondering if anyone has any experience with it (or with any similar projects) what your findings are, and what direction you would advice us to take with this.
Best regards,
Tim
Update2:
Turns out what I need is perfectly described in this Enunciate module: http://jira.codehaus.org/browse/ENUNCIATE-23. The only problem is that it's not implemented... (yet?)
Anyone have any experience in creating your own modules, or knows of an existing solution that provides the same functionality?
I'm not sure how much weight my response carries (since I'm the lead developer of Enunciate) but I'd like to encourage you to try it out. I know a lot of happy Enunciate users.
Even if you decide not to use it, I'd like to hear about your experience and why you decided against it.
I've found that using the Metro libraries to provide the web service engine, a web page is produced when accessing the end point with a GET which allows you to easily get the WSDL if you need it.
I do not know if that is what you mean with a "web site" but it is a nice default way to work.

Categories