so what i am trying to do is , i have 2 html pages - 1 allows to search users in DB by email and 1 by ID. So i have 2 forms on each to let user insert a number and a String.Now i want the request made on each of this 2 pages to go to the same servlet for different processing , is that possible?
This is the servlet that redirects to 1 of the 2 html pages with the search forms, namely searchbyid.html and searchbyemail.html.
public class SearchServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String value = request.getParameter("searchType");
RequestDispatcher view;
if(value.equals("ById")){
view = request.getRequestDispatcher("searchbyid.html");
view.forward(request, response);
}else{
view = request.getRequestDispatcher("searchbyemail.html");
view.forward(request, response);
}
Utils.searchBy(request, response);
}
Here are my 2 forms: The search by id form is on "searchbyid.html".
<div id="container">
<form method="POST" action="ResultServlet">
<p>
<label for="searchId">Please enter user id</label>
<input id="searchId" name="searchId" type="text">
</p>
<p>
<input id="submitButton" type="SUBMIT">
</p>
</form>
</div>
Now the second form, searching by email is on searchbyemail.html.
<div id="container">
<form method="POST" action="ResultServlet">
<p>
<label for="searchEmail">Please enter user E-mail:</label>
<input id="searchEmail" name="searchEmail" type="text">
</p>
<p>
<input id="submitButton" type="SUBMIT">
</p>
</form>
</div>
The fix for my problem would be to create 2 servlets for each form, but i do not want to do that.I want to create a single servlet wich will handle both forms even if they come from 2 different html pages. Now my questions are:
1.Can a servlet only have 1 doGet and 1 doPost?is overloading possible?
2.Should i use GET or POST for the forms and why?
3.Should i use GET method for one of the forms and POST for the other one in the same servlet?
4.Is there a way to make a doXXX method accept different requests?
5.What would be the most elegant solution to make 1 single servlet manage both forms without using any framework - just servlets and html/jsp?