• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

General Queries regarding JSPs.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic