0

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. this is the servlet code

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

<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

<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>
Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63
user2528061
  • 47
  • 2
  • 5

3 Answers3

3

For <input type="text"> you have to set the name of the component as shown in StackOverflow Servlets wiki example:

<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.

You have other problems with the form you posted in your code:

  • In a simple form, you must not have an enctype="multipart/form-data" at least that you're handling file uploads (that's not shown in the example). If the code is just to send a text data from client to server, remove this.
  • The action attribute in your <form> doesn't send the info to the right URL mapping of the servlet. You should change it to /thu.

In the end, your <form> should look like

<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?

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

I think you need to set the name attribute for all the input elements that you want to access on servlet:

<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.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

I think you not need enctype="multipart/form-data" and check your mapping in web.xml file. It should need to change the url as @Luiggi Mendoza mentioned. we can't send vlaues using enctype="multipart/form-data". In my project I have tried that.But in your problem can be solve with changing those codes.....

falsetru
  • 357,413
  • 63
  • 732
  • 636