[Logo]
Forums Register Login
Problem with doGet and doPost methods
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.


processRequest is not part of the Servlet API; what API are you using?
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;

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.
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>
}
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?
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
You are declaring the object "out" of PrintWriter 2 times once in processRequest() and once in doGet().......check it out
 

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
 

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
Bring out your dead! Or a tiny ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter
https://coderanch.com/t/690611/ScroogeXHTML-RTF-HTML-XHTML-converter


This thread has been viewed 4732 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 21, 2018 18:11:04.