简单是servelt实现

先写一个类继承HttpServlet=>从response对象取得输出(也就是说将流从服务器端输出到客户端浏览器);

具体的实现如下:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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

public class Test1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Test1() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String strJson = "{\"students\":[{\"name\":\"xiaobai\",\"age\":12}, {\"name\":\"xiaoming\",\"age\":23}, {\"name\":\"xiaowen\",\"age\":22}, {\"name\":\"xiaojie\",\"age\":31}]}";
        out.println(strJson);
    }
}

web.xml文件配置(服务器启动应用,将首先读取web.xml文件,从而访问web.xml对应的url就可以访问对应的servlet):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JsonServletTest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Test1</display-name>
    <servlet-name>Test1</servlet-name>
    <servlet-class>servlet.Test1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Test1</servlet-name>
    <url-pattern>/Test1</url-pattern>
  </servlet-mapping>
</web-app>

访问localhost:8080/root/Test1就可以访问对应的servlet,并且服务器端返回响应的json格式的数据

你可能感兴趣的:(EL)