導航:首頁 > 編程語言 > javafrom提交地址參數

javafrom提交地址參數

發布時間:2025-01-11 17:04:06

❶ 怎麼把java從後台取到的數據傳到jsp頁面

這個在servlet中可是使用轉發實現參數傳遞

	protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
//TODOAuto-generatedmethodstub
request.setCharacterEncoding("UTF-8");
request.setAttribute("username","dhweicheng");
request.setAttribute("password","123456");
request.getRequestDispatcher("/my.jsp").forward(request,response);
}

<%@pagelanguage="java"contentType="text/html;charset=utf-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>頁面代碼</title>
</head>
<body>
<h5>通過request對象取值</h5>
賬號:<%=request.getAttribute("username")%><br/>
密碼:<%=request.getAttribute("password")%><br/>
<P>=======================</P>
<h5>通過EL表達式取值</h5>
賬號:${username}<br/>
密碼:${password}
</body>
</html>

❷ java HttpPost怎麼傳遞參數

public class HttpURLConnectionPost {

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

readContentFromPost();

}

public static void readContentFromPost() throws IOException {

// Post請求的url,與get不同的是不需要帶參數

URL postUrl = new URL("http://www.xxxxxxx.com");

// 打開連接

HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();

// 設置是否向connection輸出,因為這個是post請求,參數要放在

// http正文內,因此需要設為true

connection.setDoOutput(true);

// Read from the connection. Default is true.

connection.setDoInput(true);

// 默認是 GET方式

connection.setRequestMethod("POST");

// Post 請求不能使用緩存

connection.setUseCaches(false);

//設置本次連接是否自動重定向

connection.setInstanceFollowRedirects(true);

// 配置本次連接的Content-type,配置為application/x-www-form-urlencoded的

// 意思是正文是urlencoded編碼過的form參數

connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

// 連接,從postUrl.openConnection()至此的配置必須要在connect之前完成,

// 要注意的是connection.getOutputStream會隱含的進行connect。

connection.connect();

DataOutputStream out = new DataOutputStream(connection

.getOutputStream());

// 正文,正文內容其實跟get的URL中 '? '後的參數字元串一致

String content = "欄位名=" + URLEncoder.encode("字元串值", "編碼");

// DataOutputStream.writeBytes將字元串中的16位的unicode字元以8位的字元形式寫到流裡面

out.writeBytes(content);

//流用完記得關

out.flush();

out.close();

//獲取響應

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;

while ((line = reader.readLine()) != null){

System.out.println(line);

}

reader.close();

//該乾的都幹完了,記得把連接斷了

connection.disconnect();

}

(2)javafrom提交地址參數擴展閱讀:

關於Java HttpURLConnection使用

public static String sendPostValidate(String serviceUrl, String postData, String userName, String password){

PrintWriter out = null;

BufferedReader in = null;

String result = "";

try {

log.info("POST介面地址:"+serviceUrl);

URL realUrl = new URL(serviceUrl);

// 打開和URL之間的連接

URLConnection conn = realUrl.openConnection();

HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;

// 設置通用的請求屬性

httpUrlConnection.setRequestProperty("accept","*/*");

httpUrlConnection.setRequestProperty("connection", "Keep-Alive");

httpUrlConnection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

httpUrlConnection.setRequestMethod("POST");

httpUrlConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");

Base64 base64 = new Base64();

String encoded = base64.encodeToString(new String(userName+ ":" +password).getBytes());

httpUrlConnection.setRequestProperty("Authorization", "Basic "+encoded);

// 發送POST請求必須設置如下兩行

httpUrlConnection.setDoOutput(true);

httpUrlConnection.setDoInput(true);

// 獲取URLConnection對象對應的輸出流

out = new PrintWriter(new OutputStreamWriter(httpUrlConnection.getOutputStream(),"utf-8"));

// 發送請求參數

out.print(postData);

out.flush();

// 定義BufferedReader輸入流來讀取URL的響應

in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(),"utf-8"));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

//

// if (!"".equals(result)) {

// BASE64Decoder decoder = new BASE64Decoder();

// try {

// byte[] b = decoder.decodeBuffer(result);

// result = new String(b, "utf-8");

// } catch (Exception e) {

// e.printStackTrace();

// }

// }

return result;

} catch (Exception e) {

log.info("調用異常",e);

throw new RuntimeException(e);

}

//使用finally塊來關閉輸出流、輸入流

finally{

try{

if(out!=null){

out.close();

}

if(in!=null){

in.close();

}

}

catch(IOException e){

log.info("關閉流異常",e);

}

}

}

}

閱讀全文

與javafrom提交地址參數相關的資料

熱點內容
mx5系統基帶版本 瀏覽:184
ntlea全域通win10 瀏覽:171
qq怎麼查看別人的收藏 瀏覽:135
地震三參數matlab程序 瀏覽:57
怎樣給優盤文件加密軟體 瀏覽:7
收拾文件有哪些小妙招 瀏覽:431
pdf文件去底網 瀏覽:253
win10重裝系統需要格式化c盤嗎 瀏覽:424
路由器trx文件 瀏覽:655
淘寶店鋪數據包怎麼做 瀏覽:195
win10鍵盤黏連 瀏覽:332
json如何生成表格 瀏覽:323
怎麼修復sql資料庫表 瀏覽:40
微信微博差別 瀏覽:163
簽到積分換禮品app 瀏覽:812
mfc最近打開文件 瀏覽:672
app埋點平台都有哪些app 瀏覽:314
瑞斯康達網路管理界面 瀏覽:254
ca證書管理器linux 瀏覽:358
蘋果id安全提示問題3個字元 瀏覽:949

友情鏈接