Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General Queries regarding JSPs.

 
Manish Vyas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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?
 
arnel nicolas
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • 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:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic