newbie
newbie

Reputation: 14950

How can I get the html data inside a Java servlet?

Good day!

How can I access the HTML text field value inside a servlet? My example code is as follows:

        out.println("<html><head></head>");
        out.println("<body>");
        out.println("Item not found...");
        out.println("<h2>Add Item:</h2>");
        out.println("<form action = \"AddandSearch\">"); 
        out.println("Item Name: <input type =\"text\" name =\"name\"> <br>");
        out.println("Unit Price: <input type =\"text\" name =\"unitPrice\"> <br>");
        out.println("On Stock : <input type =\"text\" name =\"stock\"> <br><br>");
        out.println("<input type =\"submit\" value =\"Add Item\">");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");

I need to get the value of name, unit price and stock after the user presses the button so i can put it in an arraylist. Is it possible to assign it on the same servlet? I tried using this code:

String id = request.getParameter("name");

but it is not working because the button must be pressed first. Can i use a getter and setter method or anything equivalent? I need a textfield for the data entry and it must be done inside a servlet. The result must also be generated inside the same servlet. Thank you.

Upvotes: 0

Views: 6798

Answers (5)

Stephen C
Stephen C

Reputation: 718678

I tried using this code: String id = request.getParameter("name"); but it is not working because the button must be pressed first.

You do need to use the getParameter(...). But I suspect that you were trying to do this in the same doGet(...) method created the form HTML ... before you sent the response containing that HTML to the user.

What needs to happen is this:

  1. Create HTML and send to the writer.
  2. Return from doGet(...).

  3. Wait for user to click the submit button.

  4. Get a new call on the doGet(...) method.
  5. Figure out that this is an AddandSearch request ... e.g. by looking at the request URI
  6. Call getParameter("name") to get the parameter.

Given that your servlet is (now) handling requests from different forms, the doGet method needs to dispatch to different parts of your code(e.g. different methods) to handle each form type.


(We've also mentioned here and elsewhere that embedding HTML in your code like that is not good engineering practice. It is better to use JSP + JSTL, or some other templating technology.

But if this is what your instructor told you to do for this exercise, go with the flow. He may have a good reason ... like not having time in the course to cover JSP, JSTL and other "advanced" Java EE stuff. Curriculum congestion can be a serious issue.)

Upvotes: 3

Mehul Kaklotar
Mehul Kaklotar

Reputation: 375

your form submit button should have a name.

out.println("<html><head></head>");
    out.println("<body>");
    out.println("Item not found...");
    out.println("<h2>Add Item:</h2>");
    out.println("<form action = \"AddandSearch\">"); 
    out.println("Item Name: <input type =\"text\" name =\"name\"> <br>");
    out.println("Unit Price: <input type =\"text\" name =\"unitPrice\"> <br>");
    out.println("On Stock : <input type =\"text\" name =\"stock\"> <br><br>");
    out.println("<input type =\"submit\" name=\"submit\" value =\"Add Item\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");

And when you press the submit button redirect to the same servlet and

if(request.getParameter("submit")!=null)
{

      //your code to handle form submission 
}
else
{
out.println("<html><head></head>");
    out.println("<body>");
    out.println("Item not found...");
    out.println("<h2>Add Item:</h2>");
    out.println("<form action = \"AddandSearch\">"); 
    out.println("Item Name: <input type =\"text\" name =\"name\"> <br>");
    out.println("Unit Price: <input type =\"text\" name =\"unitPrice\"> <br>");
    out.println("On Stock : <input type =\"text\" name =\"stock\"> <br><br>");
    out.println("<input type =\"submit\" name=\"submit\" value =\"Add Item\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
}

Upvotes: 0

Abi
Abi

Reputation: 65

in the doGet method, you can use this syntax to get the value,

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

Upvotes: 0

Arve
Arve

Reputation: 8128

String id = (request.getParameter("name")==null)?"nothing here":request.getParameter("name");

Are you handling this in the doGet() method of the AddandSearch handling servlet? You should probably use method="post" (as the action is called "add", which gives a hint of persistence) and handle it in the doPost() method.

Upvotes: 1

Mike Baranczak
Mike Baranczak

Reputation: 8374

it is not working because the button must be pressed first

I think you just answered your own question right there.

Upvotes: 1

Related Questions