【JAVA】使用servlet实现简单接口返回给前端json数据

gengboxb 932 0

1、创建web工程

2、创建基本文件目录

【JAVA】使用servlet实现简单接口返回给前端json数据

3、HttpServer.java

package com.gengbo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// 定义接口
public class HttpServer extends HttpServlet {
    /*
    * get请求,通用的写法
    * */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        System.out.println("get-----");
        String user = req.getParameter("user"); // 获取key-value的值 /HttpServer?user=123
        System.out.println(user);
        resp.setCharacterEncoding("UTF-8"); // 设置编码格式
        resp.setContentType("application/json; charset=utf-8"); // 设置响应头
        String jsonStr = "{\"name\":\"lgb\",\"type\":\"你好java\"}"; // 定义对象数据
        PrintWriter out = null; // 定义流
        try {
            out = resp.getWriter(); // 获取输出流
            out.write(jsonStr);  // 写入数据
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close(); // 关闭流
            }
        }
    }

    /*
    * post请求
    * */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       super.doPost(req, resp);
        resp.setCharacterEncoding("UTF-8"); // 设置编码格式
        resp.setContentType("application/json; charset=utf-8"); // 设置响应头
        System.out.println("----------post");
    }
}

4、Serverlet.java

package com.gengbo.servlet;

import javax.servlet.*;
import java.io.IOException;

public class Serverlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("hello word");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

5、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
           version="4.0">
     <!-- 定义Serverlets接口 -->
    <servlet>
        <servlet-name>Serverlet</servlet-name>
        <servlet-class>com.gengbo.servlet.Serverlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Serverlet</servlet-name>
        <url-pattern>/Serverlets</url-pattern>
    </servlet-mapping>

    <!-- 定义HttpServer接口 -->
    <servlet>
        <servlet-name>HttpServer</servlet-name>
        <servlet-class>com.gengbo.servlet.HttpServer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HttpServer</servlet-name>
        <url-pattern>/HttpServer</url-pattern>
    </servlet-mapping>
</web-app>

发表评论 取消回复
表情 图片 链接 代码

分享