从applet调用spring控制器方法

时间:2014-11-18 13:16:47

标签: spring applet

我有一个applet,其swing键需要将图像保存到数据库中。我们已经使用spring的Dispatcher servlet来集成flex和spring。所以我只需要调用一个现有控制器的方法, 我的applet servlet的web.xml

 <servlet>
     <servlet-name>applettoservlet</servlet-name>
     <servlet-class>AppletToServlet</servlet-class>
     <load-on-startup>2</load-on-startup>
  </servlet>

    <servlet-mapping>
        <servlet-name>applettoservlet</servlet-name>
        <url-pattern>/jas/applettoservlet/*</url-pattern>
    </servlet-mapping>  

我在包含html的本地文件夹中部署了applet jar,但是我发现了FileNot异常,我觉得web.xml servlet映射存在问题,因为在容器启动时没有创建servlet。

我的Servlet代码:

public class AppletToServlet扩展了HttpServlet {

private static final long serialVersionUID = 1L;

// Getting a String object from the applet and send it back.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("---------AppletToServlet------doPost-");
    try {
        response.setContentType("application/x-java-serialized-object");
        InputStream inputStream = request.getInputStream();
        ObjectInputStream inputFromApplet = new ObjectInputStream(inputStream);
        String string = (String) inputFromApplet.readObject();
        // getting string value and passing to applet
        OutputStream outputStream = response.getOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(string);
        objectOutputStream.flush();
        objectOutputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

    Html :

         

        <body>

    <h1>Applet to Servlet Test</h1> 
      <applet width=300 height=100 MAYSCRIPT code="AppletToServletExample"        archive="app2serv.jar"> </applet>
    </body>
</html>

Applet code :

public class AppletToServletExample extends Applet {

private static final long serialVersionUID = 1L;
private TextField inputField = new TextField(10);
private TextField resultField = new TextField(10);

public void init() {
    // add input label, field and send button
    add(new Label("Input Your Name", Label.RIGHT));
    add(inputField);
    Button sendButton = new Button("Send");
    add(sendButton);
    sendButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            sendData();
        }
    });
    // add output label as a non editable field
    add(new Label("Output:", Label.RIGHT));
    add(resultField);
    resultField.setEditable(false);
}

// Get a connection to the servlet.
private URLConnection getServletConnection() throws MalformedURLException,
IOException {
    //URL urlServlet = new URL(getCodeBase(), "jas/applettoservlet");
    URL urlServlet = new URL("http://localhost:29801/nba/secure/jas/applettoservlet");
    URLConnection con = urlServlet.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type",
            "application/x-java-serialized-object");
    return con;
}
// Send the inputField data to the servlet and show the result in the
// outputField.
private void sendData() {
    try {
        String input = inputField.getText();
        // send data to the servlet
        URLConnection con = getServletConnection();
        OutputStream outputStream = con.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outputStream);
        oos.writeObject(input);
        oos.flush();
        oos.close();
        // receive result from servlet
        InputStream inputStream = con.getInputStream();
        ObjectInputStream inputFromServlet = new ObjectInputStream(
                inputStream);
        String result = (String) inputFromServlet.readObject();
        inputFromServlet.close();
        inputStream.close();
        // show result
        resultField.setText(result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

我的应用程序上下文文件:                   在上面的包是我现有的控制器,好像春天没有映射网址..我错过了什么?

0 个答案:

没有答案