2

How can I able to have the same selected item of my ddl when I clicked a submit button and redirect at the same page?

PeterS
  • 724
  • 2
  • 15
  • 31
  • Can you please give a little more information as to what you are trying to do? A little code or a screenshot may also help. Thanks – Prakash K Mar 08 '13 at 07:42
  • @PrakashK for example I have dropdown lists for filtering the data displayed...Now when I selected "all" in ddl1 and 'M' in ddl2 when I clicked search the page will refreshed. Now when the time that the page already refreshed I want to show the 'all' and 'M' as the defualt selected in my ddl and not the first list in ddl – PeterS Mar 08 '13 at 07:50

2 Answers2

0

I hope you must be sending the all of DDL1 and M of DDL2 to the server as request parameters when you click on search.

If the search takes you to your servlet to fetch the search-results or do any kind of business logic than before redirecting or forwarding to JSP which contains the DDLs you can add the all & M request parameters as request attributes, then in the JSP just check those request attributes against the <option> values.

I have assumed the following might be how you code looks like:

JSP code snipet

<select name="ddl1">
    <option value="0">all</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D">D</option>
</select>

<select name="ddl2">
    <option value="0">All</option>
    <option value="L">L</option>
    <option value="M">M</option>
    <option value="N">N</option>
    <option value="O">O</option>
</select>

Servlet code snippet

String ddl1Param = request.getParameter("ddl1");
String ddl2Param = request.getParameter("ddl2");

// your business logic
// ...

request.setAttribute("ddl1Attr", ddl1Param);
request.setAttribute("ddl2Attr", ddl2Param);

// your forward or redirect logic goes here
// ...

Now the changed JSP code would look something like this:

We have just included an <c:if> condition in each option block to check if the attribute returned is equal to the option value, if yes then the selected attribute would be placed for the option.

I am using JSP Expression Language (EL).

<select name="ddl1">
    <option value="0" <c:if test="${'0' eq ddl1Attr}">selected</c:if>>all</option>
    <option value="A" <c:if test="${'A' eq ddl1Attr}">selected</c:if>>A</option>
    <option value="B" <c:if test="${'B' eq ddl1Attr}">selected</c:if>>B</option>
    <option value="C" <c:if test="${'C' eq ddl1Attr}">selected</c:if>>C</option>
    <option value="D" <c:if test="${'D' eq ddl1Attr}">selected</c:if>>D</option>
</select>

<select name="ddl2">
    <option value="0" <c:if test="${'0' eq ddl2Attr}">selected</c:if>>all</option>
    <option value="L" <c:if test="${'L' eq ddl2Attr}">selected</c:if>>L</option>
    <option value="M" <c:if test="${'M' eq ddl2Attr}">selected</c:if>>M</option>
    <option value="N" <c:if test="${'N' eq ddl2Attr}">selected</c:if>>N</option>
    <option value="O" <c:if test="${'O' eq ddl2Attr}">selected</c:if>>O</option>
</select>

Another approach

If you are just forwarding the request on click of search to the same JSP (i.e. no servlet in between) then the JSP code can be modified as:

Either you can use param.ddl1 or param["ddl1"], it is the same thing. This EL fetches the request parameter ddl1 and ddl2.

<select name="ddl1">
    <option value="0" <c:if test="${'0' eq param.ddl1}">selected</c:if>>all</option>
    <option value="A" <c:if test="${'A' eq param.ddl1}">selected</c:if>>A</option>
    <option value="B" <c:if test="${'B' eq param.ddl1}">selected</c:if>>B</option>
    <option value="C" <c:if test="${'C' eq param.ddl1}">selected</c:if>>C</option>
    <option value="D" <c:if test="${'D' eq param.ddl1}">selected</c:if>>D</option>
</select>

<select name="ddl2">
    <option value="0" <c:if test="${'0' eq param['ddl2']}">selected</c:if>>all</option>
    <option value="L" <c:if test="${'L' eq param['ddl2']}">selected</c:if>>L</option>
    <option value="M" <c:if test="${'M' eq param['ddl2']}">selected</c:if>>M</option>
    <option value="N" <c:if test="${'N' eq param['ddl2']}">selected</c:if>>N</option>
    <option value="O" <c:if test="${'O' eq param['ddl2']}">selected</c:if>>O</option>
</select>

Another way would be to store the values in session. But I think sticking with request would be better.

Prakash K
  • 11,669
  • 6
  • 51
  • 109
0

You cannot store the data submitted from the form , because you are Redirected to the Page. The option i can think of is to store it in session and pass it around.

This SO question should help you how to set data in Session

Community
  • 1
  • 1
Sudhakar
  • 4,823
  • 2
  • 35
  • 42