问题描述
你好朋友,
我无法将某些值从servlet传递到我的jsp.我已经在代码中使用了请求分配器,但是它没有将任何值传递给jsp,因为在我的jsp中,我将其获取为null.
下面是我的代码.我需要将3个值从servlet传递到jsp,从中发送第一个值,但不发送第2个和第3个值.
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()是一个JavaScript,在其中返回在此组合框中选择的值,并使用form.submit()传递给servlet(将表单操作交给servlet)]
[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>
用于选择级别的类似代码,因此我不在此处添加.
下面是servlet代码.
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.
推荐答案
这篇关于将值从Servlet传递到JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!