Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets and xml modification.

 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys had a lot of help from this site, its really great.

I'm currently quite new to java and servlets, but I have an assignment where I need to creat a JSP which invokes a servlet which picks up an xml file read all the nodes etc and then you can pick the node from a drop down list and modify the element I think ive said this correct.

Lets say for example my XML was.



Then I would want a drop down box to be populated with RegistrationNo, StudentID, Name and Fname. Then once I click on that I want it to show the current value lets say for example I picked StudentID, it would show next to it : 1110708048 but then I would like there to be a text box in which I can enter a new one and submit it.

I know how to read xml in to java and modify it etc. Its more the reading the things in to the drop down box etc.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!


Hey thanks for the reply this is kind of what I figured.

I'm getting a bit confused though and at the moment I'm kind of writing everything in the Servlet ( its getting really mixed up).

This is the code I have at the moment it collects all the details from the config file and then displays them in a dropdown menu. I really obviously want the dropdown menu in the JSP but I cant really work out how to do this.

I guess whats next is to get the dropdown box in JSP, Once thats done I need to write some code which says if X is selected display properties.getProperty(x); Which I dont think is too hard I just dont know how to get the logic for which item is selected. thanks for the help so far and any more help you guys can give this forum is great.
This is my current Servlet code.

 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).


Thanks for the reply again, I have actually done a few things you have said in regards to I have 2 seperate bits of code now. I'm struggling trying to find a good tutorial on how to wire them. I think my google skills are getting terrible. Could you possibly link me to something you think may help.

I now need to get the JSP dropdown box to populated from the servlet, once I know how to wire them I think this should be simple and just move over the while{ }. once thats done I then need to use the selected text from the JSP to update the correct fields. I have managed to work out how to get the selected drop down and get the text from the text area. So also I don't think this will be too complicated. I just need to work out how to wire the servlet to the JSP.
This is the current code I have for the servlet:


JSP Code:

 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21586
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


HAHA Nice link ;). Unfortunately I have searched that and looked at all the pages on there already and I cant really get my head round that. I have managed to create a Form with a submit button but all that I really did was: when the button is clicked it starts the servlet. So I guess im kind of on the right track linking the JSP to servlet just need to try and find out how to use them properly.

Thanks and i'll try and go through them again see if I can work my head round it.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)


Yes sorry about saying xml I originally thought I need to get the config file in to xml format but apparantly that is not the case and it supports reading in java.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had some luck this morning, Finally got it working . Theres a few things I want to change but I'm not sure how. Firstly I am using radio buttons as post doesnt support dropdown or so thats what it seems, so I would like to find an alternative to use a list instead of radio buttons. Then I want to create another servlet in which this list is read from the servlet. any help would be great I have attached my code soo far.


JSP PAGE:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic