如果你现在已经有一个web系统,而为了一些需求,你集成了restlet来提供Restful web service,那么如何在restlet里面获取放到HttpServletRequest和Session里面的值呢?
可以利用ServletCall类来达到目的,我们可以在Resource的init方法里面加入一些代码:
@Override
public void init(Context context, Request request, Response response) {
super.init(context, request, response);
if(request instanceof HttpRequest){
HttpServletRequest hsr = ServletCall.getRequest(request);
Enumeration params = hsr.getParameterNames();
HttpSession session = hsr.getSession();
if(session != null){
String sessFlag = (String)sess.getAttribute("sessFlag");
}
String address = hsr.getParameter("address");
}
}
同时给出ServletCall的getRequest方法的源代码,以便于加深理解:
public static HttpServletRequest getRequest(Request request) {
HttpServletRequest result = null;
if (request instanceof HttpRequest) {
final HttpCall httpCall = ((HttpRequest) request).getHttpCall();
if (httpCall instanceof ServletCall) {
result = ((ServletCall) httpCall).getRequest();
}
}
return result;
}
最后,我想说的是,使用Restlet和Servlet交互,只是假设你已经有一个web系统,或者说,你已经在这个系统上投资很多,否则,完全可以基于Restlet构建一个新的系统。看看Restlet的开发者Rhett Sutphin 在论坛对一个提问者的解答关于集成Servlet问题中提到的:
>
> My Questions:
> 1. What is the best way to integrate my servlet with the RESTLETs?
>
> 2. Is there a way for me to pass java objects from the restlet to the
> servlet? This way in the servlet I can extract java objects and not
> rely on
> re-assembling the JSON or XML contained in the Response
> Representation?
For a new application (and assuming I understand what you're trying to
do), I'd suggest using content negotation in Restlet and not using
Servlet/JSP at all.
If you really don't want to do that, or if you already have a large
investment in JSP-based views, I'd extract the business logic into a
separate layer and make it available to both your servlets and your
restlets.
分享到:
相关推荐
看名字,有需要下jar包
org.restlet.ext.servlet-2.1.1.jar
com.noelios.restlet.ext.servlet_2.4.jar
NULL 博文链接:https://ajaxcn.iteye.com/blog/438237
org.restlet.jar
Restlet所需要的所有jar包 一次下载,以后高枕无忧!
FE助手和Restlet Client .
restlet相关文档
它甚至提供一个适配器(adapter),使你可以在标准Servlet容器(如Apache Tomcat)内部署一个Restlet应用。 Restlet还提供两个HTTP客户端连接器(HTTP client connector)。它们一个是基于官方的HttpURLConnection类...
Restlet Client插件是一款运行在chrome内核浏览器上的Web服务测试插件,该插件主要用于测试各种Web服务,能查看网站基本信息、浏览网页代码并能发送HTTP请求来测试网站Web服务,同时支持自动化API场景。用户在安装了...
Restlet与Spring 集成
一个简单的Restlet开发框架的Basic认证的例子
org.restlet.ext.servlet-2.3.0.jar
sqoop-1.4.6.2.3.99.0-195.jar org.restlet-2.4.3.jar org.restlet.ext.servlet-2.4.3.jar
它甚至提供一个适配器(adapter),使你可以在标准Servlet容器(如Apache Tomcat)内部署一个Restlet应用。 Restlet还提供两个HTTP客户端连接器(HTTP client connector)。它们一个是基于官方的HttpURLConnection类...
restlet项目
restlet2.1学习笔记项目代码
restlet入门helloworld示例
本文是对Restlet官方教程http://restlet.com/learn/tutorial/2.2/的中文翻译。