3

So, after a lot of trial and error, I finally got a server up and running along with an example servlet.

I'm trying to make it so that you can enter a song and artist and press submit and my java program will read the information.

The information I want is from a form on a JSP page. The code is here:

<form>
            <input type="text" placeholder="Enter Song"/>
            <input type="text" placeholder="Enter Artist"/>
            <input type="submit" value="Submit" formmethod="post" formaction="GetSongAndArtist"/>
</form>

Now, I have this written already for my servlet code, but I'm just unsure what I need to do to finish it:

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

    response.setContentType("text/html;charset=UTF-8");

    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet GetSongAndArtist</title>");
        out.println("</head>");
        out.println("<body>");
        BufferedReader br = request.getReader();
        out.println("<h2>" + br.read() + "</h2>");
        out.println("<h1>Servlet GetSongAndArtist at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

So my questions are, will the information that the user writes in the text boxes automatically be included in the HTTP post request? Also, what would be the best way of reading the information?

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
Tim Ogden
  • 35
  • 6
  • It might be quicker to follow any of many servlet tutorials available on the web, although I'd focus on (a) modern servlets, and (b) absolutely nothing that includes generating HTML in the servlet :( – Dave Newton Dec 31 '15 at 14:17

2 Answers2

3

Try giving your inputs names:

<input type="text" name="song" placeholder="Enter Song"/>
<input type="text" name="artist" placeholder="Enter Artist"/>

Then get your parameters using the HttpServletRequest Object:

String song = request.getParameter("song");
String artist = request.getParameter("artist");
brso05
  • 13,142
  • 2
  • 21
  • 40
  • Wow, that was a lot easier than I was expecting, thank you! I don't have the reputation on this account to mark this as the right answer, but if someone else can do it, feel free! – Tim Ogden Dec 31 '15 at 14:34
  • @TimOgden you should be able to u might just have to wait a while. There is a difference between marking as correct and upvoting. You won't be able to upvote but you can mark as correct... – brso05 Dec 31 '15 at 14:36
1

You can write your form like this:

   <form action="ServletName" method="GET">
   <input type="text" name="song" placeholder="Enter Song"/>
    ...
   </form>

And create a web.xml file something like this:

  <servlet>
    <servlet-name>ServletName</servlet-name>
    <servlet-class>Servlet_name</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ServletName</servlet-name>
    <url-pattern>/ServletName</url-pattern>
</servlet-mapping>

And in your servlet to retrieve data use this:

 String song = request.getParameter("song");
 ...
brso05
  • 13,142
  • 2
  • 21
  • 40
Abdelhak
  • 8,299
  • 4
  • 22
  • 36