Java 在jsp和servlets中设置参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9800240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting parameters in jsp and in servlets
提问by suraj
In my web application , I get a request from the client. 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.
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?
Please tell how the above can be accomplished?
在我的 Web 应用程序中,我收到了来自客户端的请求。我在我的 jsp 页面或 servlet(调用哪个)中处理请求,并希望相应地转发到其他 jsp 或 servlet。
但是在转发之前我想设置一些参数,然后用这些新参数转发它。转发时不应出现旧参数。只有新参数应该存在。如何才能做到这一点?
我可以从 servlet 转发到 jsp,反之亦然吗?
请问上面的怎么实现?
采纳答案by Sabya
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. So this will be a new requestto the resource from the browser .
sendRedirect() 将标头发送回 browser ,其中包含要重定向到的资源的名称。所以这将是浏览器对资源的新请求。
forward() action takes place within the server without the knowledge of the browser .
forward() 动作在浏览器不知情的情况下在服务器内发生。
回答by Rakesh Patel
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.
当您可以在请求中设置属性时,它将在目的地丢失。意味着您无法在第三个资源上访问该属性。
request.setAttribute(attribute name,attribute value)
you can do same thing in session also.
你也可以在会话中做同样的事情。
回答by Chandra Sekhar
use
用
ServletRequest.removeAttribute(name of your old attribute)
ServletRequest.setAttribute(name , value)
After setting the attributes, get the RequestDispatcherusing
设置属性后,使用获取RequestDispatcher
getRequestDispatcher(url)
and then use forward()method
然后使用forward()方法
回答by Ramesh PVK
You have to forward to JSP/Servlet using RequestDisptcher. Set the request attribute on the request to set parameters using
您必须使用 RequestDisptcher 转发到 JSP/Servlet。设置请求上的请求属性以使用设置参数
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
. You can achieve this using Request Wrapper. Wrap the request before forwarding override the set and get attribute methods.
但是,你cannot hide the attribute existing before forward by default
。您可以使用请求包装器来实现这一点。在转发之前包装请求覆盖 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);
}
}
回答by Carlos Jaime C. De Leon
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. If url will be resolved to a servlet, you can implement the doGet method to just call the doPost.
请注意,这将是 url 的 GET 方法。如果 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 已经在响应上写了一些东西,重定向将被忽略......