Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving functionality from JSP to Servlet without posting form  RSS feed

 
M Shiner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

First off hello! I've been a fan of these forums for a while and I thought today I will finally join the community!

And what better introduction than to ask for your expertise!

Bit of background, I've been coding Java as.. I would say early-intermediate and I'm doing what I can do read up further on design patterns, structuring but I've recently moved to web development with Java on a Tomcat 7 server and I'm a little bit out my depth.

So I've taken over a web application which is pretty big but I'm going to focus on a small segment here.
I have a JSP page called View.jsp which is half scriptlet which gathers information about a person from a database then displays the data in table.

So we have:


Now I don't like so much Java logic being placed directly in the JSP.. It should belong in a servlet but it's functional so fine.

My problem is: It's my task to create 4 new buttons that provide functionality (Create new subordinate, delete, etc) which, as it's been left have the link:
"
These jsp files are purely Java! So I really want to move to a servlet but they're passed JSP variables.
I thought about turning the table into a form and using these buttons to POST to the servlet but <%= %> JSP variables are filled in dynamically and I cannot post them.

I came across a method of creating input fields which are hidden in order to post that data.. But I was hoping something.. cleaner.
The trouble is I can't change too much (especially the View.jsp) because it'll snowball and before I know it i'll have to change half the application.

Can anyone guide me to a way I can create the functionality for the 4 buttons which post to a servlet rather than a JSP page?

I hope I've explained myself clearly enough.
Thanks guys,

Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Refactoring Java code out of a JSP is a good thing to do.

Mike Js wrote:So I really want to move to a servlet but they're passed JSP variables.
I thought about turning the table into a form and using these buttons to POST to the servlet but <%= %> JSP variables are filled in dynamically and I cannot post them.

But I don't know what you are saying here. This is the part you likely need to expand upon.
 
M Shiner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

I mean, the data is put into a table with JSP variables like : <%= variable %> but it's these variables I need to pass to the servlet.

My first thought was too try and post these variables like so:



Now my servlet displays id as null. I know exactly why but I need to find a way for getting 'id' from a variable in JSP to the servlet without posting from a form. I should probably add javascript/ajax functions aren't really an ideal solutions.. As you can possibly tell, this is a legacy application. Maybe as some point though!

I hope this is a better explanation.

Many thanks,

EDIT:

To add more context to my question, I have managed to achieve what I wanted to do. By adding (to the html form code above) a hidden input, I can pass the variable to the servlet.


My servlet now displays the ID.

So I guess my question is; is there a better way without hidden inputs?

Thanks,

Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. Only active form input elements are passed as part of a submit.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Shiner wrote:My servlet now displays the ID.


Your servlet shouldn't be displaying anything (unless I've misinterpreted that statement, and you meant logging?).
It should be handing off to a JSP to do the display.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi M Shiner,
It is high time to move the Java code from JSP to a servlet.
Otherwise maintaining the code will become very difficult and finding a bug will be more difficult (personal experience).
M Shiner wrote:The trouble is I can't change too much (especially the View.jsp) because it'll snowball and before I know it i'll have to change half the application.

You can take 1 JSP at a time. Start doing with the JSPs in which you are working now.

IMO, Programming should not be done just to extend the functionality by adding lines of code to the existing code.
While analysing/writing code, you should think what better you can do with this code so that you can easily maintain/understand this code in future, more importantly,
what you can do so that other programmers will be benefited in the future who come across this code.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!