要开发一个接收HTTP请求的服务端,由于端口的限制,只能是依托于Tomcat等容器的应用。所以就想到用spring mvc接收http请求,并处理返回。
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进行简单任务调度的例子
分享到:
相关推荐
主要给大家介绍了关于Spring MVC请求参数与响应结果全局加密和解密的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController 的程序进行处理, helloController 再调用 一个名为 ...
在服务器中请求被Controller接收,Controller调用相应的Model层处理请求,处理完毕后结果返回到Controller。 Controller再根据请求处理的结果找到对应的View视图,渲染数据后最终响应给浏览器。 此外,Spring MVC还...
在Spring MVC中,DispatcherServlet作为前端控制器,负责请求的接收和响应结果的处理。处理器映射器(HandlerMapping)和处理器适配器(HandlerAdapter)是核心组件,负责找到和调用适当的处理器(Controller)。 ...
慨括了spring mvc请求参数常见的几种获取方法
基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController 的程序进行处理, helloController 再调用 一个名为 ...
Spring MVC框架围绕DispatcherServlet这个核心展开,DispatcherServlet的作用是截获请求并组织一系列组件共同完成请求的处理工作。 JavaServer Faces (JSF) 是一种用于构建 Web 应用程序的新标准 Java 框架。它...
介绍SpringMVC如何接收请求,和接收请求的方式,以及@RequsetMapping的使用
Spring MVC是Spring提供的构建Web应用程序的框架,该框架遵循了Servlet规范,负责接收并处理Servelt容器传递的请求,并将响应写回Response。Spring MVC以DispatcherServlet为核心,众多组件如HandlerMapping为辅助,...
使用Spring MVC或Spring Boot中打印或记录日志一般使用AOP记录Request请求和Response响应参数,在不使用AOP的前提下,如果在Filter中打印日志,在打印或消费请求类型为Content-Type:application/json的请求时,会...
将应用程序分为 Controller、Model、View 三层,Controller 接收客户端请求,调用 ...Spring MVC 就是对这套流程的封装,屏蔽了很多底层代码,开放出接口,让开发者可以更加轻松、便捷地完成基于 MVC 模式的 Web 开发。
主要给大家总结介绍了关于Spring MVC请求参数接收的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
该工程是一个基于maven的 spring mvc demo工程,其是...具有如下功能:可接收REST请求,能操作数据库、基于注解、统一返回、统一异常处理。对于初学者可用于熟悉如何搭建工程,对于成手可将该工程快速应用于自己的项目
NULL 博文链接:https://songdi5403.iteye.com/blog/2344273
Spring MVC下彻底解决@ResponseBody转义html特殊字符问题,解决包括尖角号(),单引号('),双引号("),按位与(&),反斜杠(\)。
Spring MVC采用了模型-视图-控制器(MVC)的设计模式,通过DispatcherServlet来接收和处理HTTP请求,将请求路由到相应的控制器进行处理,并生成响应返回给客户端。 MySQL数据库:作为一个常用的关系型数据库管理...
模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式。它主要通过分离模型、视图及控制器在应用程序中的...控制器负责接收来自用户的请求,并调用后台服务(manager或者dao)来处理业务逻辑
它基于经典的 MVC 设计模式,通过将应用程序分为模型(Model)、视图(View)和控制器(Controller)三个部分,实现了业务逻辑、用户界面和请求处理的分离。 下面是 Spring MVC 的核心组件和工作流程: 模型...
本demo 用来写了springmvc如何接受ajax传集合以及接收form表单传集合