如果我需要从servlet返回值到jsp

时间:2022-05-24 11:49:41

I'm totally new in jsp/servlet and working with an application that I have many jsp pages and servlet. In the first jsp page I choose a customer.

我是jsp / servlet的新手,并且使用了我有很多jsp页面和servlet的应用程序。在第一个jsp页面中,我选择了一个客户。

<select id ="sel" name="customer">
   <option>customer1</option>
   <option>customer2</option>
   <option>customer3</option>
   <option>customer4</option>
</select>

In the second jsp page it shows me the menu related to this customer(for example I choose configuration and goes to the third jsp page )

在第二个jsp页面中,它显示了与该客户相关的菜单(例如,我选择配置并转到第三个jsp页面)

<% HttpSession session = request.getSession(true);
   String chos_cust=request.getParameter("customer");
   session.setAttribute("cust_menu",chos_cust); %>

   ....
<%= session.getAttribute("cust_menu")%>

In the third jsp page I choose cateories related to this customer(there are many categories for each customer) An sql query runs here to show categories as radio buttons:

在第三个jsp页面中,我选择与该客户相关的软件(每个客户有许多类别)。此处运行SQL查询以将类别显示为单选按钮:

  <input type="radio" name="chos_gr" value="${groups.group_name}"checked > ${groups.group_name}

In the forth jsp page according to the chosen category it will show subcategories. In this jsp page I have three div in html form with hidden type.In the first div list of products has been shown , the second div is for adding a product and in the last div products can be deleted.

在根据所选类别的第四个jsp页面中,它将显示子类别。在这个jsp页面中,我有三个隐藏类型的html形式的div。在第一个div列表的产品已经显示,第二个div用于添加产品,在最后div产品可以删除。

  String config_gr = request.getParameter("chos_gr");
  session.setAttribute("config_menu",config_gr);

I have used a servlet to do these operations(add, delete). It works well but when I add or delete a product and use requestDispstcher/forward in servlet to back to the last jsp to see the result(list of products) it shows null for category and nothing in list of products.

我使用servlet来执行这些操作(添加,删除)。它运行良好但是当我添加或删除产品并使用servlet中的requestDispstcher / forward返回到最后一个jsp以查看结果(产品列表)时,它显示类别为nu​​ll而产品列表中没有任何内容。

If I should return the value of category in the servlet to the last jsp? Could someone tell me what the problem is?

如果我应该将servlet中的category类的值返回到最后一个jsp?有人能告诉我问题是什么吗?

I can't add a comment for this post. As @underdog suggested I used ${sessionScope.config_menu} in the last jsp and it still shows null for category. It's weird because I get the customer value when I return to the last jsp to see the result but nothing for category.

我无法为此帖添加评论。正如@underdog建议我在最后一个jsp中使用$ {sessionScope.config_menu}并且它仍然显示类别为nu​​ll。这很奇怪,因为当我返回到最后一个jsp以查看结果时,我获得了客户价值,但没有类别。

2 个解决方案

#1


0  

You are adding all the object/values in a session attribute. With request dispatcher you forward the flow to a jsp with request & response objects. The request object contains no info of the customer you have set in the session object.

您正在添加会话属性中的所有对象/值。使用请求调度程序,您可以将流转发到带有请求和响应对象的jsp。请求对象不包含您在会话对象中设置的客户的信息。

In the jsp try accessing the values from the session scope. ${sessionScope.config_menu} would give you the desired output.

在jsp中尝试访问会话范围中的值。 $ {sessionScope.config_menu}会为您提供所需的输出。

#2


-1  

Instead of using a response.sendRedirect(url), consider using following:

而不是使用response.sendRedirect(url),请考虑使用以下内容:

RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request,response); 

Above code with the getRequestDispatcher() will allow you to include your calling servlet's request by using dispatcher.forward, which will include the session attributes you were missing.

上面的代码使用getRequestDispatcher()将允许您使用dispatcher.forward包含调用servlet的请求,其中包括您缺少的会话属性。

[NOTE: OP was already doing the above]

[注意:OP已经在做了以上]

[UPDATE]

Based on the way you were suggesting to code certain parameters in <input type="hidden" name="myfield"> fields, I suspect, you did nothing else to store the values. Which means, the first time around, they might have been set using the parameters from the previous request, but when using the dispatcher.forward, this did not happen.

基于您建议在字段中编码某些参数的方式,我怀疑,您没有做任何其他事情来存储值。这意味着,第一次,它们可能是使用前一个请求中的参数设置的,但是在使用dispatcher.forward时,这并没有发生。

For fields to be initiated with the appropriate value, they require to be made available either through request parameters (e.g. GET parameters, encoded in the URL), or otherwise set within the JSP itself trough <input type="hidden" value="${whatever}" name="myfield"> or similar code. I am also missing that part of the code to be able to validate.

对于要使用适当值启动的字段,它们需要通过请求参数(例如,GET参数,在URL中编码)提供,或者通过或类似代码。我也错过了能够验证的部分代码。

Using a dispatcher.forward(), you might not have required parameters set.

使用dispatcher.forward(),您可能没有设置所需的参数。

#1


0  

You are adding all the object/values in a session attribute. With request dispatcher you forward the flow to a jsp with request & response objects. The request object contains no info of the customer you have set in the session object.

您正在添加会话属性中的所有对象/值。使用请求调度程序,您可以将流转发到带有请求和响应对象的jsp。请求对象不包含您在会话对象中设置的客户的信息。

In the jsp try accessing the values from the session scope. ${sessionScope.config_menu} would give you the desired output.

在jsp中尝试访问会话范围中的值。 $ {sessionScope.config_menu}会为您提供所需的输出。

#2


-1  

Instead of using a response.sendRedirect(url), consider using following:

而不是使用response.sendRedirect(url),请考虑使用以下内容:

RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request,response); 

Above code with the getRequestDispatcher() will allow you to include your calling servlet's request by using dispatcher.forward, which will include the session attributes you were missing.

上面的代码使用getRequestDispatcher()将允许您使用dispatcher.forward包含调用servlet的请求,其中包括您缺少的会话属性。

[NOTE: OP was already doing the above]

[注意:OP已经在做了以上]

[UPDATE]

Based on the way you were suggesting to code certain parameters in <input type="hidden" name="myfield"> fields, I suspect, you did nothing else to store the values. Which means, the first time around, they might have been set using the parameters from the previous request, but when using the dispatcher.forward, this did not happen.

基于您建议在字段中编码某些参数的方式,我怀疑,您没有做任何其他事情来存储值。这意味着,第一次,它们可能是使用前一个请求中的参数设置的,但是在使用dispatcher.forward时,这并没有发生。

For fields to be initiated with the appropriate value, they require to be made available either through request parameters (e.g. GET parameters, encoded in the URL), or otherwise set within the JSP itself trough <input type="hidden" value="${whatever}" name="myfield"> or similar code. I am also missing that part of the code to be able to validate.

对于要使用适当值启动的字段,它们需要通过请求参数(例如,GET参数,在URL中编码)提供,或者通过或类似代码。我也错过了能够验证的部分代码。

Using a dispatcher.forward(), you might not have required parameters set.

使用dispatcher.forward(),您可能没有设置所需的参数。