Skip

JSP/servlets whoas and woes
September 28, 2010 6:55 AM   Subscribe

JavaFilter: I've pored over dozens of tutorials and read Head First Servlets and JSP. I thought I understood how to utilize servlets in order to generate dynamic content. I can get a simple form/one page site going but am at a complete loss when trying to come up with a design for a full web application. My boss wants a demo by the end of the week (he has seen the single pages, now wants them to work together). My anxiety levels are skyrocketing as it has become clear I don't know what I'm doing. Details inside.

Before I get any this belongs in a tech forum replies, I'm not currently a member of any and want to avoid being that person who signed up to post a question. This is generally frowned upon, no? I'm hoping the tech-smart mefites can help.

I'm working on a exam application in which users can enter through either the main page listing all the exams or directly through the page for an exam. The exams, questions and answers are all stored in a database and the html to display them are dynamically generated. I already have working DAO and DTO classes.Getting data from the database to the JSP/servlets isn't an issue. There is also a profile page in which users can edit their profile or create a new one.

All the "pages" look the same. There's a header, footer, nav bar and main content box. The nav bar only has two items, home and profile. Clicking on any of the items should change what is displayed in the main content box. When an exam is selected, it is displayed in the main content box. What I had in mind is to create home, profile and exam JSPs with code to populate the main content box. The servlet (controller in the mvc pattern) is to figure out what url was used, set a session variable and forward the request to a main JSP containing the complete layout code. From there, the JSP will use an include directive based on this session variable to populate the main content box. I didn't want to dump all the DAO method calls to get all the data objects and the code to process them (ie associating answers with questions) in one giant servlet file so I thought I could create a servlet for each JSP. This is where I'm stuck. The main servlet will call the corresponding servlet for a JSP which do its data processing and pass the request back? That sounds like bad design that won't work.

Questions:

Would it be better layout design to create header, footer and nav bar component JSPs and do an include in the home, profile and exam JSPs?

How the heck do I get the servlet to run before the JSP? The URL pattern for the main servlet mapped to /* in my web.xml file but accessing any of the JSPs won't run the servlet first. It has to run before processing anything else since it loads the database connection and user information into session.

How can I load the main JSP with all the layout code no matter what URL (home, profile or exam) page I enter through?

The embarassing million dollar question. How do I link the pages of my application together? Maybe my brain is fried. I can't for the life of me figure out how to use one main servlet and several "helper" ones to populate that main content box.

Added level of complexity. The application is to display 5 questions at a time. I'm thinking of splitting the questions into divs and using back and next buttons to show/hide the divs. Is there a better implementation?
posted by vilandra to Computers & Internet (7 answers total) 1 user marked this as a favorite
 
I do web application development professionally. I cut my teeth on J2EE development, but over the past five years I've been more on the PHP side. Because my Java skills aren't entirely up to date I'll go light on specifics and defer to someone more up to date, but I want to offer a technical and "soft" tip here.

Technically, what you want to do is to use a framework. A lot of the questions you ask concern how to manage passing control and object state from page to page. A framework will do this for you. Something like Struts or Tapestry may do the trick. It will take some initial time to get fluent with this, which leads to the soft tip.

A general mock up creates the illusion that the system is nearly built and it just needs to be "wired together." You know the reality is different. The mock up just build consensus and gets things started off well. You need to express to your boss that it is going to take more than a week of time to build this -- not because you are learning the ropes, but because everyone will need more time to evaluate and respond to the working system.

Trust me, when people use a system they can poke and break they think about it much differently than when it is a stand up demo. You will need time to evaluate feedback and often retrace your steps to accommodate changes in design that come naturally when people start to use it. If your boss thinks this is so simple that no refinements could arise, he is plain mistaken. Software evolves and a week is too short of a gestation period.
posted by dgran at 7:45 AM on September 28, 2010 [1 favorite]


Is there some specific reason you're using Java for this? Is the goal more "learn to develop Java web applications" or "develop a working exam application"? If the latter, I'm not sure Java is the right choice in this case. There are many good reasons people develop web applications in Java. But IMHO it's not particularly well suited to simple web apps being developed under tight deadlines by programmers with no experience. I think you'll have better luck with a simpler development environment.
posted by sharding at 8:45 AM on September 28, 2010


I've done some development with Struts before, so I may be able to help. All of this advice varies depending on what technology and what versions you're using, though, so keep that in mind.

It's important to understand how your config files work; in Struts, the struts-config maps servlets to url paths in your application, and the tiles definitions map the paths to the JSPs. Just google struts-config.xml to find some examples. Once you have a few action tags defined in your config file, it should be simple to link all your pages together. Your problem of the the servlets not executing before the JSP is interpreted suggests that there are some problems with your configuration; you can't just access a .jsp page directly and have the servlet run automagically; you've got to wire it up with XML config files.

Regarding your layout JSPs, all you should need to do is include the layout tiles in the page you are loading (I don't remember the syntax off the top of my head), but these should also be mapped in tiles-def.

Hope this helps.
posted by Androgenes at 9:44 AM on September 28, 2010


- Yes, by all means create seperate JSP's for your header, footer, navigation, etc. and then include them from your main page JSP's. When you need to change a header you'll be glad you did this.

- JSP's should really be inside the WEB-INF folder, and so will not be directly accessible by a browser. I believe there are also some weird behaviours that happen when you map the main servlet to /*. Most apps I've worked with map it to /app/*, and maybe have another one for /admin/* (admin tool section), or others. Sometimes it might even be a single servlet with multiple mappings like this, but no /*

- Which JSP to serve up should depend entirely on what your servlet does with the request.

- You could use one main servlet and then hand off request to "helper" servlets, but I don't think you'll be helped much by this. Try a flow like this...

. Servlet receives a request and interprets it. It decides what "request" is happening, and hands it off to appropriate "request handler". (this could be as simple as looking at the url and parsing it for "page1" or something)

. The RH set's up any data objects, performs any front end validation and decides what "event" is happening (could be "bad data", "login", "QuestionPage", or whatever).

. Controller then calls up the appropriate event handler. (Again, this can be as simple as seeing "X" event and so calling up "XEventHandler" class) EH performs work, decides what page comes next.

. Controller forwards to appropriate JSP (as decided by the EH), and sets up any data objects that it requires in session, or page, or application objects as appropriate.



- Nothing wrong with that approach for navigation but with the questions you are having, you could tackle that later.


I have basically described a really rough outline for a framework, as mentioned above. Frameworks take care of most of these details for you. Rolling your own as I've laid out is likely extreme overkill for what you are trying to do. Really, for your needs and at your experience level, you should do what works and is fast, regardless of if you think "it's the right way" or if it's "ugly". I also agree with the above assessment that Java is not necessarily the best tool for the job. Who is going to be using this? Where will it be hosted? Java web app hosting tends to be either expensive or highly skill intensive.

I'll add that I'm speaking from a few years of application maintenance experience and I have not tried to roll something from scratch for ages. I inherit apps and do bug fix or enhancement work.
posted by utsutsu at 9:56 AM on September 28, 2010


Technically, what you want to do is to use a framework.

Seconded. I'll get to that.

Now, to some individual questions:

Would it be better layout design to create header, footer and nav bar component JSPs and do an include in the home, profile and exam JSPs?

I would. You may want to use JSP Templates which are a bit more flexible, and a bit more complex.


How the heck do I get the servlet to run before the JSP? The URL pattern for the main servlet mapped to /* in my web.xml file but accessing any of the JSPs won't run the servlet first.


You would have to forward or redirect the request to the JSP. But if you want to run code before every request you're looking at something like a Filter.

It has to run before processing anything else since it loads the database connection and user information into session.

I would personally do the database connection at application startup by creating a listener for a server lifecycle event--I mean, assuming you just have a pooled DB connection that everything uses. If you're making a new connection for every user/request, I guess you can do it your way (but it won't hold up to heavy use. This may not be a problem for you.)

As for user info, I generally prefer not to do my own user identification/authorization because it's easy to mess up, and the damage is high when it does mess up, and lots of smarter people than me have done it first.

How can I load the main JSP with all the layout code no matter what URL (home, profile or exam) page I enter through?

JSP Templates, linked above.

The embarassing million dollar question. How do I link the pages of my application together? Maybe my brain is fried. I can't for the life of me figure out how to use one main servlet and several "helper" ones to populate that main content box.

Just so you can get your mind wrapped around stuff right, let me just say that JSPs are servlets, too. The application server transforms the JSP into a servlet automatically and puts it into a special directory on the fly. So you're when you want to put a servlet in another servlet, you can perhaps see how that won't go well. In general you can't have one servlet write half of a document and another servlet write the other half; once a servlet starts writing it's going to be the one to finish it out.

Looking at the way you're doing things, it looks like the most straightforward thing to do is to use a custom tag. An example is here which demonstrates a simple "hello world" kind of example. Based on what I think I understand from your question, you would perhaps create custom tags for home, profile, and exam content.

Finally, a framework may help, if only to do access control which is somewhat lacking in flexibility in the usual base Tomcat setup (if that's what you're using). Struts, Spring, and Play all have their proponents, and they are listed here in order of how "heavy" they are; how different they are from what you're doing and how many things they do for you (and of course, can be more complex in the name of simplicity).

Also, RESTlet might help you out, it's a lightweight way to add REST semantics, if that kind of architecture is easier for you to follow.

With your background/experience I would recommend as a first step looking at custom tags to do the grunt work of including what you need, and then work on integrating it into a whole with JSP fragments and/or templates.
posted by RikiTikiTavi at 10:08 AM on September 28, 2010


Good advice so far. I just want to add that whatever you're doing, 2-3 days might not be enough to really finish up - even for a pro. I'm not sure what your demo needs to have, but Java web dev is fairly time-consuming in general. You might want to talk to the boss early and ask for a few days' extension.

Of course, your boss may be asking for a lot less than I'm picturing...
posted by Citrus at 11:57 AM on September 28, 2010


2-3 days might not be enough to really finish up - even for a pro

Oh, yes--listen to this. This is not going to be done by your deadline. You're going to have to take whatever action you need to do with that in mind. In general, the earlier you tell someone about a deadline to be missed, the easier it is to deal with it.
posted by RikiTikiTavi at 1:59 PM on September 28, 2010


« Older I use Videora to convert my v...   |  Pumpkin ideas for a Halloween ... Newer »
This thread is closed to new comments.


Post