• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Jstl not reflecting values back to servlet

 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


 
Ranch Hand
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...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?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic