Reputation: 765
I have a Servlet called Menu that generates some html and a dynamic menu, I mean by dynamic that the links in the menu change each time the user select a table in a frame, here is a quick exemple:
Browse?table=student
Search?table=student
Browse?table=professor
Search?table=professor
I included this Servlet in the beginning of most of my servlets in order to optimise modifications, I used the following code:
RequestDispatcher dispatcher = request.getRequestDispatcher("/Menu?table="+tableName);
dispatcher.include(request, response);
but when I try to send a post request to Servlet that contain the previous code, I receive an error saying: HTTP Status 405 - HTTP method POST is not supported by this URL
I'm using NetBeans 6.9.1 and TomCat 6.0.26 and Java version 1.6
So is there any way to include my Servlet Menu in the POST method of another Servlet? or is there another solution to have the dynamic menu other than a Servlet?
To explain more to you guys, here is the code of my Servlet:
public class ExecuteAnySql extends HttpServlet {
//No problem here
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//code
//include the Menu Servlet
//code
}
//this does not work when I include the Menu Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//code
//include the Menu Servlet
//code
}
}
Thanks guys, I think that I found a solution,
The solution you provide me will work only if I have 1 Servlet, but I have too many and I want that they share the same menu.
So all I have to do is to make a static method that takes the table name as a parameter and generate the html code. If I proceed like this, I can call my method from all of my Servlets.
Upvotes: 1
Views: 1478
Reputation: 103135
You need to provide POST support in the servlet. The servlet template that Netbeans uses by default does this for you. But simply provide a single method that does the work of the servlet:
public void doEverything(HttpServletRequest request, HttpServletResponse response){
//all your servlet code here
}
Then in the doPost you can simply call this method and in your doGet you can call this method like this:
doEverything(request, response);
This way the single servlet handles both GET and POST.
So expanding on your posted code:
public class Menu extends HttpServlet {
//No problem here
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doEverything(request, response);
}
//this does not work when I include the Menu Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doEverything(request, response);
}
protected void doEverything(HttpServletRequest request, HttpServletResponse response){
//do actual menu code
//forward to the requested servlet
}
}
Upvotes: 2
Reputation: 38300
Just an expansion of Vincent Ramdhanie's answer:
In the servlet that you are including you need to implement the doPost method. I've often seen something like this:
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
{
doGetAndPost(request, response);
}
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
{
doGetAndPost(request, response);
}
protected void doGetAndPost(final HttpServletRequest request, final HttpServletResponse response)
{
... process requests here.
}
Upvotes: 0