Garann, Depends somewhat on your architecture. Are you running your database on a different server than your jsp/web application?
I always recommend the Model->View->Controller (MVC or Model 2) architecture for true reusability. Although I have written my controller servlet (MVC) in the past for small projects, I recommend using the Jakarta Struts library vs writing your own. But the learning curve is a little high, so only take this approach if you have sufficient time. If you need to stick with Model 1 architecture because of time/learning curve constraints (which is where the logic is included in the jsp with HTML code, no separation of logic from the view. Same architecture that your ASP's are in), then move your "data" needs into Java class beans, and skip the "controller servlet" concept. Please keep in mind that there are two types of Java class beans (at least in my mind, as the term "bean" is often used interchangeably). Those that include a visual component, and run in a container (also referred to as applets), and those that have no visual component/interface, and do NOT have a constructor that takes parameters. These are the beans that I am referring to. Keep in mind that the difference between a Java class, and a Java "class" bean that is used with JSP's is that a Java "class" bean cannot have a constructor that takes parameters. Any data that needs to be stored and /or passed from one JSP page to another should be maintained in it's own Java class bean. What data you choose to "contain" in which "bean" is up to you, but many people will follow their database schema as a guide to designing their classes. You use the "set" and "get" properties of your bean to pass your data into HTML code for display purposes, or to other beans/servlets for logic purposes. You can then "store" your beans in the session object and retrieve them from the session object. This is how you pass beans from one JSP/serlvet to another. Since JSP's are a short hand way of writing servlets, you can write "pure logic" JSP's with no HTML to try and separate your view from your "model" and data. Just keep in mind that using this architecture does not allow you to declare other methods easily (as you have already discovered). But for a small project, and for someone that is just starting out learning JSP's, this is not a bad approach. As your skill level grows, you can then move on to pure servlets, and MVC/struts. As an aside, I am glad that you have an OO background. I have run into so many young people who are VB/ASP programmers only, who have been thrown into JSP/Java development with no training, no mentoring, and no background in OOD via C++ or Java. I don't think very much of any company or management team that does that to their employees. It's a setup for frustration and failure on the part of the programmers that are placed in that position, and is not a positive way to introduce anyone to a new language. Celeste -----Original Message----- From: Means, Garann R. [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 23, 2002 2:22 PM To: [EMAIL PROTECTED] Subject: Re: Where to put methods for a jsp page? I apologize, I am dancing around my main questions here. What would be best - to try and work this all from the jsp page (sounds like no, because I do need multiple people to use this application at once), or to put it in a separate servlet? Or a Java Bean? I understand what people have said about how to use the methods inside the page, but my question has become more philosophical at this point. My understanding is that functionality reused within a page should be a servlet, classes reused throughout the app should be Java Beans, and things called by multiple applications would be EJB's. Is that right? At any rate, thank you all for your careful explanations. Garann PS - Celeste, thank you for pointing out that this is still OOP. I'm not a VB programmer, but I was approaching my changeovers as a find-and-replace kind of thing, not really considering that these are STILL Java programs. Keeping that in mind is going to help me a lot in learning jsp. -----Original Message----- From: Borislav Iordanov [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 23, 2002 11:32 AM To: [EMAIL PROTECTED] Subject: Re: Where to put methods for a jsp page? Hi Garann, A JSP page is first translated to a Java class, a servlet, then compiled and executed on each request. All code outside of your functions is placed in a single method of the Java servlet and that method has the 'request', 'response' etc... "implicit" object declared as local variables. That is way they are available in the JSP page. However, when you declare other methods in the JSP, they don't have those local variables and that is way it does not compile. You might consider passing a PageContext or a HttpServletRequest parameter to your functions, or declaring a member variable like this: <%! HttpServletRequest currentRequest; %> <% currentRequest = request; %> However, the latter method is not suggested as member variables prevent the JSP container from reusing the same Java object when serving several requests at a time. Hope this help. --- "Means, Garann R." <[EMAIL PROTECTED]> wrote: > Hi, > > I'm converting a bunch of asp pages to jsp. The asp > pages had methods > running on the server side, so I've left them there > in the jsp. When I try > to compile the jsp, I get errors telling me the > compiler doesn't know > "request.getParameter", but only within the methods. > I'll post the code, and > maybe someone can give me feedback? > > <!-- top of page (there are some declarations up > here, superfluous for these > methods) --> > <%! public String mkDate() { > String sHourHTML = ""; > String sMinHTML = ""; > String sHTML = ""; > > sHTML = "<select > name='selCollHour'>"; > > // ... > // ... > // ... this all compiles fine, and > doesn't use > request.getParameter > > return sHTML; > } %> > > <%! public String redChem(String root) { > float usrIn = 0; > float lBnd = 0; > float uBnd = 0; > String sRed = ""; > > // these next three lines are > throwing the errors > usrIn = > Float.parseFloat(request.getParameter("txtArea" + > root)); > lBnd = > Float.parseFloat(request.getParameter("hdnLowBnd" + > root)); > uBnd = > Float.parseFloat(request.getParameter("hdnUpBnd" + > root)); > > if (usrIn != 0) { > if ((usrIn > uBnd) || (usrIn > < lBnd)) { > sRed = > "style='color:red'"; > } > else { > sRed = ""; > } > } > return sRed; > } %> > > > Thanks, > Garann Rose Means > ITAS1 > WA State Dept. of Corrections > > =========================================================================== > To unsubscribe: mailto [EMAIL PROTECTED] with > body: "signoff JSP-INTEREST". > For digest: mailto [EMAIL PROTECTED] with body: > "set JSP-INTEREST DIGEST". > Some relevant FAQs on JSP/Servlets can be found at: > > http://archives.java.sun.com/jsp-interest.html > http://java.sun.com/products/jsp/faq.html > http://www.esperanto.org.nz/jsp/jspfaq.jsp > http://www.jguru.com/faq/index.jsp > http://www.jspinsider.com __________________________________________________ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/ =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com
