0

I'm trying to use forms in my servlet code like this;

 out.println("<FORM ACTION=\""+ BooksBought+"\"method = \"POST\">\n" +
         "<INPUT TYPE=\"SUBMIT\"\n "+
             "       VALUE=\"Update Order\">\n" +
             "</FORM>\n");

But I keep getting errors like BooksBought cannot be resolved to a variable. BooksBought is another servlet that works by itself so I know this is where the problem is. Please help.

web.xml

<servlet-mapping>
  <servlet>
      <servlet-name>BooksBought</servlet-name>
      <servlet-class>BooksBought</servlet-class>
  </servlet>
  <servlet-name>BooksBought</servlet-name>
  <url-pattern>BooksBought</url-pattern>
</servlet-mapping> 
user2162882
  • 61
  • 1
  • 8

2 Answers2

0

Yeah - it's not that magical. That action needs to be the URL that maps to your BooksBought servlet. Are you deploying these in a war? Did you set up a web.xml?

Check out:

What is url-pattern in web.xml and how to configure servlet

http://docs.oracle.com/javaee/7/tutorial/doc/servlets.htm

Community
  • 1
  • 1
jgitter
  • 3,396
  • 1
  • 19
  • 26
  • Also - all the cool kids are now using client-side templating instead of building static HTML on the server. If you're interested, check out Handlebars, Mustache, Undescore templating, among others. – jgitter Feb 19 '14 at 19:14
  • I'm working on eclipse, and I set up a web.xml. I also tried to use the URL that maps to the BooksBought servlet, but nothing works. – user2162882 Feb 19 '14 at 19:18
  • What container are you deploying to? Are you sure that you don't have the url wrong? Can I see the appropriate bits of your web.xml? – jgitter Feb 19 '14 at 19:20
  • Make sure you put a / in front of your URL mapping if you're mapping to a path. Also, your servlet-class should be fully qualified. Are you seeing errors in the logfiles for your container when you attempt to startup or deploy? – jgitter Feb 19 '14 at 19:54
  • If you used a URL string as your action parameter you would not get that error. Please try again. – jgitter Feb 19 '14 at 20:27
0

If BooksBought is a servlet, not a variable, you should do more like:

   out.println("<FORM ACTION=\"BooksBought\" method=\"POST\">");

You also need to put a space between the " and method. You could also use single quotes so you don't have to escape the quotes. Makes it easier to read.

   out.println("<FORM ACTION='BooksBought' method='POST'>");

If the servlet for this form is a level deeper than the one you are going to post to, you'll need to add a ../

   out.println("<FORM ACTION='../BooksBought' method='POST'>");
developerwjk
  • 8,619
  • 2
  • 17
  • 33