要开发一个接收HTTP请求的服务端,由于端口的限制,只能是依托于Tomcat等容器的应用。所以就想到用spring mvc接收http请求,并处理返回。
1.新建maven的web应用,配置web.xml中spring的监听及spring mvc的servlet映射。
2.配置相应的spring mvc映射文件。在web.xml中配置的mvc servlet名字为mytest,对应的映射文件就是mytest-servlet.xml
3.新建一个测试用的controller,因为用到了spring mvc的注解功能,要扫描对应的controller包,所以新建的SimpleController要在com.ajita包下面。
4.发布,部署到Tomcat下就可以了。我自己测试环境的地址为:http://localhost:8080/HttpServerTest/simpletest.do?level=555&message=ddd ,访问就可以了。
附件是demo代码,还包含了一个调用quartz进行简单任务调度的例子
1.新建maven的web应用,配置web.xml中spring的监听及spring mvc的servlet映射。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/*.xml
</param-value>
</context-param>
<!-- Filter 定义 -->
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- Filter 映射 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring容器启动监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<!-- spring mvc的servlet,加载WEB-INF/SpringSecurity-servlet.xml的配置文件,以启动Spring
MVC模块 -->
<servlet>
<servlet-name>mytest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mytest</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
2.配置相应的spring mvc映射文件。在web.xml中配置的mvc servlet名字为mytest,对应的映射文件就是mytest-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 对web包中的所有类进行扫描,以完成bean的创建和自动依赖注入功能 -->
<context:component-scan base-package="com.ajita"/>
<!-- 启动Spring mvc的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 对模型视图的名称的解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
3.新建一个测试用的controller,因为用到了spring mvc的注解功能,要扫描对应的controller包,所以新建的SimpleController要在com.ajita包下面。
@Controller
public class SimpleController {
@RequestMapping("/simpletest.do")
public void test(Model model, HttpServletRequest request,
@RequestParam(value = "level", required = false) String level,
@RequestParam(value = "message", required = false) String message,
HttpServletResponse response) {
String ret="heihei";
try {
ServletOutputStream stream = response.getOutputStream();
if(level!=null||message!=null)
ret=level + message;
stream.print(ret);
stream.close();
} catch (IOException ex) {
}
}
}
4.发布,部署到Tomcat下就可以了。我自己测试环境的地址为:http://localhost:8080/HttpServerTest/simpletest.do?level=555&message=ddd ,访问就可以了。
附件是demo代码,还包含了一个调用quartz进行简单任务调度的例子