A Groovy Web Console in a Java webapp? - java

I've been looking at Groovy a little lately, and I love the idea of the Groovy Web Console.
I'm idly wondering whether there's a way to embed a simple web control to write and run scripts from in a web app I might put together myself.
Is there a plugin-style solution to embed a control like this?
Are there any resources or guidance out there to help in persisting source code?

The Grails web framework has the Grails console which is basically a Groovy console that has access to your web app's state. More specifically, in the Grails console there are a bunch of predefined variables which you can use to access/change the web app at runtime. For example ctx is a predefined variable that provides access to the application's Spring beans (AKA ApplicationContext).
Normally the grails console runs as a desktop app, but there's a plugin available that will let you include it as part of the application itself, and therefore use it via a web browser.

Related

Creating a web application with java

I have been trying for the past week or so to wrap my head around how I would go about creating a web application using java. I have looked into many services such as Vaadin, GWT, and CUBA, but because I don't really know which I need I haven't had much luck. Here is what I need:
Web application framework in java (I don't know HTML or Javascript)
Ability to use polymer elements
I would also prefer a minimized use of XML files, but I could work with them if need be.
The Java framework for web development is J2EE. It provides a Web Container to run web application (.war file) as well as other stuff.
Web application can be servlet, JSP etc.
If you are not familiar with Java Web Application, you may want to start with using Eclipse. The following video shows how to use eclipse to build a project for web application
https://www.youtube.com/watch?v=Av6zh817QEc
Since one criteria is to use Polymer I recommend having a look at
gwtmaterialdesign
there are already a few tutorials how to get started and also ready to use templates.
Anyway I believe that you've to dig a little into GWT ...

Can I create a Play! framework project and use existing Java code?

I'm currently in a project which already has some codebase in plain Java, we need to develop a new web application which will leverage on this plain Java code.
We already have code for database access, some business rules, etc. I really just want to use Play for the web development part and don't want to do a whole new implementation of models using a Play approach.
What I thought when looking for a Java web framework was to find something that I could embed in the project, start a server (like Jetty or something) in my application and it would take care of the web part. But reading Play's documentation I can only find examples on how to run Play, not how to embed it into some project.
What I want to know is: can I embed a Play application inside another application? If not, how can I use my already developed plain Java code inside a Play! project?
Thanks for your attention!
Provide your existing code as a Jar file and call it from your Play app.
If your application is too big/this can't be done for whatever reason, create some API in your app that Play can connect to (via webservices, etc) and use it.
I would not recommend embedding Play inside, not because it can't be done (I'm sure it can) but would be messy and it is probably a good idea to keep the different concerns (web/whatever your app does) separated for easier maintenance/upgrades.
If your codebase is small, you can try this solution:
put your code into app/ folder (should be generated by play).
3rd party jars addition explained here: http://www.playframework.com/documentation/2.0.4/SBTDependencies

Is it possible to reference a Java app from a Grails app?

We have a Java/Spring/Hibernate codebase which is the core to our pretty large platform. We also have quite a few separate Java webapps (Struts or Spring MVC) running alongside which reference the core system, pulling in the applicationContext and services from it.
We have a requirement to build another webapp that also references the same core system, so I was hoping to do this with Grails. From what I've found it appears you can easily jar up legacy Java code, place it in your Grails project lib directory and reference it that way.
http://www.itexto.net/devkico/?p=333
Unfortunately this will not work for me as I need to reference the running core system and its services from my Grails app, as you would reference a Java app from another Java app. Is there any way to do this or is it just not possible with Grails?
Any information would really help...
Thanks,
Justin.
I would look into building a Service Layer on top of your Spring services/beans. This is a pattern documented in Martin Fowler's Patterns of Enterprise Application Architecture book. Essentially you want a thin layer that wraps your Spring services (your business logic) that can be called by a web service or a remote call of some sort from your web-apps. This will allow you to host your applications in different processes but still be able to communicate between them without trying to hack everything into the same web container and figure out how to reference the same running processes.
See documentation on Grails+Spring integration. When you have gotten Spring working inside Grails, next steps should already be standard Spring.
As you have not detailed, how your existing Struts and Spring MVC applications "pull in applicationContext and services" I cannot comment on the details. However, it should be similar to the way your existing applications do it.
EDIT: check also answers to this question on how to organize your Spring beans.

What framework + application server + development environment to use for Java services?

I'm trying to write a Java service which runs 24/7, scrapes content from the web, and stores it into a database. What is the best framework to use for this given that I'd like to...
1.) Have an application server that I can deploy my code to (and have it run automatically). This application server should sit on a separate box from the machine that my development environment will run on.
2.) Have a development platform (I would prefer something based on eclipse) which allows me to deploy my code directly to the application server (so I don't have to ftp everything over to test).
3.) Utilize a framework like Spring.
In effect, I'd like to know what to choose for my...
1.) application server
2.) development environment (ide) -- if eclipse, what server adapter to use
3.) framework
So far, I've tried using Virgo with SpringSource STS, but was unable to configure the web server adapter for a non-localhost-residing server. I don't want to have to install Virgo on my development box, and I don't want to have to ftp hop my code over to my production server in order to deploy.
Why don't you just use Tomcat or some other web container, but it may be better to split this into two applications.
Have one that goes out and does the scraping, as a standalone application, for this you can pick anything, I would go with Groovy (http://groovy.codehaus.org/), as ease of development and maintenance is important here, and you can use the Groovy plugin for Eclipse.
The other would be the web service and for this I would think Scala (http://www.scala-lang.org/) would be nice, if you have time to learn it, but Grails (a groovy framework) would be beneficial, so you can write a REST or SOAP web service.
By separating them then you can pick the best solution for that particular aspect, since the web server shouldn't be involved in scraping, but the web server will want to read from the database.
These two languages run on the JVM and can use regular java classes/libraries, but there are improvements over plain Java in them.
Turns out there are some maven plugins that will remotely deploy my app for me. The most notable is Cargo. This way, I can keep all of my initial tools/services the same (Virgo, STS, Maven).
I've actually build something similar quite recently. My application could run without a servlet container or an application server. The reason I choose to run my app in a Tomcat servlet engine is so that I can add a REST API to it to easily retrieve server status information, but I digress.
The plain vanilla Eclipse J2EE installation has decent Tomcat support so without knowing more about your tastes and specifics I'd go with that.
To make your application self starting you need to implement the ServletContextListener interface:
public class ServerClass extends HttpServlet implements ServletContextListener {
public void contextInitialized( ServletContextEvent event ) {
// create and start a thread here.
}
public void contextDestroyed( ServletContextEvent event ) {
}
}
Add the following to your web.xml:
<listener>
<listener-class>com.my.ServerClass</listener-class>
</listener>
Which framework you want to use, only you can decide. Your question is to generic to give a decent answer on that. Read up on a few and pick one. Plain old Java will also do just fine and otherwise Scala might be a good substitute choice.
So, to answer your questions:
Tomcat servlet engine
Vanilla J2EE Eclipse version
Plain old Java

How to deliver a Java program locally through a browser

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.

Categories

Resources