How to get form parameter in servlet? request.getAttribute does not work

Is it possible for the same servlet to check? It looks like some kind of recursion might be needed here, but when I enter something into the email field and press the submit button, the email option remains empty. After you click Submit, the URL will change to:http://localhost/servlet/EmailServlet?Email=test

The Email: nulltext field is also displayed on the page , but I expected it to go through the validation function (i.e. it will not be null). Is it possible to achieve this type of recursive behavior?

public class EmailServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException 
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String theForm =
            "<FORM METHOD=\"GET\" ACTION=\"EmailServlet\">\n<INPUT TYPE=\"TEXT\" NAME=\"Email\"><P>\n" 
            + "<INPUT TYPE=\"SUBMIT\">\n</FORM>";
        String email = (String) request.getAttribute("Email");

        // Bogus email validation...
        if( email == null )
        {
            out.println("Email: " + email + "\n" + theForm);
        }
        else if(emailAddressNotBogous(email))
        {
            out.println("Thank you!");
        }
        else
        {
            out.println(""Invalid input. Please try again:\n" + theForm);
        }
        out.flush();        
    }
}

Update: An error occurred in the code in accordance with the accepted answer. Changing getAttribute to getParameter fixes the "problem" :).

String email = (String). getAttribute getParameter ( " " );

+3
2

, :

  request.getParameter("Email");

, , .

+3

POST, doPost() . doGet() , doPost() .

, doGet() - . , . ...

+1

Source: https://habr.com/ru/post/1733419/


All Articles