This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a servlet to write some information to a mySQL database. I get my connection to the database in the init() method of the servlet. So now do I write my information to the database in the doPost method? What I did was send an Object which is a Vector to my Servlet. In the doPost method now, I recieve that object with ObjectInputStream in = new ObjectInputStream(request.getInputStream()); And then I do: Vector v = (Vector)in.readObject(); And I loop through the vector and write to the database. However, I am not getting any error messages from my Exceptions but it is not writing to the database either. And I don't know how to get my servlet to write to a terminal so I can debug a little better. Basically I am wondering if I am overiding the correct method to write information. I am not sending anything back to the client. Thanks.
Gregg How do you send the vector to the servlet? Is it submitted by a JSP or another servlet? Or are you chaining servlets together and passing requests? Or does this just run when the servlet is instantiated? IF it from a form on a web page/jsp/servlet then whatever method is specified in the HTML form tag is the one to use in your servlet to handle the request. the same if you have passed a request to another servlet. If this is somthing that happens when the servlet is intialized then you would just leave it in the init method. Does that help? If not post some code and maybe I'll be able to follow exactly what you're doing - I'm a little slow sometimes
Yeah, that is beginning to help. It's actually a stand alone application that is accessing a servlet directly. Below is the code in my Client application that connects to the servlet:
I guess my question now is if I do all my work in the init() method, how do I get the Object? In the doPost, doGet() methods, I know to use response.getInputStream(). But in the init() method I don't know how to do it. I think my problem is the doGet() or doPost() methods are never really doing anything because the request to the servlet is not from an HTML page? I don't know. Thanks for you help Dave.
An interesting addition to my problem. In my client code, if I have the line: ObjectInputStream in = new ObjectInputStream(con.getInputStream()); and in my servlet code in the doGet() method I have System.out.println("Inside Get") and I run my client, I print the "Inside Get". However if I have in my Client code: ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream()); And then respectivly in my servlet code in my doPost() method I have System.out.println("Inside Post"); Then I run my client, I get nothing printed. So for some reason my doPost method is not getting called and I don't know why. Hope someone can shed some light.
Joined: May 10, 2001
Gregg doGet is the default method called in a servelt if one is not specified (ie in an html form). I've never done things the way you are but it sounds like you're going to want to put your code in the doGet method. You cant do it in the init method because that'll only be done once. You could also just create your own servlet class by subclassing GenericServlet and then put your code in the service method implementation you create. If you're not using any of the HTTP stuff then you dont really need and HTTP servlet. hope that helps
Ok, I figured it out, I think. Someone can correct me if I am wrong, but this works. Appearntly, in order for a servlet to work correctly, the client must open not only an outputStream to send something to the servlet, but an inputStream to recieve the response. How I found this out was that if I just opened an InputStream on the client side and not an outputStream then my init method (the first time I ran it) and whatever method (doPost() doGet()) was called. However, if I only opened an outputStream to the Servlet from the client, my methods were never called inside the Servlet (other than init() the first time). So now I got everthing working just fine. Dave, thanks for all your help. And I am using the GenericServlet class like you suggested and it works perfectly. Thanks again.