问题描述
我正在尝试编写一个简单的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页面上显示。 提前致谢。
1楼
您可以在URL中传递它,并从url中获取Java中的值
2楼
在假设您要调用RESTful服务的前提下,有多种获取输入参数的方法。 您可以参考以下文章以了解实现此目的的方法-http:
每种代码的示例都可以在获得
您可以参考的另一篇好文章是