简体   繁体   中英

communicating between php and servlet applications

I am creating a web servlet , which is presently accessed by php , using : $payload = file_get_contents(' http://localhost:8080/HelloWorldServlet/index?name=Joe&age=24 ');

This calls a HelloWorldServlet web application running on my tomcat server with a url pattern for /index. The doGet() is invoked for the servlet. The doGet() method writes the data in json, as response.. My question is how do I send back the json to php , just to display it? Also, the php application is running on port 8888.

Here is the code for doGet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //GsonBuilder builder = new ();
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();

        response.setContentType("application/json; charset=UTF-8");
        String key = request.getParameter("name");
        String value = request.getParameter("age");
        String jsonString =  gson.toJson(new Tuple(key, value)).toString();
        request.setAttribute("data", jsonString);
        //response.sendRedirect("localhost:8888/MYPHPAPPLICATION/testcall.php");
        try {

              getServletConfig().getServletContext().getRequestDispatcher(
                "/display.jsp").forward(request,response);

            } catch (ServletException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }

    }

Also, I created a filter for changing the request when it tried to forward to a php page. But it didn't work as expected.

 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String requestURI = request.getRequestURI();

        if (requestURI.contains("display.jsp"))
        {
            String toReplace = "localhost:8888/MYPHPAPPLICATION/testcall.php";
            req.getRequestDispatcher(toReplace).forward(req, res);
        } else 
            chain.doFilter(req, res);

    }

This is my web.xml

<?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" 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>HelloWorldServlet</display-name>

  <servlet>
    <description></description>
    <servlet-name>HelloWorldServlet2</servlet-name>
    <servlet-class>com.srccodes.example.HelloWorld</servlet-class>
    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>
        <servlet-name>HelloWorldServlet2</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>



  <filter>
    <filter-name>urlRewriteFilter</filter-name>
    <filter-class>com.srccodes.example.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>urlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>

Just write it to the response body and return immediately.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // ...
    String jsonString = gson.toJson(new Tuple(key, value)).toString();

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(jsonString);
}

You don't need JSP for this. It's primarily designed to act as a template for HTML output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM