基于jFinal建立简单的服务端-接收请求并返回指定内容
本菜鸡是一名弱弱的测试工程师,最近完成了一个支付相关的项目,项目工作中,需要建立一个模拟支付宝的网关,主要是接收请求并返回数据。作为一名没有丝毫开发经验的菜鸡,初期入门相当费劲,主要还是思维上的转变。由于本人技能水平有限,本篇文章只介绍如何启动服务,并接收请求,返回数据。希望可以给想要入门的童鞋一些帮助。
一、首先建立一个maven工程
二、web.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>demo.DemoConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三、在main中创建DemoConfig.java
继承JFinalConfig类,并实现其中的5个抽象方法。
package com.test.common; import com.jfinal.config.Constants;
import com.jfinal.config.*; /**
* Created by wangx on 2015/8/23 0023.
*/
public class DemoConfig extends JFinalConfig {
public void configConstant(Constants me){ } public void configRoute(Routes me){ } public void configPlugin(Plugins me){ } public void configInterceptor(Interceptors me){ } public void configHandler(Handlers me){ }
}
四、创建一个controller类,接收并返回数据。这里类名为DemoController.java并继承Controller
package com.test.controller; import com.jfinal.core.Controller; /**
* Created by wangx on 2015/8/23 0023.
*/
public class DemoController extends Controller{
public void index(){
renderText("学无止境!");
}
}
五、检查一下web.xml中的param-value值,应该与DemoConfig文件的路径一致,包括包名和类名。以下截图也包括了我的工程结构,可以更直观的理解各个类的关系。

六、在DemoConfig中增加路由和main函数,注意端口号不要与其他程序重复。这里设置为80,是http默认端口号
package com.test.common; import com.jfinal.config.Constants;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.test.controller.DemoController; /**
* Created by wangx on 2015/8/23 0023.
*/
public class DemoConfig extends JFinalConfig {
public void configConstant(Constants me){
me.setDevMode(true);
} public void configRoute(Routes me){
me.add("/", DemoController.class);
} public void configPlugin(Plugins me){} public void configInterceptor(Interceptors me){} public void configHandler(Handlers me){} public static void main(String[] args) throws Exception {
JFinal.start("src/main/webapp", , "/", );
}
}
七、运行main函数,控制台print如下内容,则启动成功。这时我们打开网页,访问自己PC的ip

八、返回我们制定的内容

九、设置端口和url,更改DemoConfig中的两个参数,在DemoController中新增一个方法。帮助大家理解。
DemoConfig.java
package com.test.common; import com.jfinal.config.Constants;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.test.controller.DemoController; /**
* Created by wangx on 2015/8/23 0023.
*/
public class DemoConfig extends JFinalConfig {
public void configConstant(Constants me){
me.setDevMode(true);
} public void configRoute(Routes me){
me.add("/test", DemoController.class);
} public void configPlugin(Plugins me){} public void configInterceptor(Interceptors me){} public void configHandler(Handlers me){} public static void main(String[] args) throws Exception {
JFinal.start("src/main/webapp", , "/", );
}
}
DemoController.java
package com.test.controller; import com.jfinal.core.Controller; /**
* Created by wangx on 2015/8/23 0023.
*/
public class DemoController extends Controller{
public void index(){
renderText("学无止境!");
} public void demo(){
renderText("吓死宝宝了!");
}
}
访问url:http://localhost:8080/test/demo

到此,可以启动服务,并返回数据。
十、pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>com.test</groupId>
<artifactId>jFinalDemo</artifactId>
<version>1.0</version>
<packaging>war</packaging> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>cos</artifactId>
<version>26Dec2008</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build> </project>
基于jFinal建立简单的服务端-接收请求并返回指定内容的更多相关文章
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...
- jQuery通过Ajax向PHP服务端发送请求并返回JSON数据
SON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.JSON在前后台交互的过程中发挥着相当出色的作用.请接着往下看教 ...
- c#基于supersocket的简单websocket服务端收发消息实现
using log4net; using SuperSocket.SocketBase; using SuperSocket.WebSocket; using System; using System ...
- socket编程,简单多线程服务端测试程序
socket编程,简单多线程服务端测试程序 前些天重温了MSDN关于socket编程的WSAStartup.WSACleanup.socket.closesocket.bind.listen.acce ...
- C# TCP socket发送大数据包时,接收端和发送端数据不一致 服务端接收Receive不完全
简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的TCP Socket (异步方式) C# 的tcp Socket设置自定义超时时间 C# TCP ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- Pomelo:网易开源基于 Node.js 的游戏服务端框架
Pomelo:网易开源基于 Node.js 的游戏服务端框架 https://github.com/NetEase/pomelo/wiki/Home-in-Chinese
- 基于JAX-WS的Web Service服务端/客户端 ;JAX-WS + Spring 开发webservice
一.基于JAX-WS的Web Service服务端/客户端 下面描述的是在main函数中使用JAX-WS的Web Service的方法,不是在web工程里访问,在web工程里访问,参加第二节. JAX ...
- winsock 编程(简单客户&服务端通信实现)
winsock 编程(简单客户&服务端通信实现) 双向通信:Client send message to Server, and if Server receive the message, ...
随机推荐
- RadioButtonList的使用
前台绑定: <asp:RadioButtonList ID="hlBatchYuJi" runat="server" RepeatColumns=&quo ...
- IOS主要框架介绍(转)
本文是<Sunvey the Major Framworks>一文的翻译 框架是一个目录,这个目录包含了共享库,访问共享库里代码的头文件,和其它的图片和声音的资源文件.一个共享库定义的方法 ...
- 浏览器与HTML5的相辅相成
浏览器与HTML5的相辅相成 往往一项技术的更新和发展并不是单一性的,浏览器和HTML5技术的发展亦是如此,而它们的进步也带动了整个行业的变化.浏览器与HTML5相辅相成的关系也让我们的网页能够实现更 ...
- SpringMvc 页面DATE传值问题
页面传过来yyy-MM-dd格式的日期类型,springMVC是不认的. 解决办法: @DateTimeFormat(pattern="yyyy-MM-dd") 在类属性上加上注解 ...
- Python 2x -> 3.x
Nowadays, Python 3 is becoming more and more popular than Python 2, but there are still a lot of cod ...
- jsp中查询条件的回显
后台框架为ssh,前台纯手写无框架是最老的写法,因为是接手别人的项目无法改变框架原型,只能基于修改. 进入正题: 我这里查询条件有两种input的text(文本框)和select(下拉框). 1.te ...
- qsort C++ VS2013 leetcode
class Solution { private: static int compare(const void * a, const void * b) { return (*(int*)a - *( ...
- Python3.5 用 pip 安装lxml时出现 “Unable to find vcvarsall.bat ”?(转载)
来自:https://www.zhihu.com/question/26857761 解决步骤: 1. 安装wheel,命令行运行: pip install wheel 2.在这里下载对应的.whl文 ...
- 解除Team Foundation Server 5个用户的限制
因为所有的用户必须加入到Team Foundation Licensed Users组内才能连接上TFS; 所以只要手工修改数据库,就可以破解5用户限制了.我们以TFSGuest4帐户做测试. 具体操 ...
- [PHP] - Laravel - CSRF token禁用方法
前文 CSRF攻击和漏洞的参考文章: http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html Laravel默认是开启了CSRF功能, ...