使用jsp写简单的接口
来源:互联网 发布:黑金ps知乎 编辑:程序博客网 时间:2024/06/05 15:37
这里介绍用jsp写简单的接口。整个项目会作为文章的最后作为附件提供下载。数据传输是使用常用的json。在客户端发送请求时,用的是HttpClient-4.x。jar包下载如下:
点击下载
使用Eclipse新建一个动态网页项目。
命名为Http好了。
将下载的jar包复制到Http\WebContent\WEB-INF\lib下,并add to build
新建LoginPost.jsp【模拟登录,客户端发送post请求】代码如下:
<%@page import="net.sf.json.JSONObject"%><%@page import="java.io.BufferedReader"%><%@ page language="java" pageEncoding="UTF-8"%><% // 设置请求的编码为UTF-8; request.setCharacterEncoding("UTF-8"); // 设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); // 读取post请求的数据流 BufferedReader bufferedReader = request.getReader(); String string = bufferedReader.readLine(); // 逐行读取 String data = new String(); // 请求的数据 while (string != null) { data += string; // 将每行的数据加入到data中 string = bufferedReader.readLine(); } JSONObject jsonObject = new JSONObject(); try { // 将data转换为json对象 jsonObject = JSONObject.fromObject(data); // 得到用户名 String username = jsonObject.getString("username"); //得到密码 String password = jsonObject.getString("password"); //清空json对象的数据 jsonObject.clear(); // 如果用户名和密码相同,则视为登录成功 if (username.equals(password)) { jsonObject.put("result", "successed"); jsonObject.put("reason", "账号与密码相同"); } else { jsonObject.put("result", "failed"); jsonObject.put("reason", "账号与密码不相同"); } } catch (Exception e) { jsonObject.clear(); jsonObject.put("result", "failed"); jsonObject.put("reason", e.getMessage()); } // 将处理后的结果返回给客户端 out.print(jsonObject);%>
客户端请求jsp代码,HttpPost.java:
import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.methods.StringRequestEntity;import net.sf.json.JSONObject;public class HttpPost { public static void main(String[] args) { try { // 将用户名和密码放入json对象中 JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "Aiden"); jsonObject.put("password", "Aiden"); // 新建HttpClient对象,用于访问jsp HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod("http://localhost:8080/Http/LoginPost.jsp"); // 让post请求携带json数据 String string = jsonObject.toString(); RequestEntity requestEntity = new StringRequestEntity(string, "application/json", "UTF-8"); postMethod.setRequestEntity(requestEntity); // 发送post请求 httpClient.executeMethod(postMethod); // 得到响应内容 String result = new String(postMethod.getResponseBody()); System.out.println(result.trim()); } catch (Exception e) { e.printStackTrace(); } }}
一定要先让tomcat运行LoginPost.jsp,然后再执行HttpLogin代码。
运行的结果如下:
接下来介绍用jsp响应get请求的方法。
新建LoginGet.jsp,代码如下:
<%@page import="net.sf.json.JSONObject"%><%@page import="java.io.BufferedReader"%><%@ page language="java" pageEncoding="UTF-8"%><% // 设置请求的编码为UTF-8; request.setCharacterEncoding("UTF-8"); // 设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); JSONObject jsonObject = new JSONObject(); // 读取get请求的数据流 try { // 得到用户名 String username = request.getParameter("username"); // 用到密码 String password = request.getParameter("password"); // 如果用户名和密码相同,则视为登录成功 if (username.equals(password)) { jsonObject.put("result", "successed"); jsonObject.put("reason", "账号与密码相同"); } else { jsonObject.put("result", "failed"); jsonObject.put("reason", "账号与密码不相同"); } } catch (Exception e) { jsonObject.put("result", "failed"); jsonObject.put("reason", e.getCause()); } // 将处理后的结果返回给客户端 out.print(jsonObject);%>
编写HttpGet.java,模拟发送get请求
import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.GetMethod;public class HttpGet { public static void main(String[] args) { HttpClient httpClient = new HttpClient(); // 将账号密码放入路径中 GetMethod getMethod = new GetMethod("http://localhost:8080/Http/LoginGet.jsp?username=Aiden&password=Aiden"); try { httpClient.executeMethod(getMethod); String result = new String(getMethod.getResponseBody()); System.out.println(result.trim()); } catch (Exception e) { e.printStackTrace(); } }}
还是一样,要让tomcat运行LoginGet.jsp,然后再执行HttpGet代码。
运行结果如下:
整个工程下载地址:点击下载
1 0
- 使用jsp写简单的接口
- 使用jsp写简单的九九乘法表
- ATL写接口使用简单案例
- jsp的简单使用
- 接口的简单使用
- 接口的简单使用
- JSP写的一个简单计算器
- jsp写的简单购书网站
- WebService-写简单接口
- INotifyPropertyChanged接口的简单使用
- libcurl简单接口的使用
- 简单的使用API接口
- jsp简单入门及用jsp写各种java写过的小程序
- 用dubbo框架写的简单的接口作为服务端
- 用dubbo框架写的简单的接口作为客户端
- 利用javabean+jsp写的简单计算器(附加图片)
- jsp写一个简单的登录界面,有默认值
- 直接操作Framebuffer来写简单的图形接口
- spring简单概念
- 图片压缩
- 偷懒新姿势,打造属于RecyclerView的万能适配器Adapter和ViewHolder
- MyEclipse 启动不了
- USACO 2.1 frac1
- 使用jsp写简单的接口
- [poj 2417] Discrete Logging · BSGS
- 在Android Studio中的res文件夹下新建含alpha的XML文件
- hdu2795 Billboard (机智的线段树点更新)
- Yii RESTful Quick Start
- HDU 1054 Strategic Game(二分图最小覆盖集)
- android ViewPager 不带滑动效果切换item
- 【POJ 3461】Oulipo 中文题意&题解&代码(C++)
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject(2)-通过set方法或者其他方法注入