Forums Register Login

Problem with doGet and doPost methods

+Pie Number of slices to send: Send
Hello,
I wrote a simple thing.Taking two textbox values and dispalying them .Any code i write in doget() and doPost() dont work,but in the processRequest() method it works.I can see the values in the url when i use GET.


+Pie Number of slices to send: Send
processRequest is not part of the Servlet API; what API are you using?
+Pie Number of slices to send: Send
I think Servlet API.I dont know much about this.After seeing your comment ,i searched for what API is used in servlets.



i used these packages

javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

+Pie Number of slices to send: Send
The Servlet API has no such method; it must be in your code. Why don't you post what you have, and we may be able to point out what's going wrong.
+Pie Number of slices to send: Send
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@WebServlet(name = "validate", urlPatterns = {"/validate"})
public class validate extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

out.println("<html>");
out.println("<head>");
out.println("<title>Servlet validate</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet validate at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");

} finally {
out.close();
}
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

processRequest(request, response);

}

public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
+Pie Number of slices to send: Send
Please UseCodeTags when posting code to the forums.

As processRequest() is a method you wrote in your code, how were we supposed to know anything about it?
+Pie Number of slices to send: Send
Hi..Lifecycle of a servlet has 3 methods
(1)init()
(2)service()
(3)destroy()
Whatever main logic we have in servlet we write it in the service() method..........Now i think you are using the Netbeans IDE for writing servlets program..........Now in Netbeans IDE there is no service() method but an processRequest() method..............i.e. service() is equal to processRequest()...............
Now whatever method you have chosen to pass data i.e. Get or Post from these method the service() method is called............i.e. in Netbeans from the from the doGet() method and doPost() you can see a call given to processRequest() method (this you will find at the bottom of the code, while creating servlets)...............
You also said that when i write code in doGet() or doPost () it doesn't executes then you might be doing some mistake because i have tried this and it works
+Pie Number of slices to send: Send
You are declaring the object "out" of PrintWriter 2 times once in processRequest() and once in doGet().......check it out
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:Please UseCodeTags when posting code to the forums.

As processRequest() is a method you wrote in your code, how were we supposed to know anything about it?




sorry about that i changed it
+Pie Number of slices to send: Send
 

Rameshwar Soni wrote:You are declaring the object "out" of PrintWriter 2 times once in processRequest() and once in doGet().......check it out




its working .i removed the PrintWriter in processRequest.


Thank you Rameshwar Soni,Bear Bibeault ,Ulf Dittmer
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6420 times.
Similar Threads
POST or GET
request.getParameter is returning null value
doGet and doPost
doPost and doGet
doPost and doGet
Thread Boost feature
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:42:18.