Click here to Skip to main content
12,623,295 members (27,807 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: JSP
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
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)]

<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.

<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.

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 7-Nov-11 19:02pm
Updated 7-Nov-11 19:11pm
RaisKazi34.5K
v3
Comments
RaisKazi 8-Nov-11 0:12am
   
Edited: Formatted using "pre" tag.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

instead of using query string, use cookies...
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100