0

I'm trying to get values from html but it just gives null with either Post or Get commands. I'm also using Wildfly Application Server. When I submit it goes to the next page but the values seem 'null'.

PS: I added the servlet in xml file like this:

<servlet-mapping>
<servlet-name>DataServlet</servlet-name>
<url-pattern>/dataServlet</url-pattern>
</servlet-mapping>

Servlet:

package webpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/dataServlet")
public class DataServlet extends HttpServlet {


    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // Get
        String combobox=request.getParameter("User");
        String value=request.getParameter("demo");

        PrintWriter writer = response.getWriter();

        // Build
        String htmlRespone = "<html>";
        htmlRespone += "<h2>User Id: " + combobox + "</h2>";
        htmlRespone += "<h2>User Id: " + value + "</h2>";
        htmlRespone += "</html>";

        // Return
        writer.println(htmlRespone);
        System.out.println(combobox);
    }
}

HTML:

<select name="User">
        <option selected="true" value="Example1">User1</option>
        <option value="Example2">User2</option>
        </select>
        <form method="post" action="dataServlet">
        <button type="submit">Change</button>
        </form>
        <h1></h1>
        <p id="demo">Example3</p>
2
  • Your inputs are not in the form so they are not sended Commented Mar 8, 2017 at 8:07
  • Instead of writing to respone, could you try forwarding it to a jsp page as explanied here : stackoverflow.com/questions/2370960/… Commented Mar 8, 2017 at 8:08

1 Answer 1

3

The inputs should be in the <form> like this for User

    <form method="post" action="dataServlet">
        <select name="User">
            <option selected="true" value="Example1">User1</option>
            <option value="Example2">User2</option>
        </select>
        <button type="submit">Change</button>
    </form>

    <h1></h1>
    <p id="demo">Example3</p>

And this is the name attribute that is necessary (demo), note that I don't thing this will work woth a <p>, but you can use a hidden input to set a value in the form.

PS : be careful, you should use the same syntax, once you start with uppercase then with lowercase User demo

EDI : A w3schools form guide

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

2 Comments

So everytime that I need to have a value from html I need to form it? PS: Since I cannot put <p> in <form> it cannot be sent.
@JohnD. No, you can always use Ajax to send a request with specific argument and recover the result in any format you want (this is just text). You should search for it, there is plenty of tutorial, and this is quite simple to use. But in the current page, yes, only inputs inside the form with a name attribute will be add to the request.

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.