0

I have a servlet that creates an html text box and then redirects to another servlet on submit. How can I access the value of the html text box from the new servlet? I am able to access servlet variables from the new servlet but I am not aware of how to access the value of the html generated code.

thanks,

Here is the servlet that gets the text input

  public class ServletB extends HttpServlet {



    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {

        response.setContentType("text/html");

String value = System.getProperty("card");


PrintWriter out = response.getWriter();


out.println("<center><h1>Your preffered method of payment is "+value+"</h1><br />");
out.println("Please Enter Card Number<input type =\"text\" name = \"number\"/><form action=\"http://codd.cs.gsu.edu:9999/cpereyra183/servlet/ServletC\"><input type =\"submit\" value=\"Continue\" /><input type=\"button\" value=\"Cancel\" /></center>");

    }
   }}

This is the servlet the first servlet redirects to all I do is try to do is output the text input

  public class ServletC extends HttpServlet {



    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {

        response.setContentType("text/html");

String value = System.getProperty("card");


PrintWriter out = response.getWriter();

out.println(request.getParameter("number")); 
    }
    }

3 Answers 3

2

If you give the input field a name

<input type="text" name="foo">

then you can access it in the postprocessing servlet as a request parameter by the input field's name.

String foo = request.getParameter("foo");

See also:


Unrelated to the concrete question, in contrary to what the majority of servlet tutorials want to let believe us, HTML actually belongs in JSP, not in a Servlet. I'd suggest to put that HTML in a JSP.

Sign up to request clarification or add additional context in comments.

1 Comment

I saw the "another answer has been posted" message, and I just knew it was one of yours :)
1

If your markup looks something like this...

<form action="anotherServlet">
    <input name="myTextbox" />
</form>

...then you can get the value out of the HttpServletRequest object in the doGet() or doPost() method of anotherServlet like this:

String textboxValue = request.getParameter("myTextbox");

See: ServletRequest#getParameter().

5 Comments

the getParameter works only when you are going from an html page to a servlet, this is going from a servlet to a servlet, the html is inside the servlet itself
What do you mean "inside the servlet?" Please don't tell me it's an instance variable. If the variable was set with request.setAttribute(String name, Object value) you can use request.getAttribute(String name) to get it back out.
No its not an instance variable. All I am doing with say servlet1 is creating html code by outputting to the writer, when I pull up servlet 1 it shows a text box and a submit button, when I submit it goes to servlet 2, if servlet1 was a regular webpage i could access each html element by the getparameter, but it is a servlet not an html file, so when I try to use getparameter in servlet 2 by calling using say out.println(request.getParameter("number")); I get null number being the name of the text box
I do not understand at all what you mean by this: "It is a servlet not an html file." Does servlet1 get sent to a browser or not? In the end, it's all HTML, regardless of how the HTML is generated. It doesn't matter than the HTML is generated by outputting bits and bobs of HTML to the writer. The browser just sees HTML. Are you sure that you're passing the right string to getParameter()? Are you sure that the generated HTML has an input with the name that you're expecting? Could you paste the markup of the form that's being submitted into your question?
The input element must be inside of the form element. Right now, it's not. Also, as BalusC pointed out, the markup really belongs in JSPs, not the servlet.
1
public class Formvalid extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter pr = response.getWriter();
    boolean flag = true;
int count=0;
    response.setContentType("text/html");
    Enumeration enume;
    enume = request.getParameterNames();
    while (enume.hasMoreElements()) {
        count++;
        String name = (String) enume.nextElement();
        String value = request.getParameter(name);
        if (value == null ||  value.equals("")) {

            pr.println("<h5 style='color:red;'>please enter manditory values  :"
                    + name + "</h5>");
            flag = false;
        } 

    }

    pr.println("<h3>Employe Registation</h3>");

    if (!flag || count==0) {
        pr.println("<form method=\"get\" action=\"formvalid\"><br />EmpName *:<input type='text' name='Empname' ><br />"
                + "Age *:<input type='text' name='age' ><br /><tr><td>Qulification *:<input type='text' name='Qualification' ><br />Address<textarea> </textarea><br /><input type='submit' value='submit'><input type='reset' value='reset'></FORM>");
    } else {
        pr.println("<h3 style='color:green;'>submitted successfully</h3>");
    }

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.