0

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?

helloApp
  • 449
  • 1
  • 4
  • 21
  • what specific issue you facing ? – Sabir Khan Jan 22 '20 at 13:54
  • dude, is my post not specific enough? – helloApp Jan 22 '20 at 13:56
  • yes..its not specific enough ..you are saying what you are trying to do and not what issues you facing when you are trying to do that. This is a place to find answers to your issues & not to perform stunts. – Sabir Khan Jan 22 '20 at 14:02
  • my issue is that i don't know how to do one single servlet to handle 2 requests coming from 2 different html pages , how was that not clear from my post? – helloApp Jan 22 '20 at 14:07
  • you are showing a specific code in your question , what issues you face when you run that code ? Does that make sense ? Any exception , any errors after your trials or trying to solve issues beforehand without executing code? – Sabir Khan Jan 22 '20 at 14:12
  • what issues i face?i don't know how to write a servlet wich accepts 2 different requests from 2 different html forms.how is this so hard to understand?How can i run code wich i have not written ?I am asking for help to write that code.Is this not allowed on this community, to ask questions on how to do stuff wich you don't know how?I just showed you the code i have, and i want to write a servlet for those html forms, and need help for that. – helloApp Jan 22 '20 at 14:19
  • Yes, your understanding seems correct about community that its not a free coding service. Your question seems more of a theoretical one so i will try to find helpful links. First write a servlet for one html then try second one and list specific problems you face for second form. – Sabir Khan Jan 22 '20 at 14:27

0 Answers0