tags:

views:

211

answers:

7

I'm a .NET web developer who has just been asked to produce a small demo website using NetBeans IDE 5.5. I have no experience with Java up to this point.

I've followed a couple of quick tutorials, one which just uses a JSP file and another which uses a servlet. My concern at this early stage is that it looks difficult to keep my application code away from my markup.

Using JSP files looks quite similar to the old days of classic ASP. On the other hand, servlets look useful but seem to involve a lot of writing out markup to the output stream, which I'm not happy with either.

Is there an equivalent to the ASP .NET code-behind model, or any other strategies for separating out markup and code? Is it possible to keep markup in the JSP and then use the servlet from the JSP?

+1  A: 

Wicket has "proper mark-up/logic separation" (as they put it).

Fabian Steeg
Thanks for the suggestion, however I'd rather use something that is built in to the IDE.
gilles27
+1  A: 

You might want to take a look at Custom Tag Libraries in JSPs

Nikhil Kashyap
+3  A: 

You can't do something similar with ASP.NET code behind using pure JEE technologies. You need to use an MVC framework like Spring MVC or Struts. The idea is that you create your controller (Java Class) and a JSP page and configure an action to tie the JSP page with the controller.

It isn't a simple as the ASP.NET as it requires configuration and an external framework.

Netbeans has JSF framework embedded. Have a look at this tutorial.

kgiannakakis
+1  A: 

For a simple website I would use Struts and Tiles, and make use of the Tag Libs that they provide to make the JSPs look neat and marked up, and separate the code into Actions in the Controller - an Action is just a block of code the Struts servlet calls after doing its work.

It doesn't take long to get the basics of Struts 2 and Tiles, you can pick it up in a day easily.

And yes, Servlets are quite a simple level to write such systems, and it is easy to end up with HTML generators there, which as you've said, is clearly not right. I think you need to be putting more values on the request and session objects, and then formatting them within the JSP.

The strength here is that you can pick your MVC framework to match the scale of your application, yet still have access to raw servlets should you require them for specific actions (e.g., one thing I did in the past was generating and serving an Excel file from values in the database).

JeeBee
When you say I could pick it up in a day easily, is that allowing for the fact that I've never used Java until today?
gilles27
If you've used C#, yes. You have to write a method body in an Action class (validate input, do work, set fields for output). Writing the stuff behind that (database, etc) is another thing of course.
JeeBee
+2  A: 

The thing about Java is that it doesn't really come 'bundled' with stuff in the same way .NET does... you generally go looking for the goods.

This makes starting with web apps in Java daunting because there are so many options. Many of them are built off JSPs, which is a plus.

If you are set on using vanilla JSPs you are not going to have a good time. Otherwise, I would suggest Wicket, Stripes or Spring MVC.

Stripes is probably the simplest of the three (Spring is a little complicated and Wicket is conceptually different from the other two). Having said that Spring MVC is probably the most used of the three..

All Stripes really is is JSPs and ActionBeans. Action beans are Java classes that contain actions, which are methods that perform your actions. Theses ActionBean classes also contain the data for the current page.

This page has more information about Stripes.

SCdF
I've marked this as the answer because, in the time I had to evaluate different options, Stripes looked like the easiest to pick up and use, and provided the separation I wanted.
gilles27
A: 

You can use this and do the same as what you did in .NET! It is a very good MVC framework.

+1  A: 

If you want to get something out quickly (I note you're putting together a demo), I would perhaps forgo the Java web frameworks, and simply use the servlet solution, combined with a simple templating solution.

Either Velocity or Freemarker will provide the facility of separating your markup from your code. Velocity is simpler than Freemarker. Freemarker is more powerful than Velocity.

I'm not down on the various frameworks mentioned. It's just that if your timescales are short and you're happy to program to the servlet interface for this requirement, then the above may provide the quickest/dirtiest solution :-)

Brian Agnew