Servlet接收Post请求以及回复请求

本文主要介绍了Servlet如何接受HttpCilent发送过来的请求以及对请求进行回复

Servlet需要用到Servlet-api.jar包

package com.firstdata.project;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        String origRequestMsg = null;
        byte[] reqBuf = null;
        try {
            InputStream is = req.getInputStream();
            reqBuf = readInputBytes(is);
            origRequestMsg = new String(reqBuf, "GBK");
            System.out.println(origRequestMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 出来完收到信息之后可以就可以回复请求了

        try {
            OutputStream os = res.getOutputStream();
            String resMsgOut = "this is response message";
            os.write(resMsgOut.getBytes("GBK"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * reads input stream passed and populates bytes
     * 
     * @param is
     * InputStream
     * @return byte[]
     * @throws IOException
     */
    private byte[] readInputBytes(InputStream is) throws IOException {
        ArrayList<ByteCountObj> inputByteList = new ArrayList<ByteCountObj>();
        byte[] inBuf = new byte[2000];
        int coutRead = is.read(inBuf);
        while (coutRead != -1) {
            ByteCountObj obj = new ByteCountObj(inBuf, coutRead);
            inputByteList.add(obj);
            inBuf = new byte[2000];
            coutRead = is.read(inBuf);
        }

        coutRead = 0;
        ListIterator<ByteCountObj> it = inputByteList.listIterator();
        while (it.hasNext()) {
            ByteCountObj ob = it.next();
            coutRead += ob.getByteCount();
        }

        ByteBuffer byteBuf = ByteBuffer.allocate(coutRead);
        it = inputByteList.listIterator();
        while (it.hasNext()) {
            ByteCountObj ob = it.next();
            coutRead = ob.getByteCount();
            inBuf = ob.getByteBuf();
            byteBuf.put(inBuf, 0, coutRead);
        }
        if (byteBuf.hasArray()) {
            return byteBuf.array();
        } else {
            return null;
        }
    }

    /**
     * 用来储存获取到的byte[]以及字节流的长度
     * 
     * @author
     * 
     */
    public class ByteCountObj {

        private byte[] byteBuf;

        private int byteCount;

        public byte[] getByteBuf() {
            return byteBuf;
        }

        public int getByteCount() {
            return byteCount;
        }

        public ByteCountObj(byte[] byteBuf, int byteCount) {
            this.byteBuf = byteBuf;
            this.byteCount = byteCount;
        }
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用Servlet或者Spring MVC来接收POST请求。以下是一个简单的Servlet代码示例: ```java import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PostServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取POST请求中的参数 String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); // 处理POST请求 // ... // 返回响应 response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("success"); } } ``` 在上述代码中,我们通过重写doPost方法来处理POST请求,使用request.getParameter方法来获取POST请求中的参数,然后进行处理,最后通过response.getWriter().write方法将处理结果返回给客户端。 另外,如果你使用Spring MVC框架,可以使用@RequestBody注解来获取POST请求中的参数,如下所示: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller public class PostController { @RequestMapping(value="/post", method=RequestMethod.POST) @ResponseBody public String doPost(@RequestBody String requestBody) { // 处理POST请求 // ... // 返回响应 return "success"; } } ``` 在上述代码中,我们使用了@Controller和@RequestMapping注解来定义一个控制器,使用@RequestBody注解来获取POST请求中的参数,然后进行处理,最后通过@ResponseBody注解将处理结果返回给客户端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值