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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
John D.
  • 81
  • 1
  • 1
  • 12
  • Your inputs are not in the form so they are not sended – AxelH Mar 08 '17 at 08:07
  • Instead of writing to respone, could you try forwarding it to a jsp page as explanied here : http://stackoverflow.com/questions/2370960/generate-an-html-response-in-a-java-servlet – Darshan Mehta Mar 08 '17 at 08:08

1 Answers1

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

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • So everytime that I need to have a value from html I need to form it? PS: Since I cannot put

    in

    it cannot be sent.
    – John D. Mar 08 '17 at 08:13
  • @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. – AxelH Mar 08 '17 at 08:19