如何在 servlet 中不使用提交按钮来提交表单

发布于 2024-10-28 00:10:26 字数 2184 浏览 3 评论 0原文

if(Category.equals("Seasonal Decorations")){
    out.println("<form name='adddeco' id='adddeco' method='post'>");
    out.println("<input type=hidden name=cater value=decos>" +
                "<table border=0 colspan=5 >" +
                "<tr><th>Name & Description of DECORATION </th><th><input type=text name=deco_itemname width=30></th></tr>");
    out.println("<tr><th>Product ID                  </th><th><input type=text name=deco_PID width=30></th></tr></table>");
    out.println("<tr><th>Provider                    </th><th><input type=text name=deco_provide width=30></th></tr>");
    out.println("<tr><th>AVAILIBILTY</th><th><select name=DECO_AVAIL>"+
                "<option>UNAVAILABLE</option>" +
                "<option>AVAILABLE</option></select></th></tr>");
    out.println("<tr><th>No.of DECORATION                 </th><th><input type=text name=deco_qnty width=30></th></tr>");
    out.println("<tr><th>Shelf NO                    </th><th><input type=text name=deco_shelfno width=30></th></tr>");
    out.println("<tr><th>Original Price              </th><th><input type=text name=deco_orgprice width=30></th></tr>");
    out.println("<tr><th>Selling Price               </th><th><input type=text name=deco_sellprice width=30></th></tr>");
    out.println(" <input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'>&nbsp;&nbsp;&nbsp;<input type=reset value=clear>" +
                "</form>");

上面是我在 Servlet 中的代码,以便向数据库输入值,但问题是我无法将此表单提交到下一页,因为您可以看到我没有使用“提交”按钮我正在使用 onclick='' 事件。如果“out.println();”在 JSP 中,上面的代码就可以工作。已删除,但正如您所看到的,我需要比较 category.equals("seasonaldecors") 并仅在需要时在特定代码上运行,因此如果有人知道如何克服这个特定问题,请帮帮我。

请记住,我无法使用提交按钮来提交,因为我使用的是条形码扫描仪。

if(Category.equals("Seasonal Decorations")){
    out.println("<form name='adddeco' id='adddeco' method='post'>");
    out.println("<input type=hidden name=cater value=decos>" +
                "<table border=0 colspan=5 >" +
                "<tr><th>Name & Description of DECORATION </th><th><input type=text name=deco_itemname width=30></th></tr>");
    out.println("<tr><th>Product ID                  </th><th><input type=text name=deco_PID width=30></th></tr></table>");
    out.println("<tr><th>Provider                    </th><th><input type=text name=deco_provide width=30></th></tr>");
    out.println("<tr><th>AVAILIBILTY</th><th><select name=DECO_AVAIL>"+
                "<option>UNAVAILABLE</option>" +
                "<option>AVAILABLE</option></select></th></tr>");
    out.println("<tr><th>No.of DECORATION                 </th><th><input type=text name=deco_qnty width=30></th></tr>");
    out.println("<tr><th>Shelf NO                    </th><th><input type=text name=deco_shelfno width=30></th></tr>");
    out.println("<tr><th>Original Price              </th><th><input type=text name=deco_orgprice width=30></th></tr>");
    out.println("<tr><th>Selling Price               </th><th><input type=text name=deco_sellprice width=30></th></tr>");
    out.println(" <input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'>   <input type=reset value=clear>" +
                "</form>");

The above is my code in a Servlet in order to enter a value to a DB but the problem is that I'm not able to sumbit this form to the next page as you can see I'm not using a "submit" button instead I'm using an event onclick=''. The above code works in JSP if the "out.println();" is removed but as you can see that I need to compare category.equals("seasonal decorations") and run on the specific code only when required so if anyone who knows about how to overcome this particular problem please help me out.

And please keep in mind that I cannot use a sumbit button to submit as I'm using a barcode scanner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-11-04 00:10:26

避免从 Servlet 中写出 HTML,这是不好的做法。它很难正确,很难维护,而且可读性很差。这是最适合 JSP 的工作。

您的问题似乎是生成的 JavaScript 无效。如果您检查输出页面的源代码,您会发现您正在打印:

<input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'>

请注意,$() 的参数周围没有引号。应该有。

这更接近正确(我说更接近是因为您的 HTML 也可能无效,具体取决于您的 DOCTYPE - 您没有关闭标签,并且属性周围没有引号):

out.println("<input type=button value=add onclick='$(\"#adddeco\").attr(action,additem1);$(\"#adddeco\").submit();'>   <input type=reset value=clear>" 

所有这些转义引号是您应该使用 JSP 的一个重要原因。

Avoid writing out HTML from a Servlet, it's bad practice. It's hard to get right, it's hard to maintain and the readability is abysmal. This is a job best suited for JSPs.

Your problem appears to be that the JavaScript being generated is invalid. If you check the source of the output page, you'll notice that you're printing out:

<input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'>

Note that there are no quotes around the argument to $(). There should be.

This is closer to correct (I say closer because your HTML may be invalid too, depending on your DOCTYPE - you don't close the tags and you don't have quotes around attributes):

out.println("<input type=button value=add onclick='$(\"#adddeco\").attr(action,additem1);$(\"#adddeco\").submit();'>   <input type=reset value=clear>" 

All this escaping of quotes is a big reason why you should use JSPs instead.

唠甜嗑 2024-11-04 00:10:26

您可能应该阅读并使用 JSTL 来使您的代码更具可读性。它允许您使用 if 语句和其他控制结构,而无需求助于 Java 代码。例子:

<c:if test="category eq 'Seasonal Decorations'">
   <form name="adddeco" id="adddeco" method="post">
   ...
   </form>
</c:if>

You should probably read about and use JSTL to make your code more readable. It allows you to use if statements and other control structures without resorting to Java code. Example:

<c:if test="category eq 'Seasonal Decorations'">
   <form name="adddeco" id="adddeco" method="post">
   ...
   </form>
</c:if>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文