This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Ok, after being rode hard and put away wet out here on the java ranch I've decided to take a stab at this servlet dish. I have the bean, and I've started to write a servlet.
I need to do some thinking out loud, and if any of you guys hear this and I'm wrong or missing something I'd appreciate a correction. Thanks in advance
Now, when I submit my form I will want to do a Method=Post. Right now i have it do action=linkage.jsp which I won't want anymore. I will want the servlet to handle that with the doPost. In the doPost I will basically write all the code that I was doing in the linkage.jsp (pulling form data from the bean and processing to get me to the correct output). My linkage page was set up to redirect me to the correct search result page since the tabular data displayed in each case will be set up depending on the search criteria entered. I think that I can now do all of this from within the servlet, but I've yet to fully wrap my head around how this will happen.
My big question is this. How do I use the bean info from within the servlet? Since it is a java class and not a jsp i won't want to do these imports anymore...
Originally posted by Bryce Martin: In the doPost I will basically write all the code that I was doing in the linkage.jsp (pulling form data from the bean and processing to get me to the correct output).
There's no automatic bean with a servlet. The submitted data is directly available on the request, which you can retrieve with the request.getParameter() (or its related methods)
In Model 1, you generally used a bean to collect the params for convenience. With a servlet, you are at a lower level can can deal with the data directly, or collect it into a bean if you'd like. But the bean is no longer necessary.
Again, you don't. If you have a reason to collect the params into a bean, just write the code to do it. Or you could deal with the param data directly. Or obtain a Map of all the params. Or just about anything else you;d like to do with the data.
Don't let the way that you had to do things in a JSP constrain your thinking. Think, rather, of what you need to do with the data.
P.S. If you are planning to make the jump to Model 2, you might want to check out the link in my sig to Front Man -- my implementation of a Front Controller pattern.
Joined: Nov 19, 2007
Yes indeed. I had downloaded the SimpleMVC example from the code barn. And that has gotten me to the point where I put the data into my bean like such..
But you are saying that I don't even need the bean. I can access all the data by just saying request.getParameter, which is good. I like that.
So now I'm thinking that I need to take the processing code out of the old linkage.jsp page and move that logic into my servlet. In that logic I create my SQL string that I need for when I connect to the DB.
Should I have a separate servlet to handle my database call? Right now as it stands my connection functions are in a .jsp that i include to give me access to the functions. I could, I suppose, pull those functions into my servlet. So much to rethink right now, and I only have a half hour left... looks like I'll be doing lots of pondering until tomorrow...
Originally posted by Bryce Martin: Should I have a separate servlet to handle my database call?
I wouldn't handle database calls from the servlet.
Ideally, your model objects should be able to work independently from the servlets and JSPs. For me, making sure they do, greatly speeds up, both the development and, later, testing and debugging of these classes.
They speed up development becuase I can give them a main method and make sure the database access and business logic is right from the command line before I start with the servlets and JSPs. It will pay dividends later on too, because you it will be really easy to write tests that can be run from jUnit without having to simulate user interaction from a browser.
A simple rule of thumb (if you don't have separate DAO layer) is not to import javax.servlet... and java.sql (or javax.sql) from within the same classes.
For instance, suppose you need a method that returns a list of your myclass objects. If your model object (call it "myModel") was independent of the servlet and JSP stuff, you could give it a main method that looked something like:
Now, you can hammer out all of your business logic right at the command line (or IDE) before worrying about how to integrate it with the web stuff.
If you have a test database with mock data, you can write tests that call this method right from your build script to test all of this logic every time you compile your app. Lastly (and this has been a big time saver for me) when the pages stop working from the browser, as they inevitably will), you can quickly determine whether a change in the database tier or in the web tier is causing the problem. [ November 20, 2007: Message edited by: Ben Souther ]
Right now I have a jsp that I <%@include file="dbcall.jsp"%>
Here is my basic setup I'm looking at right now...
Selection.jsp -> FormMangaer(servlet)
This servlet, based on the search criteria entered in the form on Selection.jsp, should create a string that will be my SQL string that is passed to my dbcall to get my resultset.
So my question would be, once I have my string for my SQL what is my next step? Where do I go? I need to make a call out to the db, get my resultset, and put the data into a table that is unique to the search criteria. I will have 8 different table formats based on what is known and not known at the time of the search. I know I can use a servlet and several functions to accomplish this, I just need this middle step(i think, unless I'm missing something).
Please bear with me, i'm really trying to break my bad training here where I work. Thanks for everything so far.