简体   繁体   English

如何在Java Servlet中获取html数据?

[英]How can I get the html data inside a Java servlet?

Good day! 美好的一天!

How can I access the HTML text field value inside a servlet? 如何访问servlet中的HTML文本字段值? My example code is as follows: 我的示例代码如下:

        out.println("<html><head></head>");
        out.println("<body>");
        out.println("Item not found...");
        out.println("<h2>Add Item:</h2>");
        out.println("<form action = \"AddandSearch\">"); 
        out.println("Item Name: <input type =\"text\" name =\"name\"> <br>");
        out.println("Unit Price: <input type =\"text\" name =\"unitPrice\"> <br>");
        out.println("On Stock : <input type =\"text\" name =\"stock\"> <br><br>");
        out.println("<input type =\"submit\" value =\"Add Item\">");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");

I need to get the value of name, unit price and stock after the user presses the button so i can put it in an arraylist. 用户按下按钮后,我需要获取名称,单价和股票的值,这样我才能将其放入arraylist中。 Is it possible to assign it on the same servlet? 是否可以在同一servlet上分配它? I tried using this code: 我尝试使用此代码:

String id = request.getParameter("name");

but it is not working because the button must be pressed first. 但是它不起作用,因为必须首先按下该按钮。 Can i use a getter and setter method or anything equivalent? 我可以使用getter和setter方法或任何等效方法吗? I need a textfield for the data entry and it must be done inside a servlet. 我需要一个用于输入数据的文本字段,并且它必须在servlet内部完成。 The result must also be generated inside the same servlet. 结果还必须在相同的servlet内生成。 Thank you. 谢谢。

I tried using this code: String id = request.getParameter("name"); 我尝试使用此代码: String id = request.getParameter("name"); but it is not working because the button must be pressed first. 但是它不起作用,因为必须首先按下该按钮。

You do need to use the getParameter(...) . 确实需要使用getParameter(...) But I suspect that you were trying to do this in the same doGet(...) method created the form HTML ... before you sent the response containing that HTML to the user. 但是我怀疑您尝试使用创建表单HTML ...的相同doGet(...)方法执行此操作,然后再将包含该HTML的响应发送给用户。

What needs to happen is this: 需要发生的是:

  1. Create HTML and send to the writer. 创建HTML并发送给作者。
  2. Return from doGet(...) . doGet(...)返回。

  3. Wait for user to click the submit button. 等待用户单击提交按钮。

  4. Get a new call on the doGet(...) method. doGet(...)方法上获得新的调用。
  5. Figure out that this is an AddandSearch request ... eg by looking at the request URI 找出这是一个AddandSearch请求...例如通过查看请求URI
  6. Call getParameter("name") to get the parameter. 调用getParameter("name")以获取参数。

Given that your servlet is (now) handling requests from different forms, the doGet method needs to dispatch to different parts of your code(eg different methods) to handle each form type. 假设您的Servlet现在正在处理来自不同形式的请求,则doGet方法需要分派到代码的不同部分(例如,不同的方法)以处理每种形式的类型。


(We've also mentioned here and elsewhere that embedding HTML in your code like that is not good engineering practice. It is better to use JSP + JSTL, or some other templating technology. (我们在这里和其他地方也提到过,将HTML嵌入代码中并不是一种好的工程实践。最好使用JSP + JSTL或其他模板技术。

But if this is what your instructor told you to do for this exercise, go with the flow. 但是,如果这是您的讲师告诉您进行此练习的步骤,请顺其自然。 He may have a good reason ... like not having time in the course to cover JSP, JSTL and other "advanced" Java EE stuff. 他可能有一个很好的理由……就像没有时间学习课程中的JSP,JSTL和其他“高级” Java EE一样。 Curriculum congestion can be a serious issue.) 课程拥挤可能是一个严重的问题。)

String id = (request.getParameter("name")==null)?"nothing here":request.getParameter("name");

Are you handling this in the doGet() method of the AddandSearch handling servlet? 您是否在AddandSearch处理servlet的doGet()方法中进行处理? You should probably use method="post" (as the action is called "add", which gives a hint of persistence) and handle it in the doPost() method. 您可能应该使用method =“ post”(因为该操作称为“ add”,从而提供了持久性的提示),并在doPost()方法中对其进行了处理。

it is not working because the button must be pressed first 它不起作用,因为必须先按下按钮

I think you just answered your own question right there. 我想您只是在这里回答了自己的问题。

在doGet方法中,您可以使用此语法获取值,

String title = request.getParameter("title");

your form submit button should have a name. 您的表单提交按钮应有一个名称。

out.println("<html><head></head>");
    out.println("<body>");
    out.println("Item not found...");
    out.println("<h2>Add Item:</h2>");
    out.println("<form action = \"AddandSearch\">"); 
    out.println("Item Name: <input type =\"text\" name =\"name\"> <br>");
    out.println("Unit Price: <input type =\"text\" name =\"unitPrice\"> <br>");
    out.println("On Stock : <input type =\"text\" name =\"stock\"> <br><br>");
    out.println("<input type =\"submit\" name=\"submit\" value =\"Add Item\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");

And when you press the submit button redirect to the same servlet and 当您按下Submit按钮时,将重定向到相同的servlet,

if(request.getParameter("submit")!=null)
{

      //your code to handle form submission 
}
else
{
out.println("<html><head></head>");
    out.println("<body>");
    out.println("Item not found...");
    out.println("<h2>Add Item:</h2>");
    out.println("<form action = \"AddandSearch\">"); 
    out.println("Item Name: <input type =\"text\" name =\"name\"> <br>");
    out.println("Unit Price: <input type =\"text\" name =\"unitPrice\"> <br>");
    out.println("On Stock : <input type =\"text\" name =\"stock\"> <br><br>");
    out.println("<input type =\"submit\" name=\"submit\" value =\"Add Item\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM