aspose file tools*
The moose likes Servlets and the fly likes why servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "why servlets" Watch "why servlets" New topic
Author

why servlets

Ashimashi Kabashi
Ranch Hand

Joined: Sep 11, 2006
Posts: 84
Hi,

I have done server side scripting on JSP and have used it to effect in accessing (xml, SAX, JDBC) etc.

My Question is that, does a good Java Web Component Developer also need to know the ins and outs of a servlet? Most job interviewers ask about servlet lifecycle etc. Why? It IS true that every JSP gets translated into a Servlet, are there some things that can only be done optimally through a servlet and not a JSP Page OR when would a developer have to write a servlet and not a JSP Page?

For Example I was asked to write a dispatcher servlet at work. Why do this? Why not just create a JSP Page?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
My Question is that, does a good Java Web Component Developer also need to know the ins and outs of a servlet?


Short answer: Yes

If you will browse through the recent posts in this and the JSP forum you will see plenty of cases where "developers" got in trouble due to not knowing the ins and outs of the servlet API.

Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

JSPs are meant to create the "view" of web app pages. Using them for processing is a misuse of the tool in the same way that using a hammer to whack a screw into a board is a misuse of a tool. Sure, you can get it to work, but the results aren't pretty.

The fact that JSPs are translated into servlets is an implementation detail that cannot be used to justify the misuse of JSP.

You may find this article helpful.
[ April 14, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ashimashi Kabashi
Ranch Hand

Joined: Sep 11, 2006
Posts: 84
To Bear.

Your article gave me a fairly good understanding of MVC.

Thanks again!
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Dig into some very well-known frameworks like struts to know why Servlets ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Ashimashi Kabashi
Ranch Hand

Joined: Sep 11, 2006
Posts: 84
yes I realize that.

Can you suggest a good place to start struts. There are plenty available once you google, but can you recommend any?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

I would strongly advise against Struts until such a time as you have a good understanding of Servlets.
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Don't actually learn it deeply as Mr.Bear mentioned.

Just get an overview of the ActionServlet.

Regards,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Or better yet, just learn about the Model 2 (web app approximation of MVC) and Front Controller patterns. Again, I'd advise ignoring Struts completely until you understand the underlying basics. It'll just confuse you.
[ April 14, 2008: Message edited by: Bear Bibeault ]
Anshul Malpani
Greenhorn

Joined: Mar 05, 2007
Posts: 13
See there is specific use of servlet. Not for Design purpose.
One more thing Understanding the JSP properly you should have good knowledge of servlet. Servlet takes less time to load in memory compare to jsp. Because translation time involves.

If you are Java programmer you will find Servlet is good for writeing the java code.


SCJP-5.0
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Anshul Malpani:
One more thing Understanding the JSP properly you should have good knowledge of servlet.


I find the opposite to be true.
If you understand servlets, it's a lot easier to decipher what JSPs are doing.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
If one is just starting out to learn Java servlet programming, then learning by way of Apache Struts framework is a good step. Once Struts is learned, then a good understanding of why, how and when to use servlet, JSP page, custom JSP tag, etc. will be developed.

Learning how to use and build web applications with Struts will also help with understanding MVC design pattern implementation.

Basically, Struts provides a Controller servlet (ActionServlet) that application developers customize via Action classes and XML-based configuration files. With a instance of a Struts skeleton, applications can be developed quickly and in accord with practical design strategies.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Originally posted by James Clark:
If one is just starting out to learn Java servlet programming, then learning by way of Apache Struts framework is a good step.
I strongly disagree. It has been my experience that diving off into Struts, or any other obfuscating framework, prevents people from learning basic JSP and Servlet skills. Instead, they just learn the framework without understanding how the underpinnings work.

Learn the basics first -- then decide if or when a framework is warranted, and then which one.

I do agree that learning important patterns such as Front Controller, Command (what Struts incrorrectly terms "actions") and MVC are crucial. I just don't think that adopting a framwwork such as Struts at the outset is the best way to do that.
[ April 16, 2008: Message edited by: Bear Bibeault ]
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
I agree with Mr.Bear

To tell about my situation, i have learning about struts once, i understood the general idea, but, things were unclear.

After passing my programmer exam, i have decided to do my SCWCD, and after buying the Head First Servlets and JSP book, i have started understanding all the deep secrets of java web components.

Not only you can understand struts of any other web frameworks after understanding the core components, but you can really build your own framework as well.

Many companies now have their web based MVC framework.

And to be more honest, i enjoy dealing with core Servlets and JSPs more than dealing with Struts (although struts is a great thing) itself, do you know why ? Because i control each and every component in the application.

Now, dealing with frameworks has great advantages, but the main point is to be great at the core technology.

Regards and thank you for reading my long post.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
>I do agree that learning important patterns such as Front Controller, Command (what Struts incrorrectly terms "actions")..

The Action class is part of the ActionServlet, i.e. Front Controller. The ActionConfig class is also part of the ActionServlet. These classes enable the developer to customize the ActionServlet for their application.

I am not familiar with the association of the Struts Action class and the Command design pattern. As mentioned, this is not correct. The Struts Action class DOES NOT implement the Command pattern.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I could very well go with what Bear has been suggesting and insisting!

Learning Struts at first is like doing your Hello World Java program in a well known editor like WebSphere, WebLogic, eclipse etc., Instead what is suggested to get the best out of the "technology" is do with your 'command prompt' and invoke the 'javac', 'java' executables to get to know the real effect and knowledge.

The same holds good here!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Hi,

With regards to all posts referring to MVC framework and the likes, may I suggest that to act as a dispatcher is not the ONLY case we would need a Servlet for.

Another example could be when the response 'type' is needed in a format other than HTML (say, GIF, PDF or XLS). This can be easily achieved by writing Servlets using corresponding API (like iText for PDF) to dynamically prepare the response.

As a JSP gets translated into a Servlet, its always recommended to know the in-outs of Servlets. IMHO, JSP should be the second-step in learning after Servlets. That is more natural and helpful, at least to me.

Best regards,

- Aditya Jha
[ April 28, 2008: Message edited by: Aditya Jha ]
 
wood burning stoves
 
subject: why servlets