从Applet调用Spring控制器

时间:2014-01-14 16:49:07

标签: spring-mvc applet communication

我正在尝试从applet中调用Spring控制器中的方法。

这是applet中的代码:

//Create connection with servlet
URL servletURL = null;
URLConnection servletConnect = null;
try {
    servletURL = new URL("http://localhost:8080/communication");
    try {
        servletConnect = servletURL.openConnection();
        servletConnect.setDoOutput(true); // to allow us to write to the URL
        servletConnect.setUseCaches(false); // Write the message to the servlet and not from the browser's cache
        servletConnect.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        servletConnect.connect();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
} catch (MalformedURLException e1) {
    e1.printStackTrace();
}

//Write bean to servlet
Communication comm = new Communication();
comm.setValue("test");
ObjectOutputStream outputToServlet = null;
try {
    outputToServlet = new ObjectOutputStream(servletConnect.getOutputStream());
    outputToServlet.writeObject(comm);
    outputToServlet.flush(); //Cleanup
    outputToServlet.close();
} catch (IOException e1) {
    e1.printStackTrace();
}

这是控制器中的代码:

@RequestMapping("/communication")
public void receiveCall(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map, ModelMap model) 
        throws ServletException, IOException, ClassNotFoundException {
    ObjectInputStream inputFromApplet = new ObjectInputStream(request.getInputStream());
    Communication myObject = (Communication) inputFromApplet.readObject();
    ...
}

调用永远不会到达servlet。

这就是我在Java控制台中的内容:

network: Connecting http://localhost:8080/communication with proxy=DIRECT
network: Connecting http://localhost:8080/ with proxy=DIRECT

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

Please do below steps :-

1. within your web.xml

  <servlet>
    <servlet-name>atozDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>  

  <servlet>
    <servlet-name>AtozServlet</servlet-name>
    <servlet-class>com.atoz.rnd.controller.AtozServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet>
    <servlet-name>AtozServletTest</servlet-name>
    <servlet-class>com.atoz.rnd.controller.AtozServletTest</servlet-class>
    <load-on-startup>3</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>AtozServlet</servlet-name>
    <url-pattern>/atozServlet</url-pattern>
  </servlet-mapping>

 <servlet-mapping>
    <servlet-name>AtozServletTest</servlet-name>
    <url-pattern>/atozServletTest</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>atozDispatcherServlet</servlet-name>
    <url-pattern>/neoAction</url-pattern>
  </servlet-mapping>


  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/atozDispatcherServlet-servlet.xml</param-value>
  </context-param>


2. Spring controller

package com.atoz.rnd.controller;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping(value="neoAction")
public class AtozAction {

    @RequestMapping( method = RequestMethod.POST)
     public void execute(HttpServletRequest request, HttpServletResponse response ) {

        try {
            response.setContentType("application/x-java-serialized-object");
            InputStream in = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(in);
            String echo = (String) inputFromApplet.readObject();

            // echo it to the applet
            OutputStream outstr = response.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstr);
            oos.writeObject(echo);
            oos.flush();
            oos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

3. Applet code snippet


    /**
     * Get a connection to the servlet.
     */
    private URLConnection getServletConnection()
        throws MalformedURLException, IOException {

        // Connection zum Servlet öffnen
        URL urlServlet = new URL(getCodeBase(),"neoAction");
        URLConnection con = urlServlet.openConnection();

        // konfigurieren
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestProperty(
            "Content-Type",
            "application/x-java-serialized-object");

        // und zurückliefern

        return con;
    }

    /**
     * Send the inputField data to the servlet and show the result in the outputField.
     */
    private void onSendData() {
        try {
            // get input data for sending
            String input = inputField.getText();

            // send data to the servlet
            URLConnection con = getServletConnection();
            OutputStream outstream = con.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstream);
            oos.writeObject(input);
            oos.flush();
            oos.close();

            // receive result from servlet
            InputStream instr = con.getInputStream();
            ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
            String result = (String) inputFromServlet.readObject();
            inputFromServlet.close();
            instr.close();

            // show result
            outputField.setText(result);

        } catch (Exception ex) {
            ex.printStackTrace();
            exceptionArea.setText(ex.toString());
        }
    }

4. Spring context file 


    <context:component-scan base-package="com.atoz.rnd" />  
    <mvc:annotation-driven />