[英]Transfer data from jsp to servlet
I trie to send data from a jsp form to a servlet, but im not able to read the data sended via POST method in the servlet. 我试图将数据从jsp表单发送到servlet,但是无法读取通过servlet中的POST方法发送的数据。 this is the servlet code
这是servlet代码
public class TUhServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String s= req.getParameter("post_text");
System.out.println(s);
resp.sendRedirect("/tuh.jsp");
}
}
This is the form in tuh.jsp 这是tuh.jsp中的形式
<form class="post_form" action="./"
method="post" enctype="multipart/form-data">
0"/><br/>
Message : <input name="post_text" type="text" size="30" maxlength="30"/>
<p><input type="submit" value="Post!"/></p>
<input type="hidden" name="type" value="post"/>
</form>
web.xml web.xml
<servlet>
<servlet-name>TUh</servlet-name>
<servlet-class>tuh.TUhServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TUh</servlet-name>
<url-pattern>/tuh</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>tuh.jsp</welcome-file>
</welcome-file-list>
</web-app>
For <input type="text">
you have to set the name of the component as shown in StackOverflow Servlets wiki example : 对于
<input type="text">
您必须设置组件的名称,如StackOverflow Servlets Wiki示例所示 :
<input id="post_text" name="post_text" type="text" size="30" maxlength="30"/>
Do the same for all your components as well like the hidden
field. 对所有组件以及
hidden
字段执行相同的操作。
You have other problems with the form you posted in your code: 您在代码中发布的表单还有其他问题:
enctype="multipart/form-data"
at least that you're handling file uploads (that's not shown in the example). enctype="multipart/form-data"
(示例中未显示)。 If the code is just to send a text data from client to server, remove this. action
attribute in your <form>
doesn't send the info to the right URL mapping of the servlet. <form>
中的action
属性不会将信息发送到servlet的正确URL映射。 You should change it to /thu
. /thu
。 In the end, your <form>
should look like 最后,您的
<form>
应该看起来像
<form class="post_form" action="thu" method="post">
<!-- contents... -->
</form>
In case you're handling a file upload but didn't posted all the JSP code here, then you should look for another approach to extract the data send in the request explained here: How to upload files to server using JSP/Servlet? 如果您正在处理文件上载但没有在此处发布所有JSP代码的情况,那么您应该寻找另一种方法来提取请求中发送的数据,该方法在此处说明: 如何使用JSP / Servlet将文件上载到服务器?
I think you need to set the name
attribute for all the input elements that you want to access on servlet: 我认为您需要为要在servlet上访问的所有输入元素设置
name
属性:
<input name="post_text" type="text" size="30" maxlength="30"/>
Now as @Luiggi mentioned, but I think ID
attribute is not required, name
attribute is required. 现在就像@Luiggi提到的那样,但我认为
ID
属性不是必需的, name
属性是必需的。
I think you not need enctype="multipart/form-data"
and check your mapping in web.xml file. 我认为您不需要
enctype="multipart/form-data"
并检查web.xml文件中的映射。 It should need to change the url as @Luiggi Mendoza mentioned. 它应该需要更改@Luiggi Mendoza提到的URL。 we can't send vlaues using
enctype="multipart/form-data"
. 我们无法使用
enctype="multipart/form-data"
发送vlaues。 In my project I have tried that.But in your problem can be solve with changing those codes..... 在我的项目中,我曾尝试过。但是在您的问题中,可以通过更改代码来解决.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.