如何在java中获取Web服务的输入

时间:2015-07-27 16:21:46

标签: java php apache web-services input

我正在尝试编写一个简单的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);
    }
}

}

我不知道如何获取上述Web服务的输入。我有一个html页面,有一个文本框和提交按钮,我通过PHP代码获取值。我想将此数字作为输入隧道传输到我的Web服务。谁能告诉我怎么办? 此外,我希望输出String []返回到PHP代码,以便它可以显示在html页面上。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

假设您要调用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/

相关问题