使用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
原创粉丝点击