I have a question about programming Java into HTML using the latest version of Tomcat. I'm trying to get the option values from the HTML to go inside the Java program and print out said values. A friend of mine suggested that I create an array of strings and then print out from there, and I decided to take that advice, however I get the following error after the following code:

<form action="displayMusicChoices" method="post">
 <--...some table code -->
            <td align="right" valign="top">I'm interested in these types of music:</td>
            <td>
                <select name="musicChoices" size="4" multiple>
                    <option name="Rock">Rock</option>
                    <option name="Country">Country</option>
                    <option name="Bluegrass">Bluegrass</option>
                    <option name="Folk">Folk</option>
                </select>
            </td>
        </tr>
    <tr>
      <td></td>
      <td><br><input type="submit" value="Submit"></td>
    </tr>
  </table>
  </form>
String[] choices = request.getParameterValues("musicChoices");
 int select_choices = choices.length;
...///Other code
 out.println(
          "<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
   ///Other ocde
        + "<p>We'll use this email to notify you whenever we have new releases ofr these types of music: </br>"

for (int i =0; i <= select_choices; i++)
out.print(choices[i] + " ");
        ///Print out selections
java.lang.NullPointerException
	DisplayMusicChoicesServlet.doPost(DisplayMusicChoicesServlet.java:61)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I can understand the NullPointerException, but I don't get HOW I can get it with this code unless it's not getting the option, which in that case I need advice on how to get said options.

Thanks for reading.

I thought that the select tag would send only the value you selected if it is not a multiple selection? Hmm... Haven't played with Servlet for almost a year... Need to recall it...

Here's the error code I get too:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

What's the code at the line (in your servlet) for this error

DisplayMusicChoicesServlet.doPost(DisplayMusicChoicesServlet.java:61)

By the way, where is the 'value' property (not 'name') for each option in your selection???

What's the code at the line (in your servlet) for this error

DisplayMusicChoicesServlet.doPost(DisplayMusicChoicesServlet.java:61)

By the way, where is the 'value' property (not 'name') for each option in your selection???

I tried putting in the values, but I used alphabet names (aka Rock, Folk ect.) instead of numbers

Here's the lines for 60 and 61 to give you context:

+ "button in your browser or the Return button shown <br>\n"
        + "below.</p>\n"
Member Avatar for ztini

Do you have @WebServlet above your class that extends HttpServelet?

@WebServlet("displayMusicChoices")
public class Servlet extends HttpServlet {	

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException {

...

Although you should if you're getting:

DisplayMusicChoicesServlet.doPost(DisplayMusicChoicesServlet.java:61)
Member Avatar for ztini

Oh,

It may be that you have " <option name="Folk">Folk</option>"

That should be " <option value="Folk">Folk</option>", this is the value for the name "musicChoices".

That's what I asked in my previous post as well...

That's what I asked in my previous post as well...

Here's what you're looking for:

public class DisplayMusicChoicesServlet extends HttpServlet
{    
    /**
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(
        HttpServletRequest request, 
        HttpServletResponse response) 
        throws ServletException, IOException
    {

//More code

 out.close();  
                doPost(request, response);
Member Avatar for ztini

Here's what you're looking for:

public class DisplayMusicChoicesServlet extends HttpServlet
{    
   ..

//More code

 out.close();  
                doPost(request, response);

I disagree with your last 2 lines there. You don't need to call doPost inside of doPost(), this is called automatically.

Also, "out" belongs to the parameter response (PrintWriter out = response.getWriter()). In my opinion, you shouldn't close this writer; that is the responsibility of the class owner...you can't assume it is not needed anymore.

I just tested changing from "name" to "value" for the option in my TomCat and it worked.

I disagree with your last 2 lines there. You don't need to call doPost inside of doPost(), this is called automatically.

Also, "out" belongs to the parameter response (PrintWriter out = response.getWriter()). In my opinion, you shouldn't close this writer; that is the responsibility of the class owner...you can't assume it is not needed anymore.

I just tested changing from "name" to "value" for the option in my TomCat and it worked.

Unfortunately it's not working on my Tomcat. Though I know it's in bad taste, I'll upload the code I have so far.

I think it should give you a clearer picture. IT might be my web.xml file that's not responding properly.

Here's the HTML since it's not uploading right

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
    <title>Murach's Java Servlets and JSP</title>
</head>

<body>
  <h1>Join our email list</h1>
  <p>To join our email list, enter your name and
     email address below. <br>
     Then, click on the Submit button.</p>

  <form action="displayMusicChoices" method="post">
  <table cellspacing="5" border="0">
    <tr>
      <td align="right">First name:</td>
      <td><input type="text" name="firstName"></td>
    </tr>
    <tr>
      <td align="right">Last name:</td>
      <td><input type="text" name="lastName"></td>
    </tr>
    <tr>
      <td align="right">Email address:</td>
      <td><input type="text" name="emailAddress"></td>
    </tr>
        <tr>
            <td align="right" valign="top">I'm interested in these types of music:</td>
            <td>
                <select name="musicChoices" size="4" multiple>
                    <option value="Rock">Rock</option>
                    <option value="Country">Country</option>
                    <option value="Bluegrass">Bluegrass</option>
                    <option value="Folk">Folk</option>
                </select>
            </td>
        </tr>
    <tr>
      <td></td>
      <td><br><input type="submit" value="Submit"></td>
    </tr>
  </table>
  </form>
</body>

</html>

Once again, thanks for helping. I will try finding the error to the problem.

Member Avatar for ztini

Add

@WebServlet("/displayMusicChoices")

above your class declaration:

@WebServlet("/displayMusicChoices")
public class DisplayMusicChoicesServlet extends HttpServlet {

Add

@WebServlet("/displayMusicChoices")

above your class declaration:

@WebServlet("/displayMusicChoices")
public class DisplayMusicChoicesServlet extends HttpServlet {

I did what you said and I got this error now

DisplayMusicChoicesServlet.java:13: cannot find symbol
symbol: class WebServlet
      @WebServlet("/displayMusicChoices")
       ^
1 error
Member Avatar for ztini
import javax.servlet.annotation.WebServlet;

I have to applogize, I thought I was using the latest release of Tomcat when I've been using the 5x version.

I have to applogize, I thought I was using the latest release of Tomcat when I've been using the 5x version.

Okay, I did go and install 7.0.8 and now I'm getting this error:

type Status report

message /examples/servlets/displayMusicChoices

description The requested resource (/examples/servlets/displayMusicChoices) is not available.

Java upTo latest 1.6.24 implements just up to Html <= 3.2

By the way, when you do doPost() inside a servlet, you could simply pass all variable to doGet(). Do not call its own function or you are creating a recursive call. Both doPost() and doGet() methods should not be recursive...

dude it's damn easy problem. Just give every option value attribute,
You can select one option at a time then:
assuming following:

<select name='hmm'>
<option value='one'>one</option>

Assuming you are submitting form via post

//inside doPost()
String selectValue=request.getParameter("hmm");
//selectValue variable would contain one now

This should help you :) sorry for any mistakes I am standing in train and typing :/

why do you people suggest to call doPost() or doGet() from other methods?? Frankly it's not worth, not needed. And @threadstarter check your web.xml file for configuration. If you have configured properly then afterwards try reloading the folder via tomcat manager.

<web-app>
<servlet>
<servlet-name>MusicStore</servlet-name>
<servlet-class>MusicStore <!--put ur class file name--></servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MusicStore</servlet-name>
<url-pattern>/MusicStore</url-pattern>
</servlet-mapping>
</web-app>

put this file in a folder along with your servlet class file in classes folder. Please ones check web-app tag i am not able to recollect it. Your servlet should be accessible via localhost:8080/urfolder/MusicStore don't forget to reload the folder from tomcat manager

@mandy011
Your first post is exactly the same as ztini and I asked for. Not new. Your second post is rude by calling 'who do you people'. Also, post & get methods actually works the same way. Why do you need to implement them separately? By using one to call the other is simple enough.

Anyway, the problem in his code tends to be more from recursive call because his doPost() calls doPost() again. That's the problem.

I didn't mean it rudely.. I was asking Why you people suggest! and another thing i was in train so i couldn't see the replies before mine.. I mean I couldn't check all replies Bcoz I was in train. And the topic wasn't solved topic thus I replied. If you felt I was rude sorry for that. But I didn't mean rudely.
Frankly speaking , Both methods doesn't work same way as such. Cause if it would have been Why java developers implemented separate methods for both purposes? Both take same arguments .. And specially optimized methods are written for the form method i.e. get and post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.