aspose file tools*
The moose likes JSP and the fly likes can  i write java code in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "can  i write java code in jsp" Watch "can  i write java code in jsp" New topic
Author

can i write java code in jsp

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hey everybody suggested me to not to write java code in jsp using scriptlets
but here i am doing one simple webapplication which displays 5 records out of 100 records when ever user press on next button.
so fo this i am writing the java code in jsp.for better performance..
if i use this java code in servlet i can eliminate java code inside jsp file.but accoring to performance i think it is not so good because if the use clicks on next button the control goes to servlets and gets the first 5 records and so on...
which is the better way ??
any experienced developer can give be the best suggestion..
thanks in advance

cinux


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
It seems that your applicaiton is really a small one. So please dont hesitate to write code on jsp.

Many people may tell that, it's real sin , if you write code on jsp's rather you must use any latest framework.
Dont mind them.

Coming to performance, if your problem will solve with a little code on jsp's, then do it. Otherwise you will waste time in giving control to servlet and back.




SCJP 1.4, Brainbench
LinkedIn - Blog
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
but i think jsp also becomes a servlet when we send a request to the jsp file.
what is the difference if we wirte the java code in a servlet instead of writing everything in jsp file???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Personally, I always structure my applications properly regardless of their size. All too often what starts as a "small project" snowballs into something bigger, and if you've started off with a poor structure it's unlikely to scale well. It's also just good discipline and practice, and a good habit to get into.

Performance is not a factor. It will have no discerable performance impact to factor processing code into a servlet which dispatches to a JSP over just plunking the code down into the page.

It is also incorrect to think that in order to write scriptless JSP pages that you have to use "any latest framework". You don't. I don't.

And finally, the advice of anyone who says "dont listen to other people's advice" is the first that I would regard with caution.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Yeah actually as bear mentioned. Please use the standard practices. They are standards because they have been tested through ages.

Secondly it won't effect the performance as you think. Don't measure performance like if it's in the same JSP it would execute faster becasue

1. The JSPs in any case would be translated to servlets.
2. You would not be able to reuse the code / or would be difficult to reuse a code written in JSP.
3. Debugging and maintaining code in a JSP is very .
4. Moreover in case you get into this habbit and you code like this it would be dificult to chnage jobs and probably hold on to the job present even.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Bear Bibeault:
Personally, I always structure my applications properly regardless of their size. All too often what starts as a "small project" snowballs into something bigger, and if you've started off with a poor structure it's unlikely to scale well. It's also just good discipline and practice, and a good habit to get into.

Performance is not a factor. It will have no discerable performance impact to factor processing code into a servlet which dispatches to a JSP over just plunking the code down into the page.

It is also incorrect to think that in order to write scriptless JSP pages that you have to use "any latest framework". You don't. I don't.

And finally, the advice of anyone who says "dont listen to other people's advice" is the first that I would regard with caution.


i dont know any of the frameworks available in the market now..
the only way is i have to follow MVC2 architecture that is jsp,servlet ,bean..
so for this ishoudl definitely use... java code in my jsp files.....
so what is ur suggestion now
waiting for ur reply.
cinux
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
the only way is i have to follow MVC2 architecture that is jsp,servlet ,bean.. so for this ishoudl definitely use... java code in my jsp files.....

You don't need to use Java scriptlets in JSP pages, no matter whether you use MVC1, or MVC2, or neither of them. Check out JSTL and the expression language (EL).


Ping & DNS - my free Android networking tools app
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Can someone explain MVC1 and MVC2. where does struts figure in?


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Some articles:
http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html

Struts is a framework that employs the MVC/model2 architecture. Basically it helps you follow some decent design principles with your web application.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Read this article.
Hiren Pathak
Greenhorn

Joined: Mar 24, 2003
Posts: 21
Originally posted by saikrishna cinux:
hey everybody suggested me to not to write java code in jsp using scriptlets
but here i am doing one simple webapplication which displays 5 records out of 100 records when ever user press on next button.
so fo this i am writing the java code in jsp.for better performance..
if i use this java code in servlet i can eliminate java code inside jsp file.but accoring to performance i think it is not so good because if the use clicks on next button the control goes to servlets and gets the first 5 records and so on...
which is the better way ??
any experienced developer can give be the best suggestion..
thanks in advance

cinux



There is a tag library out there which does the pagination. and its free.

the Taglib is displayTag. Try it.. you donnt even have to give control back to your Servlet.


SCJP, SCWCD
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by saikrishna cinux:
hey everybody suggested me to not to write java code in jsp using scriptlets
but here i am doing one simple webapplication which displays 5 records out of 100 records when ever user press on next button.
so fo this i am writing the java code in jsp.for better performance..
if i use this java code in servlet i can eliminate java code inside jsp file.but accoring to performance i think it is not so good because if the use clicks on next button the control goes to servlets and gets the first 5 records and so on...
which is the better way ??
any experienced developer can give be the best suggestion..


No performance issues here. Its just about the readablity, easeness, and the use of right technology.

Recommendations:

- No Java in JSP
- No DB operation stuff in JSP

For pagination you can search the same forum or Servlet forum with the word pagination and you would get lot of threads discussing that.
 
jQuery in Action, 2nd edition
 
subject: can i write java code in jsp