Mark E Hansen wrote:It's hard sometimes to see the relationship between the JSP page and the Servlet. Think of it this way - the Servlet is the place where the application logic happens. The JSP page is just the View where the user is shown the result of the Servlet doing its action(s) and can provide input/actions.
Think about the servlet as the initial point - where your web application user goes first. Your servlet can detect that the user hasn't seen the JSP page yet, so it does a forward (or whatever you want) to the JSP page - providing any data the JSP page will need (like, for example, the values that should appear in the drop down control).
You can include an Edit button on the JSP page so the user can select an entry in the drop down control and then click on Edit. What happens then? What if the Edit button actually went back to the servlet?. The servlet knows where the application is based on the parameters passed when the edit button was clicked, so it can do a forward to the "edit" JSP page with whatever information that page needs to allow the user to edit the value. When the user clicks on the Submit button on the edit page, it goes back to the servlet ...
I hope you can see where I'm going![]()
Sounds like a fun challenge. Good luck!
Mark E Hansen wrote:You're on the right track, except that you're not using a JSP page - instead you're having the Servlet emit the HTML directly. You might find it easier to use a JSP page. There are lots of easy-to-follow Servlet/JSP tutorials available so just Google that and follow one or two. You'll quickly get the idea. As far as generating the page the way you are, just mock up an HTML page and make it work the way you want, then adjust your Servlet to generate that.
The tutorials will really help because they will show you how to wire the Servlet to the JSP (or the JSP to the Servlet - depending on how you look at it).
Mark E Hansen wrote:Sure: http://www.lmgtfy.com/?q=servlet+jsp+tutorial
When the user changes the value, you're going to want to go back to the Servlet and let it handle the update of the data. It should then respond with a new page which will show the user the result of the update (perhaps it will be the same page, showing the same data with just the addition of a message at the top which says that the update was successful).
Again, have a look at the Servlet/JSP tutorials - they will help you get the hang of this Model/View/Controller design pattern and it will me much easier to then understand where you need to do which parts.
Good luck.
Paul Clapham wrote:Hopefully you get the idea now, but just in case it isn't quite clear yet, here's the most desirable way to handle things in the simplest situations:
1. Browser sends request to servlet.
2. Servlet processes request, checks validity of inputs, updates databases, puts relevant data into request scope, forwards to JSP.
3. JSP produces HTML (for the browser) including formatted version of relevant data which it found in request scope.
It's tempting to think of what the user sees in the browser after step 3 as "the JSP" but don't do that. Otherwise you find yourself saying things like "the JSP calls the servlet" and that isn't what happens at all.
(Also all of this has nothing to do with XML so I'm going to copy it into a more relevant forum.)
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|