This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Problem with doGet and doPost methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with doGet and doPost methods " Watch "Problem with doGet and doPost methods " New topic
Author

Problem with doGet and doPost methods

nvamsi naki
Greenhorn

Joined: Jun 09, 2011
Posts: 8
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.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41060
    
  43
processRequest is not part of the Servlet API; what API are you using?


Ping & DNS - my free Android networking tools app
nvamsi naki
Greenhorn

Joined: Jun 09, 2011
Posts: 8
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;

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41060
    
  43
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.
nvamsi naki
Greenhorn

Joined: Jun 09, 2011
Posts: 8
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>
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
You are declaring the object "out" of PrintWriter 2 times once in processRequest() and once in doGet().......check it out
nvamsi naki
Greenhorn

Joined: Jun 09, 2011
Posts: 8
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
nvamsi naki
Greenhorn

Joined: Jun 09, 2011
Posts: 8
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with doGet and doPost methods
 
Similar Threads
doPost and doGet
POST or GET
doGet and doPost
request.getParameter is returning null value
doPost and doGet