Click here to Skip to main content
15,313,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I am unable to pass certain values from servlet to my jsp. I have used the request dispatcher in my code but it is not passing any value to the jsp, as in my jsp I am getting it as null.

Below is my code. I need to pass 3 values from servlet to jsp, out of which the first value is getting sent but the 2nd and 3rd values are not.

JSP file
Java
Category
<select id="cat_sel" name="quizname"   önChange="select(); this.form.submit()">

[explanation-select() is a javascript where the value selected in this combo-box is returned and using form.submit() is passed to the servlet(the form action is given to the servlet)]

Java
<option selected>Make a selection</option>
<%
for(Quiz q : quiz)
{	
%>
<Option value="<%=q.getCategory()%>"> <%=q.getCategory()%> </Option>
<%	
} %>
<% String cat=(String)request.getAttribute("category");
if(cat!=null)
{ %>
	<Option selected="yes"> <%=cat%> </Option>
<%}

%>	
</select>
Based on the category a sub-category is selected.

Java
<select id="subcat_sel" name="subcategory"  önChange="subSelect(); this.form.submit()">
<option selected>Make a selection</option>
<%

for(Quiz q : quiz)
{
	if(q.getCategory().equals(cat))
	{
%>
<Option value="<%=q.getSubCategory()%>"><%=q.getSubCategory()%></Option>
<%	
	}//if
}//for
String subcat=(String)request.getAttribute("subcategory");
System.out.println("Subcat in jsp="+subcat); //here it is printed as null
if(subcat!=null)
{ %>
	<Option selected="yes"> <%=subcat%> </Option>
<%}
%>
</select>
Similar code for selecting level so I am not adding it here.
Below is the servlet code.

Java
String category=req.getParameter("quizname");
String sub_category=req.getParameter("subcategory");
// some code here
else if(category!=null)
	{
		System.out.println("Check servcat="+category);//value selected in jsp is printed here
		updatecategory(category,email_address);
    		req.setAttribute("category",category);
		req.getRequestDispatcher("/userquiz").forward(req, resp); //userquiz  is the jsp file.
		resp.sendRedirect("/userquiz");
	}
	else if(sub_category!=null)
	{
		System.out.println("Check servsubcat="+sub_category);
    	updatesubcategory(sub_category,email_address);
		req.setAttribute("subcategory",sub_category);
		req.getRequestDispatcher("/userquiz").include(req, resp);
		resp.sendRedirect("/userquiz");
	}

Please guys help me out.

I will really appreciate it.
Posted
Updated 7-Nov-11 18:11pm
v3
Comments
[no name] 8-Nov-11 0:12am
   
Edited: Formatted using "pre" tag.

1 solution

instead of using query string, use cookies...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900