0

i have a web project , all i wanna do is to send a String to the servlet then return the same string to the jsp Page(formList.jsp) and display it:

javascript code to send the String :

$(document).ready(function(){
    
    $('#submit').click(function() {
        var value = $('#val').val();
        $.ajax({
            type:'POST',
            data:{value:value},
            url:'FormList',
            success:function(result){
                $('#res').text(result);
            }
        });
    });
    
});

'FormList' Servlet doPost :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        String value = request.getParameter("value");
        response.getWriter().print(value);

}

web.xml (located in WEB-INF):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<servlet-name>FormList</servlet-name>
<servlet-class>FormList</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormList</servlet-name>
<url-pattern>/FormList</url-pattern>
</servlet-mapping>
</web-app>

but the when i click the submit button, the result i get is the formList.jsp page HTML source : (<!DOCTYPE html PUBLIC ....)

*i'm overriding the service method in servlet to initialize the formList.jsp

i think that doPost is not executed at all any help please

Community
  • 1
  • 1
  • 1
    Are you definitely hitting your `doPost` method (is the path in the `url` property of your ajax request right?) Or are you getting back a 404/error page from the server? – 83N May 29 '18 at 14:34
  • run server in debug mode and check if dopost is getting executed and see what is inside String value. if you not equipped with debugger just system.out.println value to check. – Shubham Kadlag May 29 '18 at 14:38
  • @83N , yes it's right (FormList) , should i put make it (/FormList) ? – Tamer Nassar May 29 '18 at 14:42
  • @ShubhamKadlag i tried , doPost is not getting executed :/ , what could be the reason ? – Tamer Nassar May 29 '18 at 14:42
  • @TamerNassar - yes I'd check that path is correct/relative to the page that's requesting it - your doPost should just return plain text of the submitted value - the ` – 83N May 29 '18 at 14:45
  • @83N , i forgot to mention , when i put some random url(jashdka) and submit the button i don't get any error , when i put this url (/FormList) i get java.lang.ClassNotFoundException: FormList . – Tamer Nassar May 29 '18 at 14:47
  • @83N no , it returns the same page HTML source – Tamer Nassar May 29 '18 at 14:52

2 Answers2

0

Give FormList servlet a different url mapping.

I guess there is confusion in resolving formlist.jsp and formlist servlet.

gagan singh
  • 1,591
  • 1
  • 7
  • 12
0

I highly recommend checking out BalusC's answer here on a question about how to do ajax with servlets.

I noticed you are using jquery, you can also create your ajax request like this:

<script>
$(document).on("click", "#submit", function() { 
    var value = $('#val').val(); //get value you want to send
    var params = {value : value}; //set it as one of the parameters

    $.post("FormList", $.param(params), function(response) { //make ajax request
                 $('#res').text(response); //retrieve response and set it to #res
    });
        });
</script>

But anyway, the reason why you are only seeing html is because you are not writing anything back to the response. You need to do it like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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


   response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
    response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
    response.getWriter().write(value);       // Write response body. (not print)
}
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44