aspose file tools*
The moose likes JSP and the fly likes Jstl not reflecting values back to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Jstl not reflecting values back to servlet" Watch "Jstl not reflecting values back to servlet" New topic
Author

Jstl not reflecting values back to servlet

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi All,
In the following jsp I have a form that where the values of the fields are reflected from beans using EL and jstl as below.


however when i try to perform operations after submitting the form in my servlet as below.
Servlet


I get the value of Code printed from the bean.getCode() statement as null. Any clue as to why this may be happening? am i missing something?




Learning and Learning!-- Java all the way!
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Vic Hood wrote:
I get the value of Code printed from the bean.getCode() statement as null. Any clue as to why this may be happening? am i missing something?

You never show where the bean object is instantiated or initialized. Do you call a setCode() somewhere in your code prior to the getCode() call or does it have some default value?


OCJP6, OCWCD5
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Mike ,
Thanks for replying! You were right about that line however the thing that baffles me is that ,when I tried to retrieve the values after they are being set in the bean it still threw a null. Here is the code that is called through InsertionDB

And the insert query fails throwing a null pointer exception
Also ive placed my bean class below for you to have a look.
File: DomainBean

Thanks for your time.
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

If an object throws returns null when you call a method (getCode), why would you expect it to return anything other than a null when you call the same method from the same object when passed as a parameter into another method?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Mike ,
I dont know if im thinking on the wrong lines here ,Im trying to pass the values that i get from the form ,into the bean (hence setting the bean variables) within the function InsertionDB, and then use these values for insertion , is my approach correct?
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

It seems like the instance variables of the DataObject are not initialized. Inside the InsertDB method, put debug statements after each assignment to see that value you are assigning to the DomainBean. I have a feeling that your statement at line 8 [domainBean.setCode(code)] is setting a null value.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Mike,
Hmm .. yes you were right .
The value of code being set is null as inferred from
File: DAO.java


The log generated is

ANy clues on how I could set the values into the bean variables ?Is it becuase im missing something in my jsp?Thanks for your time
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

How does your code handle the request parameters and attempt to save them into either the DataObject or the bean objects? Can you show your code for the doGet/doPost or handleRequest methods?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Mike,
Okay well heres my servlet code the entire doPost method


Im trying to obtain the values from the jsp, using EL and set it to the bean , so that the values are set automatically . And then i try to retreive those values through the request parameters as said above and set them into the bean . Ive pasted my jsp in the first post on this thread.I have a feeling im making a silly mistake here. Any pointers could be helpful.
Thanks.
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

So looking at your Servlet code, it looks like you are using instance variables to handle request parameters. That is a VERY bad idea because that means that those values are not thread save. If you have multiple people sending requests to the Servlet, they will overwrite each other.

On line 9 of the doPost method, you are saving the request parameter to the Servlet's instance variable named code. In the InsertionDB method, you are trying to save the DataObject's instance variable named code. Somewhere in your code, your doPost you need to set the DataObject's code variable similar to the way that you did it on line 24 of the doPost:

Minor note on that code: you should not have public instance variables. You should make those private and create public getters and setters.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

HI Mike ,
Thanks for your reply.
Just to quickly clarify your suggestion . You are saying that . I manually set the values obtained from ym servlet into the DAO like i do here

Also in addition I was against the idea of using instance variables for the fields altogether ,from my very limited knowledge of beans I was hoping that while doing something like this in my jsp

would set the values onto the bean(Ive just used one field for example sakes)
and then use the values from the form when they are submitted as they are automatically set onto the bean through set Bean and then manipulate them for further use.
But are instance variables in the servlet needed to get these values? Shouldnt they be set to the bean directly?And there would be no need of any instance variables in that case?
It would be great if anyone could correct my code as Iv e been stuck here for days now .
Im sorry if my post is confusing . its just that im relatively very new to coding and trying to get a hang of how this would work
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Vic Hood wrote:HI Mike ,
Just to quickly clarify your suggestion . You are saying that . I manually set the values obtained from ym servlet into the DAO like i do here


Yes that is something that I was suggesting, but you should do it for all the request parameters and not just those two.


Also in addition I was against the idea of using instance variables for the fields altogether ,from my very limited knowledge of beans I was hoping that while doing something like this in my jsp


You have a Servlet so there is no need to use the useBean or setProperty tags. You should set the values of your domain bean in Servlet and set it as a request attribute. i.e. request.setAttribute("domain",bean).


But are instance variables in the servlet needed to get these values? Shouldnt they be set to the bean directly?And there would be no need of any instance variables in that case?

Yes, that was what I was implying. you should be doing something like DataObject.code = request.getParameter("code");


It would be great if anyone could correct my code as Iv e been stuck here for days now .
Im sorry if my post is confusing . its just that im relatively very new to coding and trying to get a hang of how this would work

I cannot do that because this site is NotACodeMill. I would highly suggest buying the Head First Servlets and JSP book. It is well worth the cost and will vastly improve your knowledge of Servlets/JSP.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477


Thanks a lot for your reply Mike!
It helped in my solving my current problem , I wasnt setting the values obtained from the jsp into the DAO object, thanks for helping me with that.
However now when i try to insert a row , there are are two records added as in duplicate entries , any clue as to why this could be happening ?


And I wasnt asking for code , only for corrections, thats thwe way it works here
,right?And also I do have Head First Servlets and JSP , an awesome book , and am reading through it , however im being pressurised to finish this work much faster than I could progress through the book.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

...when i try to insert a row , there are are two records added as in duplicate entries , any clue as to why this could be happening ?

What gets printed (to console by your System.out.. statements) when your "InsertionDB" method executes? Are there any duplicates?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Vijitha,
Thank you fore replying and sorry for the delay ,the entire set of statements is printed twice
For example this is what happens from loading the page , to the insertion of record.
1. The dropdown content is accessed.
2.The list according to the dropdown is presented (upon selecting a value in the dropdown)
3.Upon entering values in to the fields and hitting submit , the whole page reloads ,and the insert query is executed twice (from the log) and then the whole list is redisplayed .
Any clue on where I could be going wrong?
And Ive rechecked there is only one explicit call to InsertionDB in my code so im baffled as to how its printed twice :\

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

Looks like the form get refreshes again (I haven't gone through your complete code though) unless done by the user, which results in the same request submitted again. If that's the case try using a redirect from the servlet upon a successful insertion. So that you will be redirected to a different page once the insertion happens. Sample here. (You may implement the Java stuff in your servlet)

EDIT: You may also log in your doPost() method to see whether it gets called twice...
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Vijitha,
Thanks a lot for replying . I did perform logging in the doPost (). and Using logging , I think you were right , the page refreshes again performing a double submit , I tried looking at why this was happening , but no use . Could you please provide an example of redirecting to another servlet?
EDIT: I tried doing something like this in my jsp , so that (I hoped(dont know if im right ) to work around the double submit problem.I wrote another servlet that was only responsible for listing the records and one to insert/update data.
I modified my jsp like this :
File:POC.jsp

I encapsulated the post method with a get method and used the List form to just display values and the other form to submit values for insert/update
File:ListServlet.java

And the earlier ControllerServlet as below.

Now im able to display the list by transferring control to the list servlet
However , now when i try to submit the form to perform an insert I get an error saying

What should I be doing to work around this?


Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

You should NEVER use nested forms. It is not supported by any standards and you cannot guarantee the result. I think the best course of action would be to replace your forEach loop with a custom tag. Read chapter 9 of the Head First Book. They have any example of how to custom select elements.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

...Could you please provide an example of redirecting to another servlet?

As shown in the link I provided you may use the sendRedirect() method of the response object to do this.
And also don't try to make things complex, just create two pages (one for the input and other for the display) to see whether it works in the first place.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hey everyone!
I solved the double insert issue , it was just that the submit button at the end of the page was causing it to submit again ! So I changed it to button and it worked!
However , Im trying to edit the values in the list generated below now by using the edit hyperlink
.
The jsp code of that fragment is as below.


Fragment of ControllerServlet.java

What im trying to do is get the values from the bean through obtainData(which is happening through the backend)and be able to set the values in the jsp typically here
JSP code


Any clues on how I could go about doing that?
Thanks to all of you for taking the time to help
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Your code is getting so complicated that I can't follow the logic properly anymore. It seems that your biggest problem is that you have circular dependencies between your View and Controller code. Your Controller (Servlet code) is expecting to have request parameters (It looks like it will fail to produce attributes if it will not work). Your View (JSP code) is trying to generate request parameters based on the attributes from the Controller, but those values were not assigned because the View did not provide request parameters.

Can you go through a simple Use Case. What happens when the a user tries to access your JSP/Servlet for the first time.
1) User performs a Get to access the Servlet with no request parameters
2) What data is generated when no request parameters (It looks like it returns null)
3) The JSP page tries to assign value from the attributes but those values were null
4) Then the page loads with no information

I have no clue what is going on with your obtainData method. It takes the variable objid but you do not show where it is declared or how it is set. What does the method do when a null value is passed in. In general, how does your code react if any value is null or not initalized. You also call the getDomList method twice in the same block of code, which makes no sense.

The get the data from the obtainData method is easy
 
 
subject: Jstl not reflecting values back to servlet