GetStates Servlet

Wednesday, December 29, 2010 Posted by Jagadeesh Manne
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import database.DBClass;
/**
 * @author Jagadeesh
 * if you like this example please submit your comments and suggestions
 * if you like this example please submit your comments and suggestions
 * if you have any doubts you can post comments in blog comment form
 */
public class GetStates extends HttpServlet {
   
    
    protected void processRequest(HttpServletRequest request, 
    HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //creating object for database class
        DBClass dbobj = new DBClass();
        String result="";
        String action = request.getParameter(("action"));
        String selectedcountry = request.getParameter("country");
        String selectedstate = request.getParameter(("state"));
      try {
           if(action.equals("getStates"))
            {
             //calling database method getstates and pass country
             // as arguement
               String state = dbobj.getStates(selectedcountry);
              //dividing states using ","
               String states[]= state.split(",");
               for(int i=0;i<states.length;i++)
               {
                   //adding states for selectbox options
                 result+="<option value="+states[i]+">"+states[i]+"</option>";
               }
               //sending result to jsp page.what ever we are
            //inserting in out.println it will go back to browser(jsp page)
               //as response

               out.println(result);
              
            }
             if(action.equals("getDistricts"))
            {
               //calling database method getstates and pass selectedstate
               // as arguement
               String districts = dbobj.getDistricts(selectedstate);
               //dividing districts using ","
               String getdistricts[]= districts.split(",");
               for(int i=0;i<getdistricts.length;i++)
               {
                   //adding districts for selectbox options
                result+="<option value="+getdistricts[i]+">"+getdistricts[i]+"</option>";
               }
                //sending result to jsp page.what ever we are
    //inserting in out.println it will go back to browser(jsp page)
               //as response
               out.println(result);
               
            }
             
            }
        catch (Exception e)
        {
            out.print(e);
        }
        finally {
            out.close();
        }
    } 
    
    @Override
    protected void doGet(HttpServletRequest request, 
    HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 
    
    @Override
    protected void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
Labels:
  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.

Post a Comment