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

Servlet doPost method is not triggering while posting form data to SQL Database using servlet  RSS feed

 
Bhawna Pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a newbie in Java. I have a sample dynamic web project in eclipse. I am struggling with the servlet code. Its' not caling doPost() method, as form action is post, while I am able to print something in init() method. Here is my code. Please help-

-------------------------------------------------------------------
My servlet code- Register.java


 
Swastik Dey
Rancher
Posts: 1784
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't override service method.  It's implicitly called by doPost or doGet.  Remove that service method from the code and see how it behaves.
 
Bhawna Pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swastik,

Thanks for your quick response. If you see, I have not overridden the service() method.
There is nothing inside service() method.

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
 
Swastik Dey
Rancher
Posts: 1784
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method is overridden  with an empty body.  Body is empty but method is being called.
 
Bhawna Pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried deleting the method, and yes it triggered doPost() method. But now am getting this error-

java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=my_DB

I have already added the sqljdbc4.jar file to build path of my application. Still I am getting the same error.
 
Tim Moores
Saloon Keeper
Posts: 3755
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The build path and the runtime path are different concepts. The driver jar file should be in the WEB-INF/lib directory.
 
Tim Moores
Saloon Keeper
Posts: 3755
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:Don't override service method.  It's implicitly called by doPost or doGet.

Actually, the service method dispatches to doGet or doPost (or other methods, depending on the HTTP method).
 
Bhawna Pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I copied the jar sqljdbc4.jar file into WEB-INF\lib folder of my application. but still the error is same-
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=my_DB

Could you please check my connection string as well as I am not able to figure out the root cause of this error.
 
Swastik Dey
Rancher
Posts: 1784
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think


should be

 
Tim Moores
Saloon Keeper
Posts: 3755
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't look like the right URL. Search for "jdbc url sqlserver" for details.

Edit: what he said :-)
 
Bhawna Pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Yes the URL was not correct. Also my insert query was not correctly written. I am able to insert data into my database.
Thank you so much all for your help.

Issue with these line-
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=my_DB";

PreparedStatement pst =(PreparedStatement) conn.prepareStatement("insert into 'my_DB'.
        'Employee_Data1'(empId,empName,office,mobile,manager) values(?,?,?,?,?,?,?,?,?)");

It should be-

String url="jdbc:sqlserver://localhost:1433;DatabaseName=my_DB";

PreparedStatement pst =(PreparedStatement) conn.prepareStatement("insert into Employee_Data1 (empId,empName,office,mobile,manager) values(?,?,?,?,?)");

Issue resolved. Thanks once again.
 
Swastik Dey
Rancher
Posts: 1784
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good.
 
Swastik Dey
Rancher
Posts: 1784
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although a small thought here.  Putting the PreparedStatement inside doPost means every time it's creating a new object, so as per my understanding it's losing the main functionality of pre-compilation.  Will it good if the PreparedStatement object is created inside init() and in doPost just setter methods are called.  This way code will be much more optimized.  Please correct me if I am wrong. 
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66041
140
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are wrong. Database calls should be made as needed. The init method is not the place for database activities.
 
Paul Clapham
Sheriff
Posts: 22266
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Optimized? Well, avoiding the creation of a single object is a wasted optimization. Saving a few nanoseconds isn't worth the trouble of having the code in the wrong place. The other more serious issue is that if you only have one PreparedStatement object which belongs to the servlet, then when you have two simultaneous accesses to the servlet, you're going to have race conditions.

However... one of the features of PreparedStatement is that the database will "prepare" it the first time it's used, something like compiling it. There's an optimization taking place there. So one might think that recreating a PreparedStatement for each use would lose that optimization. However databases tend to have parameters which allow them to store the optimized query -- you might have to set that parameter in the database's configuration or it might even be the default.
 
Evil is afoot. But this tiny ad is just an ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!