Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing values from Servlet to a java class  RSS feed

 
George Ma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am an absolute beginner to Java.
I am trying to code a simple servlet.
My screen will accept five values (AcctNo, short name, Status, LegalTitle and Open Date) and I need to update it in a DB2 table using JDBC.
I have a Java Class (UpdateData.java) which opens the JDBC connection and executes the Update query.

I have a code as below.

Servlet
--------



UpdateData.java
-------------------


----------------------------

This works fine. But I am sure I am not using the power of Java. I am sending each column value as arguments to the method.
Can I convert these 5 values I get from the screen as an object and pass that object to UpdateData.java and do the update.
It would be great if you can provide the code snippet for the same.

Any advice is appreciated

Thanks and Regards
George

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

Sure, just create a POJO (plain old Java object) that has the values you need as members. It'd be best to format it as a bean (with private members and getters/setters) as a convention.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, as a newcomer to Java it's important to foster good habits early. Learn and follow the Java naming conventions. For example, variables never start with a capital letter.

So, shortName, not ShortName.

And u is not an acceptable variable name in this context. Use real words.

And, pay close attention to indentation. Wonky indentation can make the structure of the code hard to inspect. Proper indentation is just as important as the rest of the conventions and is a must.

And (last one for now), never omit the braces ({ and}) from if statements and other block statements. Your code as written has a severe bug in this respect.
 
George Ma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Can you please provide a sample code to create the object.
What changes need to be made in the Servlet and Java class.

As I mentioned, I have not done it before.

Thanks and Regards
George
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give it a try. Start by creating a Java class that has one private member for the account number. Then create a getter and a setter for the value. Don't worry about getting it perfect on the first try, just try, and we'll help you along.
 
George Ma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did the following

Bean class
-----------



Servlet
-------



UpdateData.java
---------------



When I debug, I could see the variables have null values in my UdateData.java class.
Will it retain the values I am setting. Or I need to do something to pass the object specifically to UpdateData.java
 
George Ma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method in UpdateData.java has no arguements

public void updateData() {
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pay attention to your naming. Variables start with a lowercase letter.

Now you need to pass the bean, rather than the individual values to the method.
 
George Ma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did below and it worked

Servlet
------


UpdateData.java
---------------



Is this the correct way to code this. Thanks for your help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is much better to collect the values into a class than end up with a bunch of method parameters. What if the form gathered a couple of dozen values? How awful would a method with 27 parameters be?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, be sure to continue to pay mind to proper naming and indentation conventions.
 
George Ma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your updateData method look like now?
I'm just asking because the code where you show the bean class seems to imply you are populating a load of local String variables.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
Why are you using String as the type for dates? Because you are not validating anything, you might have a String of the wrong format passed to that method and not be able to do anything about it. Change the datatype for the date to a suitable date class; I would try LocalDate, but other classes might be more suitable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!