I develop Java applications with Eclipse that process data. So far I only developed stand alone applications that take data from file or database, process the data, and output the results to the console/file/database.
I would like put my application online. I never did any web development, but from my understanding, the only difference is that my code needs to be on a web server that can get http requests from users, and return http response based on my application's result.
I would like to get advice on the easiest way I can do this. What technology do I need to learn and what tools I can use for making the transition easier. I would also like to separate my code from the code related to the web stuff.
Thanks a lot!
The simplest approach to developing Java web applications is via the Servlet specification. This lets you load your application into a Servlet container (such as Jetty or Tomcat), which handles the HTTP-side invocation issue. Your servlet is then a front-end for your front end agnostic processing application.
Since all applications require a user interface, take a look at the myraid of possible templating languages available. Velocity is always a safe pick. This will help you seperate the user interface from the adapter code.
While you investigate alternatives, Java Web Start may be the fastest way to get your existing application online. Here's a trivial example.
The easiest is most likely to learn how to write a simple servlet, how the HTML looks that you want to generate and then adapt your existing application to run inside the servlet code, and print html instead of just plain text.
This is described in the Servlet part of the Java EE tutorial (which is not great, but a start)
http://java.sun.com/javaee/5/docs/tutorial/doc/bnafe.html
If you want a more accessible book, I can recommend the Head First series. http://oreilly.com/catalog/9780596005405
For a web container, Apache Tomcat is fine. http://tomcat.apache.org/
Related
I am starting my career in programming and I have being trying to learn as much as I can, specially in "new" technologies as Angular 2+, although I like a lot more working on the back-end.
I am currently working in a company that has 4 main applications all running using Java EE 7 in a JBoss application server. The applications have a user interface with is currently based in Java Server Pages (JSP) and Java Server Faces (JSF) (mainly using PrimeFaces framework). My project is related to the migration of the User Interface from those "horrible" xhtml JSF pages to something like Angular 6+.
Needless to say that this this transition should happen with minimum changes to the back end. However, in the way the Back end was originally conceived, the back end is not RESTful (no REST API). So, it is all based on Beans, which makes the back end to be closely tight to the front end, making scalebility an issue.
All that being said, considering that creating such RESTful service, although not completely off the table, is not an option now, I have the following questions:
1) Is it possible to 'feed' the data to my Angular UI using a JSP !? I did some research on the topic, and it seems it is somehow possible.
2) Considering it is possible to use the JSP pages, how much of Angular "great tools and functionality" I would loose in this process (routing, two-way-binding, service injection, etc)?
It would be great to have some unbiased opinion, since I know where the people at my company stand on that issue (they would prefer to not spending the time creating the REST API at this time) and I know where I stand (I would rather create the service, since it would be a great learning oportunity for me). Still, facing the challenges of a real world application, I would like to know the community opinion.
Thank you for the help,
biased opinion, even though you asked for a non biased one
I'm not an authority on JSP, but from what I know, these are server side templates rendered on the backend. If this is the case then you cannot use this with angular. Maybe you can make it work somehow by extending the webpack config and custom plugins which load the .jsp files, but you will definitely lose either the AOT performance from angular or the dynamic nature from JSP. And besides that, angular is moving to all AOT anyways.
My suggestion is the same as yours, write a RESTful service. It shouldn't take too long, because the requests/responses are already available. Just make sure to follow to correct REST guidelines.
Then create the angular app based on the templates from jsp. I suppose you can copy these one on one, and change the syntax from jsp to angular, and obtain the necessary data from the REST api and populate these in your template
I tried to understand this several times allready, but still have not found out how this should be done.
I want to create a web interface for some existing console or swing application. Something like the web interface SABNzb offers (I know, it's Python – it's just an example).
I have looked at several technologies allready, like creating web services using a tomcat server, or java server pages/faces, but all the tutorials that I found so far start with "Create a new Web Project..." at wich point I stop because this is not what I want! I have a finished an application in which I want to integrate a web interface, not some web service that instantiates my program as a local variable and uses its code. So basicaly it feels like all the tutorials I find are the wrong way around.
The core procedure of this is clear, the application should listen for http requests on a port I choose and answer with a created html code to it. So basicaly I could open a port using a socket and write an html page to its output on connect. But this rather feels like inventing the wheel all over again, also I'm not sure how an interactive web page would work this way.
Maybe I am thinking somewhat strange here or did not understand how some of these things work, but I am pretty unexperienced with web technologies, so grasping the concept is rather hard at the moment. Can anyone point me to a tutorial that shows how this might be done, or some other source of information on it?
You don't need JSP or JSF; all you need is a servlet. It's an HTTP listener class. You can do REST with that.
The moment you say that you have to deploy your servlet in a WAR on a servlet/JSP engine. Tomcat is a good choice.
Google for a servlet tutorial and you'll be on your way.
My First Tomcat Servlet
Ok, thanks to duffymos answer and comments i realized i was actualy searching with the wrong keywords.
Embedded web server is the thing i was looking for.
Like Simple or build in HTTPServer class in java.
I am used to develop web applications in Java (Struts, Spring, JSP...). But now I want to develop a desktop one. I never liked to design windows in Java (AWT, Swing, SWT): too much work for an ugly interface. So I think it could be a good idea if could take advantage of my web-app skills. One option is to modify the SWT Browser and make calls to a Java function instead of HTTP requests. A very good add-on would be use of JSP. Finally, I thought that probably there is some framework or tool for this.
Do you think that what I propose is a good idea?
There is available some framework for this?
I need this for light applications. So I think that embedding an Tomcat server and using it with HTTP requests is not a good idea.
Edit: One example application could be a folder comparer: you specify two folders and the app shows you which folders and files are different. In this case, I think opening an external browser is ugly. Bloated application (with its server, MVC, etc) wouldn't be the best choice.
If you have used the JavaScript library - ExtJs - then you can use it with Adobe AIR to build good looking desktop based web app.
Building app's in Adobe AIR is also simple and elegant with the flex builder ide.
If your option goes to embed a light server, check winstone is not fully J2EE compliant but should be enough for what you need.
About the browser, I am not a big fan of swt myself, it complicates a lot cross-platform deployment, so probably worth to keep an eye on jwebpane, not quite ready yet, but probably the solution you'll need.
I wouldn't discount embedding a web server. I've done this before with a web start application embedding Jetty.
The download was pretty fast, the server starts up and you can use BrowserLauncher to immediately drive your browser to the embedded server, and hence your application. Jetty is designed to be modular and have a small footprint, so you can probably cut it down to the bare necessities.
There are several options: You can use the plugin API of Firefox and develop your app in there. You can use HTML, JavaScript, the built-in database, all the browser features and access the OS level.
Or you could try PyQt (Python and Qt) which allows to write simple applications very quickly.
[EDIT] The main problem you're facing is security: For security reasons, JavaScript apps (running in a HTML page) can't access local OS resources. So unless your browser allows you to write plugins in JavaScript (which is only true for FF AFAIK), there is no way to write an application which uses HTML as the "view" without the help of something else.
Moreover, HTML is very limited when it comes to features for applications. HTML is designed to be a "static document view" not an "application". You can do things like GMail but if you compare GMail to any real mail app (Outlook, Thunderbird, Notes), you'll see quickly that real desktop apps offer a lot more features.
I want to write an application that runs entirely locally on one machine - there is no need for connection to the internet or to any external machines.
I was thinking that it would be a good idea to use a web browser as the platform for this application so that I would not have to mess around with lots of UI stuff - I could just knock together the web pages fairly quickly and take advantage of CSS to get consistent styles throughout the application.
However I want to interact with a MYSQL database on the machine in question. With this in mind I was thinking that I could somehow use Java to process the information that the user inputs from the application and communicate it to the database via JDBC.
I know that I could use an applet to do this but the downside to that is that I would like the user to be able to save files to the local machine - and I have read that applets run in a sandbox which prevents them from gaining any access to the local machine.
I also know that I could use PHP but I would like to take advantage of object oriented design which Java is perfect for.
Does anyone have any thoughts, suggestions or links to tutorials/webpages which could help me to decide how best to go about this.
Any thoughts are very much appreciated..
I know you said you don't want to mess around with GUI stuff in java, but have you looked in to java web start? It does almost exactly what you need; a user clicks a link through a web browser and your application is deployed on their machine, it even checks to make sure the right JVM is used. Because it is a full application and not an applet, your app won't be sandboxed, and you don't have any access restrictions in your program (other than the normal java stuff..), and for example, it would be easy to do what you mentioned and talk to a mySQL DB. The only downside, is what I mentioned earlier, is that you would have to design a UI in java.
Web Start Wikipedia Page
Sun FAQ on Web Start
Grails may be a useful starting point. It'll provide you with a web server solution that's standalone, and it'll look after the JDBC requirements and the CRUD (create-read-update-delete) capability via dynamically generated web pages. It should take minimal effort to put together an app providing your database interfacing via web pages.
(fyi. Grails is the Java equivalent of Rails)
If you feel comfortable with Java EE-based web development, you could probably just bundle your application with Tomcat or Jetty.
If you do not want to run standalone servlet container just for one application, you can also embed Jetty into a runnable Java application (see documentation here).
Either way you can leverage existing Java EE frameworks (Spring JDBC, Hibernate, all those web frameworks) for abstracting away technical complexities, although with embedded Jetty, you'd probably need to write some kind of integration layer for the web application framework of your choice.
I think you should give Restlet, a lightweight rest framework a try. The tutorial shows you how to start a local webserver, and by that deliver a "Hello World" through the browser within minutes (no joke!), and there's plenty of extensions for any kind of need.
In combination with Java Web Start by which you can deploy and start the application to the local host this should be what you need.
as someone suggested already you can use embbeded jetty server on your application and just let your user to start it using somekind of shell script or batch script. You only need to make your layour directory complaint with a Java Web Application and your on it. ie:
MyApplication
app/
WEB-INF/
lib/
classes/
web.xml
start.bat |
start.cmd - depends on your client OS
start.sh |
Then you should only need to take care of launching Jetty in your start.[bat|cmd|sh] with your app as your webaplication context and your done!
Using JDBC doesn't mean that you have to write an applet, you can use JDBC in any kind of application: a desktop application, a web application, EJBs, MDBs, etc.
You want to use a browser and Java on the server side? Then go for it and use Servlets / JSPs. Consider maybe using an presentation framework (Wicket, Struts2, Spring MVC,...), Hibernate for data access and Spring for other facilities and wiring. Grails is a good idea too.
BTW, I'm not a PHP specialist but PHP has object-oriented capabilities (introduced in PHP4 , enhanced in PHP5) so you won't sacrifice everything if you choose PHP.
So it really depends of what you want to do. If you want to write some Java (webapp or desktop app): choose Java. If you want to put quickly a few web pages in place and have an apache server, choose PHP. If you look for really high productivity, go for RoR or Grails.
You can try GWT + Google Gears
GWT is a GUI toolkit similar to Swing for the browser. Google Gears is a browser side database. Your app is completely in Javascript in a single HTML file and cross-browser compatible.
GWT app can make Server calls and Gears can sync up with a Server database. So you need not restrict your app data completely to the local desktop.
If you're interested in some experimentation, like new stuff and would like to reuse the plethora of Java libs (including JDBC) then you might be interested in the lift web framework, which is Scala-based.
If you want to do it as an applet you can. Sign the applet and give it permissions to the local network (to connect to the MYSQL server that way)... that should be possible. Here is a tutorial on it.
If you have a Java based web application (J2EE webapp - never mind which other underlying frameworks are being used), and you wanted to introduce a Flash based front-end, would you use Laszlo or would you rather expose a ReST-like XML interface and build and deploy a Flash application that uses that?
On one hand, Laszlo is quite amazing - doing for Flash what JSP does for HTML. It is easy to work with. It fits in very well with the rest of the web application (which is JSP based).
On the other hand it might be better to develop a complete Flash app decoupled from the server and use an XML-over-HTTP mechanism to bind the two. This would have the added advantage of being able to use the same XML interface for an AJAX front end if needed.
What would you do, and why?
I would create the contract-first services, deploy them separately, and then write the RIA client to access them.
Coming up with the schema first has the added benefit of completely decoupling the two during development. The RIA developer can create some synthetic XML streams to use for data while waiting for the services to come on-line.
I might have considered Laszlo in the past, but today, I'd stay within the Java stack and use JavaFX.
Laszlo is the product that never made it, there isn't a big enough ecosystem of developers around it.
I'd use Adobe Flex for the front end. The same benefits of using a markup language for doing flash, but it has a much larger developer base and open source projects to draw upon. For the data communication, use either REST or if you want to get clever, use BlazeDS.
OpenLaszlo is a complete RIA framework, so I'm pretty sure that you can 'compile' it to a completely standalone app that communicates with the server over HTTP. It's really very similar to Flex. The advantage Flex has is a much bigger community, a full-blown IDE, and more resources (Adobe), while OpenLaszlo is a little more innovative in that you can deploy to Flash or AJAX from one codebase.
I've actually spent some time working on a implementation similar to what you're suggesting. I had a complied Open Laszlo front end embedded in a web page with a Django (a python MVC library) REST interface on the backend and no Open Laszlo server. It works reasonably well, but there are a couple of things to watch out for. Open Laszlo only supports calls to GET and POST, so you won't be able to easily use the DELETE and PUT methods in your REST API. The other is the lack of community around Laszlo (as mentioned elsewhere). I can sometimes be frustratingly difficult to answer some basic questions when using Laszlo, particularly around the XML HTTP API and XML replication features in the framework. I personally never really looked at the Laszlo back end server seriously as I wanted an open API that could be consumed easily by other clients.
All this being said, the implementation does work and can be effective if you're willing to work around the limitations mentioned above. Plus Open Laszlo is free, which can be a really big plus if your working on a budget.