Dropdown shows last selected value after refresh the page using jsp

Post date: Jul 22, 2011 4:38:13 AM

The big headache in jsp pages is that we need to explicitly maintain the state of the control when the page is refresh.

It is very easy to do with textbox and other controls, but if we perceive the state of dropdown list means it is little difficult. Since we don't know what user select before page is refresh.

We have these information in the request object of jsp. Here i wrote the code to perceive the selected dropdown value after page is refresh.

<%@page import="java.util.Iterator"%>

<%@page import="java.util.ArrayList"%>

<%@page import="java.util.List"%>

<%@ 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>

</head>

<body>

 

      <%

      //array holding the department, it will come from datbase

            List<String> listDept = new ArrayList<String>();

            listDept.add("CSE");

            listDept.add("IT");

            listDept.add("MECH");

           

            Iterator<String> itrDept = listDept.iterator();

      %>

<form>

<table>

<tr><td>Name</td><td><input type="text" name="stdName" value ="<%if(request.getParameter("stdName")!=null){ %><%=request.getParameter("stdName") %><%} %>"></td></tr>

<tr><td>Department</td>

      <td>

     

      <select name ="optDept">

      <%

      //if values in available

      while(itrDept.hasNext()){

            //get and stored in dept.

            String dept = itrDept.next();

            %>

                  <%

                  //get the request parameter

                  String options= request.getParameter("optDept");

                  //if it is not null,that mean referesh has been occured

                        if(options!=null)

                        {

                              //check that selected values

                              if(options.equalsIgnoreCase(dept))

                              {

           

                                    //make it as selected

                              %>

                              <option value="<%=dept%>" selected="selected"><%=dept%></option>

                       

                              <% }else{

                              //else add it as it is.

                              %>

                              <option value="<%=dept%>" ><%=dept%></option>

                        <%}

                        }//end of null condtion

                        else {%>

                        <option value="<%=dept%>" ><%=dept%></option>

                        <%}

      } %>

 

</select>

      </td></tr>

      <tr><td><input type="submit"></input> </td></tr>

 

 

</table>

</form>

</body>

</html>