【问题标题】:How to take input for a web service in java如何在java中为Web服务获取输入
【发布时间】:2015-07-27 16:21:45
【问题描述】:

我正在尝试编写一个简单的 Web 服务,它必须将数字作为输入并返回与之对应的详细信息。

这是我到目前为止编写的代码。

package webserviceapp;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.jws.WebService;
import javax.jws.WebMethod;

@WebService
public class WebServiceApp {

/**
 * @param args the command line arguments
 */
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:mysql://10.100.66.28:3306/dbname";

//  Database credentials
static final String USER = "user";
static final String PASS = "pass";

static Map<Integer, String> map = new HashMap<Integer, String>();

public static void main(String[] args) {
    // TODO code application logic here
    Connection conn;
Statement stmt;

try{
        Class.forName(JDBC_DRIVER);

        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        stmt = (Statement) conn.createStatement();

        String sql = "Select * from table";
        ResultSet rs;
        rs = stmt.executeQuery(sql);

        while(rs.next()){
            //do something
        }

}catch(ClassNotFoundException | SQLException e){
        System.out.println(e);
}



}

@WebMethod(action = "returnDetails")
public static String[] returnDetails(int k) throws notFoundException{
    //do the work
    //returns String[]
}

private static class notFoundException extends Exception {

    public notFoundException(String s) {
        System.out.println(s);
        System.err.println(s);
    }
}

}

我不知道如何接受上述网络服务的输入。我有一个 html 页面,它有一个文本框和提交按钮,我通过 php 代码获取值。我想通过隧道将此号码作为我的 Web 服务的输入。谁能告诉我我该如何继续。 另外,我希望将输出 String[] 返回到 php 代码,以便它可以显示在 html 页面上。 提前致谢。

【问题讨论】:

    标签: java php apache web-services input


    【解决方案1】:

    你可以在 URL 中传递它,你可以从 url 中获取 java 中的值

    【讨论】:

    【解决方案2】:

    假设您希望调用 RESTful 服务,有多种获取输入参数的方法。您可以参考以下文章了解实现此目的的方法 - http://www.java4s.com/web-services/how-restful-web-services-extract-input-parameters

    http://www.java4s.com/web-services/ 提供每个代码示例

    另一篇可以参考的好文章是-https://vrsbrazil.wordpress.com/2013/08/07/passing-parameters-to-a-restful-web-service/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      相关资源
      最近更新 更多