Jumpstart your web design projects with these incredible templates. These are the best templates that can help you start designing and developing better & faster.
Try them for FREE! → bit.ly/creative-free-templates
Try them for FREE! → bit.ly/creative-free-templates
There are times in a web application where you want to populate a dropdown list based on the value of another drop down list. In this example, we will be creating a dropdown list for favorite spots and favorite player; since this scenario involves in returning complex java objects like lists, maps, etc, so I’m using JSON for this purpose.
In the article on AJAX in Servlet & jsp using JQuery I have demonstrated on returning simple object from servlet to jsp via jquery.
Library required
Google gson
Project Structure
Steps done to set up our Servlet for JSON
From the browser perspective: jQuery
jQuery allows you to fire an ajax request via get or post method and expects a JSON object as a response, as described in my previous post the first and second parameter of these method are the url and a key-value pair and the third argument of get method is a function that defines what is to be done with the response that is got back from the Servlet.
Jsp Page
<html> <head> <title>AJAX in Servlet using JQuery and JSON</title> <script src="js/jquery-1.11.1.js" type="text/javascript"></script> <script> $(document).ready(function() { $('#sports').change(function(event) { var sports = $("select#sports").val(); $.get('JsonServlet', { sportsName : sports }, function(response) { var select = $('#player'); select.find('option').remove(); $.each(response, function(index, value) { $('<option>').val(value).text(value).appendTo(select); }); }); }); }); </script> </head> <body> <h3>AJAX in Servlet using JQuery and JSON</h3> Select Favorite Sports: <select id="sports"> <option>Select Sports</option> <option value="Football">Football</option> <option value="Cricket">Cricket</option> </select> <br /> <br /> Select Favorite Player: <select id="player"> <option>Select Player</option> </select> </body> </html>
Here jquery will fire an ajax request when the value of the first dropdown is changed(id=”sports”).
Another must read:
- AJAX Tooltip in Java Web Application using qTip2 jQuery plugin
- jQuery form validation using jQuery Validation plugin
- Pagination in Servlet and JSP using jQuery jTable plugin
From the server’s perspective: Servlet
In Servlet, I’m going to use a GSON library to convert Java objects (lists, maps etc) to JSON strings that will be parsed by JQuery in the JSP page and will be displayed on the web page and note that I have returned the response back to jsp based on the value of sports parameter which i pass to the servlet via jQuery’s get() method.
Servlet
package com.action; import java.io.IOException; import java.util.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; public class JsonServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sportsName = request.getParameter("sportsName"); List<String> list = new ArrayList<String>(); String json = null; if (sportsName.equals("Football")) { list.add("Lionel Messi"); list.add("Cristiano Ronaldo"); list.add("David Beckham"); list.add("Diego Maradona"); } else if (sportsName.equals("Cricket")) { list.add("Sourav Ganguly"); list.add("Sachin Tendulkar"); list.add("Lance Klusener"); list.add("Michael Bevan"); } else if (sportsName.equals("Select Sports")) { list.add("Select Player"); } json = new Gson().toJson(list); response.setContentType("application/json"); response.getWriter().write(json); } }
Web.xml
Servlet mapping should be done in web.xml as shown below<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>JsonServlet</servlet-name> <servlet-class>com.action.JsonServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>JsonServlet</servlet-name> <url-pattern>/JsonServlet/*</url-pattern> </servlet-mapping>
Demo
On selecting ‘Football’ in the first dropdown list
On selecting ‘Cricket’ in the first dropdown list
1.Can we define a class without main method?
2.Can main() method take an argument other than string array?
- No, you can’t run java class without main method.
- Before Java 7, you can run java class by using static initializers. But, from Java 7 it is not possible.
- No, argument of main() method must be string array.
- But, from the introduction of var args you can pass var args of string type as an argument to main() method. Again, var args are nothing but the arrays.
- package com.instanceofjava;
- public class MainMethod
- {
- public static void main(String args[])
- {
- }
- }
3.Can we change return type of main() method?
4.Why main() method must be static?
5.Can We Declare main() Method As Non-Static?
6.Can We Overload main() method?
7.Can we declare main() method as private or protected or with no access modifier?
8.Can we override main in Java ?
9.Can we make main final in Java?
10.Can we make main synchronized in Java?
- No, the return type of main() method must be void only. Any other type is not acceptable.
- package com.instanceofjava;
- public class A
- {
- public static int main(String[] args)
- {
- return 1; //run time error : No main method found
- }
- }
4.Why main() method must be static?
- main() method must be static.
- If main() is allowed to be non-static, then while calling the main method JVM has to instantiate it’s class.
- While instantiating it has to call constructor of that class. There will be an ambiguity if constructor of that class takes an argument.
- For example, In the below program what argument JVM has to pass while instantiating class “A”?.
- package com.instanceofjava;
- public class A
- {
- public A(int i)
- {
- //Constructor taking one argument
- }
- public void main(String[] args)
- {
- //main method as non-static
- }
5.Can We Declare main() Method As Non-Static?
- No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class.
- If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.
- package com.instanceofjava;
- public class A
- {
- public void main(String[] args)
- {
- System.out.println("indhu"); //Run time error
- }
- }
6.Can We Overload main() method?
- Yes, We can overload main() method. A Java class can have any number of main() methods. But to run the java class, class should have main()
- method with signature as “public static void main(String[] args)”. If you do any modification to this signature, compilation will be successful.
- But, you can’t run the java program. You will get run time error as main method not found.
- package com.instanceofjava;
- public class A
- {
- public static void main(String[] args)
- {
- System.out.println("Indhu");
- }
- void main(int args)
- {
- System.out.println("Sindhu");
- }
- long main(int i, long d)
- {
- System.out.println("Saidesh");
- return d;
- }
- }
7.Can we declare main() method as private or protected or with no access modifier?
- No, main() method must be public. You can’t define main() method as private or protected or with no access modifier.
- This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.
- package com.instanceofjava;
- public class A
- {
- private static void main(String[] args)
- {
- //Run time error
- }
- }
- No you can not override main method in Java, Why because main is static method and in Java static method is bonded during compile time and you can not
- override static method in Java.
9.Can we make main final in Java?
- you can make main method final in Java. JVM has no issue with that. Unlike any final method you can not override main in Java.
10.Can we make main synchronized in Java?
- Yes, main can be synchronized in Java, synchronized modifier is allowed in main signature and you can make your main method synchronized in Java.