`
sillycat
  • 浏览: 2543734 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring3 with Servlets

    博客分类:
  • JAVA
 
阅读更多
Spring3 with Servlets

Actually, it is not Servlets. It is simple servlet. It is org.springframework.web.HttpRequestHandler.

The sample project is easyoauthprovider. I will put some core codes and configuration here.

Spring configuration files:
servlet-context.xml
<bean id="RequestTokenServlet"  class="com.sillycat.easyoauthprovider.servlets.RequestTokenHttpRequestHandler" >
<property name="oauthProvider" ref="oauthProvider" />
<property name="oauthValidator" ref="oauthValidator" />
</bean>

The related oauthProvider and oauthValidator is configured in spring bean xml files.

The web configuration file web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:main-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
        <servlet-name>RequestTokenServlet</servlet-name>
        <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>RequestTokenServlet</servlet-name>
<url-pattern>/request_token</url-pattern>
</servlet-mapping>


The JAVA class RequestTokenHttpRequestHandler.java is as follow:
package com.sillycat.easyoauthprovider.servlets;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.oauth.OAuth;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;
import net.oauth.OAuthValidator;
import net.oauth.server.OAuthServlet;

import org.springframework.web.HttpRequestHandler;

import com.sillycat.easyoauthprovider.plugins.oauth.OAuthProvider;

public class RequestTokenHttpRequestHandler implements HttpRequestHandler {

private OAuthProvider oauthProvider;

private OAuthValidator oauthValidator;

public void setOauthProvider(OAuthProvider oauthProvider) {
this.oauthProvider = oauthProvider;
}

public void setOauthValidator(OAuthValidator oauthValidator) {
this.oauthValidator = oauthValidator;
}

public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

public void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {

try {
OAuthMessage requestMessage = OAuthServlet
.getMessage(request, null);

OAuthConsumer consumer = oauthProvider.getConsumer(requestMessage);

OAuthAccessor accessor = new OAuthAccessor(consumer);
oauthValidator.validateMessage(requestMessage, accessor);
{
// Support the 'Variable ACCESSOR Secret' extension
// described in http://oauth.pbwiki.com/AccessorSecret
String secret = requestMessage
.getParameter("oauth_accessor_secret");
if (secret != null) {
accessor.setProperty(OAuthConsumer.ACCESSOR_SECRET, secret);
}
}
// generate request_token and secret
oauthProvider.generateRequestToken(accessor);

response.setContentType("text/plain");
OutputStream out = response.getOutputStream();
OAuth.formEncode(OAuth.newList("oauth_token",
accessor.requestToken, "oauth_token_secret",
accessor.tokenSecret), out);
out.close();

} catch (Exception e) {
e.printStackTrace();
}

}

}

Frankly speaking, it is part of the oauth sample project. I am still working on this.

references:
http://andykayley.blogspot.com/2007/11/how-to-inject-spring-beans-into.html
http://andykayley.blogspot.com/2008/06/how-to-inject-spring-beans-into.html

分享到:
评论

相关推荐

    spring-boot-reference.pdf

    4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. Getting Started 8. Introducing Spring Boot 9. System Requirements 9.1. Servlet ...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    14.1.2. Using JSR-250’s @RolesAllowed with Spring Security 14.2. Using expressions for method-level security 14.2.1. Expressing method access rules 14.2.2. Filtering method inputs and outputs 14.3. ...

    Designing Enterprise Applications with the J2EE Platform, Second Edition

    例如,Java EE (现称Jakarta EE)逐渐转向更加轻量级、模块化的框架,如Spring Boot,同时保留了其核心优势,为企业级应用提供了更为灵活的选择。 #### 六、总结 《使用J2EE平台设计企业应用》第二版是一本全面介绍...

    Java EE Development with Eclipse.pdf

    4. **常用技术和框架**:重点介绍了在Java EE开发中常用的多种技术和框架,如Servlets、JSP、EJB、Spring、Hibernate等。 5. **最佳实践**:分享了一些开发Java EE应用程序的最佳实践和技术建议,帮助开发者提高开发...

    Guide.to.Web.Development.with.Java.Understanding.Website.Creation

    学习Spring的基本概念及其主要模块(如Spring MVC、Spring Security等)是很有价值的。 - **Struts2**:虽然不如以前那么流行,但Struts2仍然是一种强大的Web框架,特别是在大型企业项目中。 ##### 3.6 测试与调试 ...

    java视频教程Day01 免费

    18. Forms Processing with Servlets(Servlet表单处理) 19. Servlets Life Cycle (Servlet生命周期) 20. Resources Accessing(Servlet资源获取) 21. Persistent States in HttpServlets(Servlet状态持久...

    Geting started with Equinox and OSGi

    Equinox是所有Eclipse系统的基础,从嵌入式航空自助登机亭、滑雪场闸门到丰富的客户端应用、集成开发环境(IDE),甚至高性能应用服务器如WebSphere和Spring dm server等,都有它的身影。 2. **创建第一个Bundle**...

    Addison - Developing Enterprise Java Applications with J2EE and UML.pdf

    这一章节介绍Java Servlet技术,探讨它们在UML中的模型表示,以及如何在案例研究中实际应用UML和Servlets。Servlets非常适合基于请求-响应的Web应用模式。 ### 第十一章:JavaServer Pages (JSP) 本章深入讲解JSP...

    Apache Tomcat 7

    Turning to technology, Aleksa has been involved with the Spring Framework since early days, becoming an expert in enterprise Java development with Spring, along with other open-source technologies....

    learning rabbitmq

    His areas of interest include a wide range of Java-related technologies (Servlets, JSP, JAXB, JAXP, JMS, JMX, JAX-RS, JAX-WS, Hibernate, Spring Framework, Liferay Portal, and Eclipse RCP), cloud ...

    基于ssm+mysql的喀什古城旅游网源码数据库论文.doc

    2. **Spring Framework (SSM)**: Spring forms the core of the 'S' in SSM, which stands for Spring, Servlets, and MyBatis. Spring is a comprehensive framework for building enterprise-level applications, ...

    Guide To Web Development With Java

    4. **框架与库**:Java Web开发中有许多优秀的框架和库,如Spring、Struts等,它们为开发者提供了丰富的功能和便利性,帮助开发者快速构建复杂的应用程序。 #### 四、Java Web开发的最佳实践 1. **代码组织**:...

    XDoclet in Action

    - **Integration with Frameworks**: Examples of integrating XDoclet with popular web frameworks like Struts and Spring. #### Chapter 5: XDoclet and Web Frameworks Building on the previous chapter, ...

    java上传及文件读取案例

    这个过程可以通过Servlets或者现代的Web框架如Spring MVC来实现。 1. **文件上传**: - **Servlets**:在传统的Java Web开发中,Servlets可以处理HTTP请求,包括文件上传。使用`HttpServletRequest`对象的`getPart...

    How Tomcat Works 中文版.pdf

    对于不熟悉这些内容的读者,作者也推荐了其他书籍作为参考,比如《Java for the Web with Servlets, JSP, and EJB》。 对于那些希望深入理解servlet容器工作原理的Java技术开发人员、Tomcat用户以及有志于参与...

Global site tag (gtag.js) - Google Analytics