Originally posted by Rebecca Abraham:
Hi,
I was asked the following questions in an interview.I was unable to answer them as I'm just a beginner to Struts. Could someone please tell the answers?
1. Can JSP always be used instead of a servlet?
2. Which is better - using a servlet or using JSP, and what is the reason?
3. Why the Action class is not used as the controller instead of ActionServlet?
4. Advantages of Struts. Are ther any disadvantages?
[ July 26, 2004: Message edited by: Rebecca Abraham ]
1. Can JSP always be used instead of a servlet?
A JSP is a kind of metadata file for a servlet, (in case your developers don't know
Java, which was one of its selling points back at 1.0), and since JSPs compile into servlets, and you can override any method in the servlet hierarchy except for service(), I believe, so the answer is yes, but you may not see anything on the page depending on what it does. (Some servlets do work but display no data, etc).
2. Which is better - using a servlet or using JSP, and what is the reason?
Depends on the problem. If the problem is categorized by data retrieval and/or data displaying, then use a JSP. If it's work with little or no gui interaction required, then a servlet is better, where the ui part is delegated to a JSP through forward() or sendRedirect(). (Imagine a JSP that does work, like starting off threads in its init() method when a special 'knocking' request comes in, but the page is blank, so the user would see nothing. Not to mention it requires using scriplets, very ugly!)
3. Why the Action class is not used as the controller instead of ActionServlet?
Need to study the MVC architectural
pattern. In Struts, the ActionServlet controller distributes requests to Action classes that can do the work, according to the ActionMapping section of the struts-config.xml file. For the Action classes to be a controller, they would have to be able to receive incoming requests, that is, something to control, and hence be servlets by definition. (They are not meant to be serlvets). Also, Action classes can delegate (forward) to other action classes and hence form a 2nd layer of control anyway.
4. Advantages of Struts. Are there any disadvantages?
(No order implied in my answers)
Advantages:
- You get a clean separation of model from view, and most of the hard boring work is done for you. (Don't believe me? trying building a web program that cleanly separates the model from the view and you'll see the number of extra classes you need to write is quite large).
- Well accepted technology, and hence, probably well tested. Many websites about Struts to go to for help.
- Nicely separated configuration metadata from actual implementation, it's all in a struts-config.xml file. (That file can be broken into sub-config files, too).
- Easy to install in all available web containers, like
Tomcat. (It's little more than a bunch of jar files you put in the right folder).
Disdavantages:
- Struts has a learning curve, like all technologies. Is it worth the developer's time to study, learn, and debug using the Struts framework? (Surprisingly, the answer isn't always going to be yes).
- It's open source. (Large companies will often baulk at using open source stuff. They like to sue people if a technology fails them in some way).
- It's built around one thing, the MVC design pattern, used for separating the data (model) from the presentation (views). If you don't have ui views, (i.e. webs services?), then MVC may not be a good pattern to use and hence Struts may not help you as much as you think.
- I've also heard it doesn't scale well to extremely large sites. (Do you think Amazon.com uses Struts?) Struts may or may not be beneficial to portals, I'm still investigating this.
- There seems to be no standard configuration tool to set up Struts. You need to organize your projects folders for action clases, form beans and so on, yourself. People, we need a graphical Swing tool to define the action classes and form beans and build the xml file.
Let's face it, the struts-config.xml file will grow as large as a DD in
EJB soon. Wanna edit that by hand in the future?
Jeff Walker