简体   繁体   English

在jsp和servlet中设置参数

[英]Setting parameters in jsp and in servlets

In my web application , I get a request from the client. 在我的Web应用程序中,我从客户端收到请求。 I process the request in my jsp page or the servlet(which ever is called) and want to forward to other jsp's or servlets accordingly. 我在我的jsp页面或servlet(曾经被称为)中处理请求,并希望相应地转发到其他jsp或servlet。

But before forwarding I want to set a few parameters and then forward it with these new parameters. 但是在转发之前,我想设置一些参数,然后使用这些新参数转发它。 The old parameters should not be present when forwarding. 转发时,旧参数不应该出现。 Only the new parameters should be present. 仅新参数应存在。 How can this be done? 如何才能做到这一点?
Can I forward from a servlet to jsp and vice-versa? 我可以从servlet转发到jsp,反之亦然吗?
Please tell how the above can be accomplished? 请告诉您如何完成上述任务?

yes you can forward the parameter servlet to jsp and jsp to servlet. 是的,您可以将参数servlet转发到jsp,将jsp转发到servlet。

when you can set the attribute in request then it will lost on destination.means you can not access that on third resource. 当您可以在请求中设置属性时,该属性将在destination上丢失。意味着您无法在第三个资源上访问该属性。

request.setAttribute(attribute name,attribute value)

you can do same thing in session also. 您也可以在会话中执行相同的操作。

You can use request dispatcher and redirect as per your need and requirement. 您可以根据需要和要求使用请求分派器和重定向。

ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("url");

rd.forward(request,response);

or 要么

response.sendRedirect("url");

sendRedirect() sends the header back to the browser , which contains the name of the resource to be redirected to. sendRedirect()将标头发送回浏览器,该标头包含要重定向到的资源的名称。 So this will be a new request to the resource from the browser . 因此,这将是浏览器对资源的新请求

forward() action takes place within the server without the knowledge of the browser . forward()动作发生在服务器内,而浏览器却不知道。

If you have no use for the request parameters and your jsp/servlet has not written anything to the response, then I suppose it would be fine to use redirect instead of forward, since redirecting will discard the request along with the parameters. 如果您对请求参数没有用处,并且您的jsp / servlet没有向响应中写入任何内容,那么我认为使用重定向而不是转发是可以的,因为重定向会将请求和参数一起丢弃。

When you do redirect, you can create dynamically and set the querystring like so: 重定向后,您可以动态创建并设置查询字符串,如下所示:

response.sendRedirect("url?a=" + var1 +"&b=" + var2);

Take note that this will be a GET method to the url. 请注意,这将是URL的GET方法。 If url will be resolved to a servlet, you can implement the doGet method to just call the doPost. 如果将url解析为servlet,则可以实现doGet方法以仅调用doPost。

Please note that a redirect will be ignored if the jsp/servlet has written something already on the response... 请注意,如果jsp / servlet已经在响应中写了一些内容,则重定向将被忽略...

You have to forward to JSP/Servlet using RequestDisptcher. 您必须使用RequestDisptcher转发到JSP / Servlet。 Set the request attribute on the request to set parameters using 在请求上设置请求属性以使用以下参数设置参数

request.setAttribute(name, value)

The Forwarded JSP can read the parameter using 转发的JSP可以使用以下命令读取参数

request.getAttribute(name)

But, You cannot hide the attribute existing before forward by default . 但是, cannot hide the attribute existing before forward by default ,您cannot hide the attribute existing before forward by default You can achieve this using Request Wrapper. 您可以使用Request Wrapper来实现。 Wrap the request before forwarding override the set and get attribute methods. 在转发之前包装请求,以覆盖set和get属性方法。

Below code explains 以下代码说明

    RequestDisptcher dispatcher =  req.getRequestDispatcher("url");
    HideExistingRequestWrapper requestWrapper = 
                                         new HideExistingRequestWrapper(request);
    requestWrapper.setAtribute("forwarded", "forwarded value");
    dispatcher.forward(requestWrapper, response);

Here is the code of wrapper implementation: 这是包装器实现的代码:

    class HideExistingRequestWrapper extends HttpServletRequestWrapper {

           private Map localattributes = new HashMap();

           public HideExistingRequestWrapper (HttpServletRequest orignialRequest) {
                  super(orignialRequest);
           }

           public Object getAttribute(java.lang.String name) { 
                 return localattributes.get(name);
           }

           public Object setAttribute(java.lang.String name, java.lang.String value) { 
                 return localattributes.put(name, value);
           }

    }

use 采用

ServletRequest.removeAttribute(name of your old attribute)
ServletRequest.setAttribute(name , value)

After setting the attributes, get the RequestDispatcher using 设置属性后,使用以下命令获取RequestDispatcher

getRequestDispatcher(url)

and then use forward() method 然后使用forward()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM