Question

I have created a jsp file named roleManagement.jsp :

<%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.sym"%>
<%@ 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>Role Management</title>
        <script type="text/javascript" src="js/jquery-2.0.3.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            $(document).ready(function() { 
                alert("entered in trial button code");

                $.ajax({
                    type: "GET",
                    url:"/jquery2xdemo/RoleManagementContoller",
                    dataType: "json",
                    success: function (data) {
                        $.each(data.aaData,function(i, obj) {
                            alert(obj.value + ":" + obj.text);
                            var div_data = "<option value=" + obj.value + ">" + obj.text + "</option>";
                            alert(div_data);
                            $(div_data).appendTo('#ch_user1'); 
                        });  
                    }
                });
            });
        </script>
        <div id="div_source1">
            <select id="ch_user1" >
                <option value="select"></option>
            </select>
        </div>
        <input type="button" id="id_trial" name="btn_trial" value="Trial Button..">
    </body>
</html>

In the above code ,I am trying to call my servlet on $(document).ready function. But I am unable to do this. I have mapped the servlet information in web.xml as :

<servlet>
    <description></description>
    <display-name>RoleManagementContoller</display-name>
    <servlet-name>RoleManagementContoller</servlet-name>
    <servlet-class>com.programmingfree.controller.RoleManagementContoller</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>RoleManagementContoller</servlet-name>
    <url-pattern>/RoleManagementContoller</url-pattern>
</servlet-mapping>

But when i run my jsp page, it is not at all getting inside the document.ready function. What am i doing wrong. Please help. Thanks in advance

Was it helpful?

Solution

Check than jQuery properly connected to the page. Following advices can be useful:

  1. Try include jQuery from google apis e.g.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>

  1. Use url tag lib:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<script src="<c:url value="/resources/js/jQuery/jquery.js" />" ></script>

OTHER TIPS

You need to use ajax call url as just RoleManagementContoller instead of /jquery2xdemo/RoleManagementContoller as you configured url pattern as /RoleManagementContoller

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top