用Java模擬form表單提交的方法,在struts2中的配置如下:
創(chuàng)新互聯(lián)長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為芙蓉企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì),芙蓉網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
!-- action屬性為actionNmae!methodName的形式
其中ActionName指定提交到哪個Action,而methodName指定提交到指定方法--
action="ActionName!add"
其中一個按鈕的代碼如下:
input type="submit" value="注冊" onclick="regist();" /
點(diǎn)擊“注冊”按鈕被單擊時觸發(fā)regist函數(shù),該函數(shù)的代碼如下:
script type="text/javascript"
function regist(){
targetForm = document.forms[0];
targetForm.action = "login!add";
}
/script
實(shí)現(xiàn)代碼如下:
public class Demo {
public static void main(String[] args) throws Exception {
Map m = new HashMap();
String url = "";
String code = "GB2312";
// m.put("sel_zazhimc", "");
//
// m.put("sel_niandu", "");
//
// m.put("txt_qishiye", "");
//
// m.put("txt_doi", "");
// m.put("xueke", "");
// m.put("zhuanye", "");
// m.put("txt_zuozhe", "");
// m.put("txt_zuozhe2", "");
// m.put("txt_zuozhedw", "");
//
// m.put("txt_zhaiyao", "");
// m.put("txt_guanjianci", "");
// m.put("txt_fenleihao", "");
// m.put("sel_niandus", "");
// m.put("sel_niandue", "");
m.put("txt_wenti", "數(shù)據(jù)");
m.put("pagesize", "10");
m.put("Submit2", "查詢");
m.put("rad_px", "zuozhexm,kanchurq desc");
String rus = doPost(url, m, code);
System.out.println(rus);
}
public static String doPost(String reqUrl, Map parameters, String recvEncoding) {
HttpURLConnection conn = null;
String responseContent = null;
try {
StringBuffer params = new StringBuffer();
for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
Entry element = (Entry) iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(), recvEncoding));
params.append("");
}
if (params.length() 0) {
params = params.deleteCharAt(params.length() - 1);
}
URL url = new URL(reqUrl);
HttpURLConnection url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod("POST");
// System.setProperty("sun.net.client.defaultConnectTimeout", String
// .valueOf(HttpRequestProxy.connectTimeOut));// (單位:毫秒)jdk1.4換成這個,連接超時
// System.setProperty("sun.net.client.defaultReadTimeout", String
// .valueOf(HttpRequestProxy.readTimeOut)); // (單位:毫秒)jdk1.4換成這個,讀操作超時
url_con.setConnectTimeout(5000);//(單位:毫秒)jdk
// 1.5換成這個,連接超時
url_con.setReadTimeout(5000);//(單位:毫秒)jdk 1.5換成這個,讀操作超時
url_con.setDoOutput(true);
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in, recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return responseContent;
}
}
界面上有個東西叫form的,form里面有個按鈕類型是submit,
一般名字都叫提交,確定,查詢之類的,你按了這個按鈕后,他會自己去找form中action所對應(yīng)的selvet(這個selvet在web-inf.xml中配置好了的),selvet中再調(diào)用相關(guān)的方法,查詢出數(shù)據(jù)后,通過 request的request.setAttr...方法,數(shù)據(jù)傳遞到頁面上去,這樣你就看到了結(jié)果
其實(shí)這個是基本的mvc模式了
看你最后一句,你好像是說用j2se來發(fā)送和取得信息,也是可以的.那就要用流了,用j2ee就不用考慮他們是怎么傳的,只要知道如何傳就可以了.
文章標(biāo)題:java代碼提交表單 javapost提交
當(dāng)前地址:http://muchs.cn/article48/dohsphp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、App設(shè)計(jì)、電子商務(wù)、營銷型網(wǎng)站建設(shè)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)