0

Im trying to create a Servlet application. When I hit the submit button I want to clear the forms and add the information to a unordered list and store the data. The issue is that I do not know how to evaluate when the button is pushed.

package org.gephi.demos;

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;


// Extend HttpServlet class
@WebServlet(name = "HelloWorld", urlPatterns = {"/test"}, loadOnStartup=1)
public class HelloWorld extends HttpServlet {

  private String message;
  private File f;
  private GephiBuilder gb;
  //private Main m;
  public void init() throws ServletException
  {
      gb = new GephiBuilder();
      System.out.println("Initialized");
      // Do required initialization
      message = "Hello World for sure";
  }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");
      //m = new Main(gb);

      // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println(HTMLUserInput("do you want a Nodes(n) or auto(a)", "directory", "d"));
        String directory = request.getParameter("d");
        // do some processing here...

        // build HTML code
        if(request.getParameter("button")!=null)
        {
            System.out.println("Hello");
            //response.getParameter("d").valu;
            String htmlRespone = "<html>";
            htmlRespone += "<h2>Your directory is: " + directory + "<br/>";     
            htmlRespone += "</html>";

            // return response
            out.println(htmlRespone);
        }
      //out.println(addJSCode());
  }

  public String HTMLUserInput(String question, String id, String name){
        String htmlOutput = "<form action=\"HelloWorld\"><label>"+question+"</label>"
            +"<input name = \""+name+"\" id=\""+id+"\" type=\"text\"/>"
            +"<input id=\"button\" type=\"button\">Submit</form>";
        return htmlOutput;
  }

  public void destroy()
  {
      // do nothing.
  }
}

If I press the button nothing changes, I imagine because this code is only evaluated on startup, how do I get it evaluated anytime I press my submit button.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Kingston12
  • 79
  • 10
  • 1. What exactly should after clicking the button 2. The collating of data can happen at e servlet side hence you can simply capture data from here – Mudassar Jun 25 '15 at 18:17
  • can you explain that more i do not understand – Kingston12 Jun 25 '15 at 18:22
  • My question to you is 1. What exactly should happen after clicking the button 2. The collating of data can happen at the servlet side hence are you okay can simply capture data from here – Mudassar Jun 25 '15 at 18:26
  • right now all that should happen is it should print out "your directory is:" to the webpage,2 i need to collect the data for a separate application, i want to print it back on the screen for the sake of being a GUI but i cannot simply keep it on the page. I need to capture it somewhere, at this point ill take it anywhere – Kingston12 Jun 25 '15 at 18:34

2 Answers2

1

1.You have to use URL for your servlet(wich was specified in servlet mapping) in action attribute . For example: <form action='/test'>

2.There is several way how to submit the form to the server. You can use <input type="submit">Submit</input> inside form tag .

Or if you want to use tag button you have to set id attribute for the form (<form id='myform' .../>) and connect button with the form specifying this id in form attribute:

<button type="submit" form="myform">Submit</button>

And button tag does not have to be located within form tag.

3.After that you can get the content of your form using request.getParameter("input name") and handle these data.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
0

Use <input type="submit" /> instead of <button>



Your urlPatterns = {"/xxxxx"}, should match form action="xxxxx"

faheem farhan
  • 401
  • 2
  • 9