11 Replies - 2846 Views - Last Post: 27 May 2014 - 04:40 AM

#1 Cryonic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-May 14

MVC Architecture Question for Java EE Webapps

Posted 22 May 2014 - 08:33 AM

Hello,

I'm currently starting to develop a web application, and I'm having some problems concerning the communication between client and server and how to apply the MVC/Model 2 pattern with the things I require. This is a very general question because I'm still in the planning stages, and I only need a general answer, so I'll try to describe what I'm trying to do and what I don't know a good solution for.

The application is supposed to feature an account system. The users are saved into a MySQL DB, together with information about the trophies/achievements they have won for contributing to the community. There will be pretty much 2 important pages: one, where the user fulfills a task, and the other, which displays the user's account information and his trophies.

I'm going to use JSPs for the view, with a Servlet as a Controller. I know that it's possible to use Javabeans as a Model, so I think I could generate user-entity objects from the Database. My problem is, that I need to generate quite a lot of HTML code specifically for every user to display his achievements, but I don't really see where I can generate it with a MVC pattern. I can't generate it in the servlet if I don't wanna make it messy. Accessing the javabeans via JSP only let's me read the beans' field contents, but generating a lot of complicated HTML code is impossible without using messy scriplets (which go against MVC again).
One last idea I had is that I could construct JSON objects from the Users and request them via AJAX every time a page is called. Then I could build the HTML in the javascript code. But then again, what would I even need the javabeans for, I could just omit them and directly read the Userdata from the DB. Is there any better, flexible solution, so I can process the User information and generate HTML code from it without destroying my MVC pattern?

Thanks in advance for your time & effort!

Cryonic

Is This A Good Question/Topic? 0
  • +

Replies To: MVC Architecture Question for Java EE Webapps

#2 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: MVC Architecture Question for Java EE Webapps

Posted 22 May 2014 - 08:09 PM

Quote

My problem is, that I need to generate quite a lot of HTML code specifically for every user to display his achievements, but I don't really see where I can generate it with a MVC pattern.


It should be placead in the view technology, which is, in this case, the JSP.

Quote

I can't generate it in the servlet if I don't wanna make it messy.


In MVC, servlets are meant for logic, not presentation code.

Quote

Accessing the javabeans via JSP only let's me read the beans' field contents, but generating a lot of complicated HTML code is impossible without using messy scriplets (which go against MVC again).


Use JSTL/EL.

Quote

One last idea I had is that I could construct JSON objects from the Users and request them via AJAX every time a page is called. Then I could build the HTML in the javascript code. But then again, what would I even need the javabeans for, I could just omit them and directly read the Userdata from the DB.


Bad idea. You're trying to use Ajax to fix a misuse of Java EE.

If you really to push the MVC envelope, consider using a front controller, actions for business logic, and JPA/ORM/DAO for data.

Also see:

How to avoid Java Code in JSP-Files?
Design Patterns web based applications
Was This Post Helpful? 1
  • +
  • -

#3 Cryonic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-May 14

Re: MVC Architecture Question for Java EE Webapps

Posted 23 May 2014 - 03:09 AM

Thanks for your reply.

Isn't EL nearly as bad as scriplets, if I try to generate a lot of HTML by reading and processing the javabeans' fields? On the other hand, these would of course be only view-related, so the MVC pattern would still exist.

It is important to note, that I don't have unlimited time for this to get used to a lot of different frameworks. It's part of a project for University. I'm already new to Java Web Development, so going deeper and deeper into more advanced frameworks and APIs is probably something I should avoid if it isn't absolutely necessary. The project is not as complicated to justify this I think.

I suppose using EL to generate the HTML is not a bad idea as a middle-ground between best practice and considering the time constraints I have.

I am still unsure about some of the details of JavaBeans with a database. I would like to create a "User" bean, which is being instanced with data from database when a user logs into the website. Whenever a user completes another task, I would like to send this information to the Controller servlet. The servlet would then save the progress in the User's bean and let the bean write the progress into the database. Would this be possible this way? Also, how does the garbage collector decide when to delete the javabeans objects? I don't know whether having a lot of user objects in RAM simultaneously would ne problematic.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: MVC Architecture Question for Java EE Webapps

Posted 23 May 2014 - 04:55 AM

Quote

Isn't EL nearly as bad as scriplets, if I try to generate a lot of HTML by reading and processing the javabeans' fields? On the other hand, these would of course be only view-related, so the MVC pattern would still exist.


No. Did you read the links I posted? If not, read them, and you'll see they're the preferred way of doing things.

Quote

I'm already new to Java Web Development, so going deeper and deeper into more advanced frameworks and APIs is probably something I should avoid if it isn't absolutely necessary.


I see. Well you could skip the ORM. DAO is a fairly simple concept. You could learn that quickly. Otherwise, just create a API (that hides the SQL) for data persistence along with your JavaBeans.

The requirements for your website are few, so you may be able to learn enough JSTL/EL to implement what you need. The examples are self-explanatory.

If you don't want to use them, then you have no other choice but to use scriplets if you want to stay within JavaEE technology and without 3rd-party frameworks. There's also JSF, but that's even more advanced.

I can't advocate for AJAX, because, as stated before, you're using it as a work around for a problem that already has a clear solution. AJAX would work fine, although you'd probably be making more work for yourself with all the Javascript/HTML integration.

Quote

I suppose using EL to generate the HTML is not a bad idea as a middle-ground between best practice and considering the time constraints I have.


Where are you getting this notion from? I've can't find a single article arguing against the use of JSTL/EL. If there is, it's probably arguing that developers should move to JSF.

Quote

The servlet would then save the progress in the User's bean and let the bean write the progress into the database. Would this be possible this way?


Using ORM, that would be easy. You just write the beans, and let the framework take of persisting it. Again, if you don't want to do that, just write a data access API that does the SQL. This DAO tutorial is good, but if you want something simpler just (1) create a utility class that encapsulates database operations, or (2) just throw the SQL in with the JavaBean class.

E.g.

class DBUtils {
     boolean saveUser(User usr) { /* write SQL */ }
     User findUser(int id);
     boolean updateUser(User usr);
}



class User {
    int id; 
    String name;
   
    //getters/setters

     boolean save() { /* write SQL */}
     .
     .
     .
     static User findUser(int id);
}



In your example, a user may have many tasks. E.g.

class User { List<Task> tasks; }



The nice thing about using an ORM (like Hibernate) is that you're entity classes can have collection fields and Hibernate will persist everything perfectly.

Without ORM, you'll need to keep everything separate and persist each class individually. E.g.

class User { int id;}
class Task { int id, custId; }



It's really not that much more effort though.

Quote

Also, how does the garbage collector decide when to delete the javabeans objects? I don't know whether having a lot of user objects in RAM simultaneously would ne problematic.


Not sure about that. I'm sure you can Google it and find out. You're not creating a production system, where something as such might actually be concerning down the road, so I wouldn't worry about it.

This post has been edited by blackcompe: 23 May 2014 - 04:59 AM

Was This Post Helpful? 1
  • +
  • -

#5 Cryonic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-May 14

Re: MVC Architecture Question for Java EE Webapps

Posted 23 May 2014 - 05:13 AM

I've read your links, actually I've seen them before I posted here so I knew their contents already. I just remember reading somewhere else that I should try to keep my JSP clear of long EL and scriptlets alike, but I'm glad using EL would be a good solution.

I was thinking of a very similar solution for the database interaction to the one you posted as code, thanks. This is probably what I am going to do, to focus more on the task. Since the amount of different beans classes I need will be very limited (probably 1 or 2 only), doing the persisting manually should be fairly simple.

Thanks for your competent help! If I run into problems during the project, I'll probably ask for some more pieces of advice. For now I know enough to start programming.
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: MVC Architecture Question for Java EE Webapps

Posted 23 May 2014 - 05:14 AM

Glad I could help. Good luck.
Was This Post Helpful? 0
  • +
  • -

#7 Cryonic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-May 14

Re: MVC Architecture Question for Java EE Webapps

Posted 25 May 2014 - 02:13 AM

Hey again,
actually, I realized I've got one small question left.

I've read a bit on the syntax of expression language and a tiny bit of JSTL stuff. What I seem to be missing until now though, is an idea how to create HTML content with it. I see how I can fill in html-tag attributes or fill in text with it, but I haven't seen an example creating HTML content yet.

Suppose I have a blank page which needs HTML generated. I'd read the score of a user from a javabean (User.score) and then display the appropriate trophy for him (via a <img src="url"> tag). How would I do that, could you give me a very brief example? I know how I can read and process the score from the bean but not how I'd conditionally add HTML.

Maybe I haven't read enough on the subject yet, but it would help if you could point me in the right direction.
Was This Post Helpful? 0
  • +
  • -

#8 Cryonic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-May 14

Re: MVC Architecture Question for Java EE Webapps

Posted 25 May 2014 - 03:33 AM

I probably found what I'm looking for.

I suppose I could do a if/else with the JSTL and then use the jsp:element, jsp:attribute and jsp:body standard action tags to create HTML tags and fill them. Is this the way you'd do this properly?

Sorry by the way for the double-post, am I blind or why can't I find an edit button on my last post?
Was This Post Helpful? 0
  • +
  • -

#9 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: MVC Architecture Question for Java EE Webapps

Posted 26 May 2014 - 05:11 AM

Quote

Suppose I have a blank page which needs HTML generated. I'd read the score of a user from a javabean (User.score) and then display the appropriate trophy for him (via a <img src="url"> tag). How would I do that, could you give me a very brief example? I know how I can read and process the score from the bean but not how I'd conditionally add HTML.


I can't, sorry. Java EE code is too complicated for me to be posting examples here. I try never to do that, but instead give high-level guidance. There are plenty of tutorials out there, including Sun's own Java EE tutorials. I hardly ever use Java EE, but I know what you're asking is simple, so I'll leave it you to figure out.

There's an if tag in JSTL that generates HTML conditionally.

Quote

I suppose I could do a if/else with the JSTL and then use the jsp:element, jsp:attribute and jsp:body standard action tags to create HTML tags and fill them. Is this the way you'd do this properly?


What I briefy read about <jsp:element> says that the tag outputs an XML element, so unless you're sending XML to the client then, no, that is not correct. Why not just put HTML inside the tag? Have a look at the flow control tags of the core library. That should help.

This post has been edited by blackcompe: 26 May 2014 - 05:12 AM

Was This Post Helpful? 1
  • +
  • -

#10 Cryonic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-May 14

Re: MVC Architecture Question for Java EE Webapps

Posted 26 May 2014 - 06:41 AM

I somehow assumed this wouldn't be possible, but you're right, I can just put HTML into a <c:choose> to select the specific HTML code I want to insert conditionally.

However, just out of curiosity: Wouldn't using jsp:element be possible as well? After all, if you write an XML file with the standard HTML tags it is essentially usable as an HTML file. XHTML is pretty much proof of this, since it is not much more than a stricter version of HTML.

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#11 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: MVC Architecture Question for Java EE Webapps

Posted 26 May 2014 - 11:56 AM

Quote

However, just out of curiosity: Wouldn't using jsp:element be possible as well? After all, if you write an XML file with the standard HTML tags it is essentially usable as an HTML file. XHTML is pretty much proof of this, since it is not much more than a stricter version of HTML.


It is, but I just don't see why you'd go about it that way. It's extra clutter.

This post has been edited by blackcompe: 26 May 2014 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#12 Cryonic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-May 14

Re: MVC Architecture Question for Java EE Webapps

Posted 27 May 2014 - 04:40 AM

Yes, I know. The way you put it it just sounded like it wouldn't be possible that way, that's why I asked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1