File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes General Queries regarding JSPs. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "General Queries regarding JSPs." Watch "General Queries regarding JSPs." New topic

General Queries regarding JSPs.

Manish Vyas

Joined: Jul 21, 2004
Posts: 13
JSP is ultimately going to be converted into servlet by JSP engine....Can I use JSP as a controller in MVC Architecture(using JavaBean as Model, as I can use useBean tag to refer Model in JSP).. If so, How can I define doPost() or doGet() or service() method in JSP & then why Servlet is preferred over JSP for the same.... Can I define my own methods in JSP? Can Global Variable be defined in JSP?, If so or not, why?
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Hi Manish,

Sounds like this is your first time in J2EE. I suggest reading this
tutorial first to give you a good start.
[ July 21, 2004: Message edited by: arnel nicolas ]
Manish Vyas

Joined: Jul 21, 2004
Posts: 13
I am quite new to Java... and very keen to know these answers... If you can give me detail reply here itself, will be very obliged.
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
There is no need to override doGet() and doPost(), as the request and response objects are implicitly available in JSP. Check out the following simple example:

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

Can I use JSP as a controller in MVC Architecture

While no one will come to your office to break your kneecaps if you do, this is generally not considered a good pattern to follow. Do yourself a favor and become familiar with what is known as the 'Model 2 Pattern' where servlets serve the purpose of controllers, and JSPs are used to render views.

How can I define doPost() or doGet() or service() method in JSP

As Jeffrey pointed out, you don't.

why Servlet is preferred over JSP for the same

There are countless articles all over the web on why the Model 2 pattern is preferred. Do a bit of searching and all will be revealed.

Can I define my own methods in JSP?

Yes, but again, don't. There are now much better mechanisms such as the JSTL and custom actions to add functionality to your JSPs.

Can Global Variable be defined in JSP?

Depends what you mean by 'global'. If you mean class-level members, then yes. But be very very careful. It is very easy to make your JSPs non-thread-safe when doing so. My advice: don't.

In fact, my general advice when it comes to JSPs is: write your JSPs with no Java in them. None. Zero. Zilch. Nada. With today's level of JSP and Servlet technology, your JSPs should contain markup, JSP actions and directives, JSTL actions and custom actions. Scriplets and scriplet expressions should either be relegated to the past, or used only as a last resort (and since adopting scriptless pages, I have yet to come up against such a 'last resort' scenario).
[ July 22, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

JSP's were created in order to provide servlet functionality where the majority of the work was involved in producting presentation. I've seen systems where a JSP incorporated business logic and dispatched to a separaet JSP for presentation, and I blush to admit that I've created JSPs that were moe Java code than markup (any they were VERY ugly). That, however, was a long time agot where saber-toothed tigers still prowled the Earth and the resources available for J2EE development were considerably less refined.

As a rule, the logic should be in servlets and the markup should be in JSPs. Although JSP-capable page editors tend to be neither common nor cheap, you ability to edit and maintain JSPs regardless of the tool used drops rapidly as you add Java code to them. The clash of syntaxes causes everything from poor editor performance (Emacs basically hists a wall everytime your cursor in or out of scriptlet code) to outright failure.

Model 2 is the name given in the J2EE world to the popular attempt to mimic how MVC works in the constricted world of HTTP. Probably the most famous implementation of Model 2 is Struts.

However, Struts may end up being supplanted by JSF in the long term - JSF is designed to permit componentized "drag and drop" design. But JSF is still a new product and I'm not sure how much I like it yet.

An IDE is no substitute for an Intelligent Developer.
subject: General Queries regarding JSPs.
It's not a secret anymore!