Ⅰ Servlet怎麼得到HTTP POST過來的報頭信息
頭部信息已經封裝到了request對象中,可以直接獲取。
String value = request.getHeader("HTTP頭中key值");
Enumeration e = request.getHeaderNames(); //獲取所有的key
容器會將信息封裝到request,並且做了合理的拆分和處理,給你省了不少事,不必使用流操作。
因為給你流對象已經讀過頭信息了,所以再讀就是數據了。
冠爾培訓------Java和3G工程師黃埔軍校!
Ⅱ 如何從HttpServletRequest request裡面獲取客戶端提交的post內容
實現思路:先通過java代碼獲取到jsp中相應的參數值(可以多次獲取到不同的值)
舉例:
form中定義:
<input type="text" value="zhangsan1" name ="username"/>
<input type="text" value="23" name ="userage"/>
java中定義:
String age = request.getParameter (「userage」);//獲取到jsp中的值
String username= request.getParameter (「username」);//獲取到jsp中的值
備註:form中標簽欄位必須有name屬性,否則獲取不到。
Ⅲ servlet 如何獲取HttpURLConnection 用post方式發送來的參數信息,
用request.getParameter("sql");方法獲取參數,然後在進行解碼
Ⅳ Servlet的post 和 get方法,搞暈了
伺服器端從客戶端(瀏覽器)獲得數據的方式有兩種,即get和post.
想必樓主對http協議也有所了解,http的get方法是將你要傳輸的數據放在url後的,也就是我們常用的test.jsp?name=aaa&age=10這種格式,數據都是可以通過url就可以看到了,你在網頁中的屬性就能看到這個url,這是get方法,由於受到http協議的限制,這種方式傳輸數據有大小限制,1024,為了能夠傳輸更多的數據,你可以採用post,post是將數據放到了http的Content中,這就不受http協議的影響了,而且傳輸的數據不能直接被看到(登陸的用戶名和密碼可不能用get哦)。大量的數據或是文件上在數據,都只能用post來傳而且get方法提交的頁面能夠通過history.back()回退,但post的數據就不能,回退時告訴已經過期,應為你已經post出去了,就不能取回來了。
簡單的說,get是通過http header來傳輸數據,有數量限制,而post則是通過http body來傳輸數據,沒有數量限制.
servlet就是專門用來處理http請求的,所以對應不同數據傳輸方式的兩種方法就是doGet()和doPost().
在裡面直接寫 html代碼不能顯示到頁面上,不過可以用以下代碼實現:
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head><title>例子</head></title>");
out.println("<body>");
out.println("樓主你好!");
out.println("</body></html>");
最後的結果會生成如下相對應的html代碼:
<html>
<head>
<title>例子</title>
</head>
<body>
樓主你好!
</body>
</html>
Ⅳ java程序如何獲取POST方式提交的數據
你好,你說的是html或jsp中的表單向伺服器中提交請求吧,在Servlet中,你可以直接使用request.getParamter("參數名稱") ;就可以接收到對應的參數了。
Ⅵ 新手 servlet中如何使用post上報數據要有例子代碼。
你想在Servlet中調用別個的伺服器嗎? 那就使用HttpClient框架
HttpClient httpclient = new DefaultHttpClient();//創建一個HttpClient
HttpPost httpPost = new HttpPost("www. URL");//創建一個Post
List <NameValuePair> nvps = new ArrayList<NameValuePair>();//創建表單
nvps.add(new BasicNameValuePair("data","test內容"));//增加一個欄位
nvps.add(new BasicNameValuePair("data2","test內容2"));//增加一個欄位
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); //將參數傳入post方法中
HttpResponse response = httpclient.execute(httpPost);//執行post請求
HttpEntity entity = response.getEntity();//得到伺服器的響應結果
String result = EntityUtils.toString(entity);//將結果轉成字元串
導入jar
Ⅶ Jquery使用post傳值給名為calculate的servlet,servlet該如何獲取該值
jquery中的post傳值給servlet的方法:
jQuery 的 get 和 post 方法有三個參數:地址,數據 和回調函數:
<script type="text/javascript">
$(document).ready(function(){
$("#btn01").click(function(){
var stationName = $("#stationName").val();
$.post("calculate",{type:"2",stationName:stationName},function(result){
alert(result);
});
}
});
});
</script>
注意:這個裡面要注意。var stationName = $("#stationName").val();是取表單元素的值。calculate 是servlet名稱。{type:"2",stationName:stationName} 是要傳到後台的參數,還有一個回調函數。
servlet後台的參數接收方法:
String name = request.getParameter("stationName");
name = new String(name.getBytes("iso-8859-1"), "utf-8");
String result = stationService.queryStationByName(name);
out.write(result);
Ⅷ servlet里如何處理post和get請求
J2EE:servlet 有專門處理的方法函數啊
post請求在doPost裡面寫
get請求在doGet裡面寫
下面是一個空的servlet響應類
init 初始化 destroy銷毀 doGet處理頁面的get請求 doPost處理頁面的POST請求
除了post請求 其他方式訪問請求全部歸屬於doGet處理
package tony.com.cn;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class action
*/
public class action extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public action() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
}
}
Ⅸ Servlet里怎麼接收客戶端post上來的二進制數據
是上傳文件、還是form中的一個值 。。。。如果是上傳文件,可以使用smartupload組件接收。。。。。。。。。。。。。。。
Ⅹ 請教servlet中怎麼獲得post來的數據
Servlet代碼如下:
packageservlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import.FinancingProctDao;
importentity.FinancingProct;
{
/**
*處理post請求
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//針對post請求,設置允許接收中文
request.setCharacterEncoding("UTF-8");
//設置可以在頁面中響應的內容類型及中文
response.setContentType("text/html;charset=UTF-8");
//得到響應流對象
PrintWriterout=response.getWriter();
//接收數據
Stringid=request.getParameter("id");
Stringrisk=request.getParameter("risk");
Stringincome=request.getParameter("income");
StringsaleStarting=request.getParameter("saleStarting");
StringsaleEnd=request.getParameter("saleEnd");
Stringend=request.getParameter("end");
//創建實體類
FinancingProctprod=newFinancingProct(id,Integer.parseInt(risk),income,saleStarting,saleEnd,end);
//查詢
if(newFinancingProctDao().add(prod)==0){
out.print("<script>alert('保存失敗');history.go(-1);</script>");
}else{
out.print("<script>alert('保存成功');location='select';</script>");
}
out.flush();
out.close();
}
}