6

I am trying to pass basic values such as id from jsp to the servlet through ajax. I tried everything but only null is being passed. Even console.log(val) does not print anything to browser console.

My understanding is: Web page has form values which onsubmit calls js file. js has ajax which calls the servlet and passes the data of the form. The servlet grabs data from ajax by request.getParameter(val)

Here is my code:

Main.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript">
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>

<form method="post" action="Main" id="firstform">
    <h1>Enter name:</h1>
    <input type="text" name="id" id="id" />
    <input type="submit" name="submit"/>
</form>

</body>
</html>

main.js

var form = $('#firstform');
console.log("gi");
form.submit(function()
{
    $.ajax({
        url: 'Main',
        data: form.serialize(),
        type: 'post',
        success: function(data){ 
            console.log(data);
        }
            });

    //return false;

});

Main.java

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Main
 */
@WebServlet("/Main")
public class Main extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Main() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int ids;
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String val = request.getParameter("id");
        System.out.print(val);
        if(val != null){
            ids = Integer.parseInt(val);
            out.print(ids); //
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

**Problems:
1)values passed from jsp to servlet
2)console.log doesnt print anything on browser console

1) works but 2) still doesnt.**

5
  • give a name attribute to this <input type="text" id="id" /> Commented May 6, 2014 at 4:27
  • change <input type="text" id="id" /> to <input type="text" id="id" name="id" /> Commented May 6, 2014 at 4:28
  • tried @www.sblog.in but still didnt work Commented May 6, 2014 at 4:40
  • try to log inside form.submit() and see what value you get. also check for form.serialize() Commented May 6, 2014 at 4:43
  • @fscore did u get your answer? Commented May 6, 2014 at 7:20

3 Answers 3

4

in main.js type is type: 'post' and you have written code in get method do type:'get'

Sign up to request clarification or add additional context in comments.

3 Comments

I changed to 'post' now but still no result. It doesnt even print on java console as mentioned here - System.out.print(val);
So type should be 'get' and not post... it worked then :) thanks
I accepted your answer although.. console.log doesnt still work
3

there is no name attribute in your input field. when you are doing

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

then in servlet then it will search for the input field having name="id" but in your form there is nothing so it will return null;

give name to the input field like

<input type="text" id="id" name="id"/>

also as sanjay has said your ajax has type post so change it to get as well

4 Comments

@fscore first try by submiting the form without using ajax and let me know if you get any values or not
@fscore you mean this line console.log("gi");
yes it does not print. infact even if I do console.log(id) it doesnt print that too
@fscore if not errors then you should atleast get this in console console.log("gi");.atleast gi will get printed
0

Just for the console.log(data) problem, may be $.ajax() function get confused with response type, try this:

  • Ajax

    $.ajax({
        url: 'Main',
        data: form.serialize(),
        type: 'post',
        dataType:'text/plain',
        success: function(data){ 
            console.log(data);
        }
    });
    
  • Servlet

    response.setContentType("text/plain;charset=UTF-8");
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.