❶ java httpserver session怎麼實現
server端的Session實現通常是通過在Server端為請求的客戶分配SessionID實現的,然後把SessionID號發送給client端,client端把這個SessionID記錄下來,這里一般是通過Cookie記錄,如果Cookie禁用掉的話就得通過URL重寫的方式了,這樣,client端下次再請求同樣的server時,http請求中會攜帶上次server分配給它的SessionID,這樣,server端就知道來自client的請求是老用戶了,也就是來自於一個已有的會話(session)。
❷ java 如何搭建http伺服器
看你具體是想做什麼,現在現成的開源的java的http伺服器有很多,像tomcat之類的都有http伺服器功能,如果你只是單純的需要用的話,直接用tomcat就好了
但是如果你是做要自己用java實現一個http伺服器的話就要稍微麻煩一點
http伺服器,本質上還是基於tcpip協議的伺服器,首先用java的ServerSocket監聽一個埠(也可以使用開源的server組件,如quickserver之類的),然後對客戶端發上來的數據進行處理,這里就需要了解一下http協議了,因為上來的數據,都是按照http協議來組織的,你需要將請求數據解析後,將響應數據組織成http的響應,發回給客戶端。這樣一個簡單的http伺服器就實現了。
但是這個請求和響應都有很多種類,一個完整的http伺服器應該要都能夠支持,所以這裡面的工作量還是有一點的。
另外,上面說的http伺服器只是一個靜態的伺服器,如果你想讓你寫的服務具有動態功能,那你的伺服器還得提供javaee的容器功能,這樣做下去,沒准你也能寫一個tomcat出來了……
❸ 關於Java JDK1.6自帶的HttpServer線程接收請求的問題,哪位對這塊比較熟悉,求解決
輸出完了要關閉連接啊。用exc.close(); 執行關閉連接。
HttpServer server = HttpServer.create(new InetSocketAddress(44444),10);
的10 表示隊列能接受10個請求。如果隊列的請求超過10個,就不再加入隊列,而是直接斷開連接。
丟失部分請求 就是因為請求超過了隊列的10個 所以伺服器直接丟棄了啊。
❹ java如何創建一個簡單的http介面
1.修改web.xml文件
<!-- 模擬HTTP的調用,寫的一個http介面 --> <servlet> <servlet-name>TestHTTPServer</servlet-name> <servlet-class>com.atoz.http.SmsHTTPServer</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestHTTPServer</servlet-name> <url-pattern>/httpServer</url-pattern> </servlet-mapping>
2.新建SmsHTTPServer.java文件
package com.atoz.http;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.atoz.action.order.SendSMSAction; import com.atoz.util.SpringContextUtil;
public class SmsHTTPServer extends HttpServlet { private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); String content = request.getParameter("content"); //String content = new String(request.getParameter("content").getBytes("iso-8859-1"), "utf-8"); String mobiles = request.getParameter("mobiles"); String businesscode = request.getParameter("businesscode"); String businesstype = request.getParameter("businesstype"); if (content == null || "".equals(content) || content.length() <= 0) { System.out.println("http call failed,參數content不能為空,程序退出"); } else if (mobiles == null || "".equals(mobiles) || mobiles.length() <= 0) { System.out.println("http call failed,參數mobiles不能為空,程序退出"); } else { /*SendSMSServiceImpl send = new SendSMSServiceImpl();*/ SendSMSAction sendSms = (SendSMSAction) SpringContextUtil.getBean("sendSMS"); sendSms.sendSms(content, mobiles, businesscode, businesstype); System.out.println("---http call success---"); } out.close(); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
3.調用http介面
String content = "測試"; content = URLEncoder.encode(content, "utf-8"); String url = "http://localhost:8180/atoz_2014/httpServer?content=" + content + "&mobiles=15301895007"; URL httpTest; try { httpTest = new URL(url); BufferedReader in; try { in = new BufferedReader(new InputStreamReader( httpTest.openStream())); String inputLine = null; String resultMsg = null; //得到返回信息的xml字元串 while ((inputLine = in.readLine()) != null) if(resultMsg != null){ resultMsg += inputLine; }else { resultMsg = inputLine; } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
打字不易,望採納,謝謝
❺ 用java編寫的httpserver如何處理客戶端發來的表單數據
獲得客戶端發送的變數用request.getAttribute("變數名")就可以獲得,想交給另一個頁面處理就轉發到(dispatch)另一個頁面
❻ java httpserver 線程怎麼結束
這主要是看你打算同時支持 GET 和 PUT 還是只打算支持其中的一個。如果你不覆蓋父類的 doGet 或 doPut 可能客戶端訪問時得到的就是 "不支持的操作」。 GET 和 PUT 的有區別的,從字面上來講: GET 是去伺服器拿。 PUT 是把東西送給伺服器
❼ java mina可以實現http server嗎
erver端的Session實現通常是通過在Server端為請求的客戶分配SessionID實現的,然後把SessionID號發送給client端,client端把這個SessionID記錄下來,這里一般是通過Cookie記錄,如果Cookie禁用掉的話就得通過URL重寫的方式了,這樣
❽ java里http server需要什麼包
tomcat里有,servlet-api.jar
❾ java 如何實現 http協議傳輸
Java 6 提供了一個輕量級的純 Java Http 伺服器的實現。下面是一個簡單的例子:
public static void main(String[] args) throws Exception{
HttpServerProvider httpServerProvider = HttpServerProvider.provider();
InetSocketAddress addr = new InetSocketAddress(7778);
HttpServer httpServer = httpServerProvider.createHttpServer(addr, 1);
httpServer.createContext("/myapp/", new MyHttpHandler());
httpServer.setExecutor(null);
httpServer.start();
System.out.println("started");
}
static class MyHttpHandler implements HttpHandler{
public void handle(HttpExchange httpExchange) throws IOException {
String response = "Hello world!";
httpExchange.sendResponseHeaders(200, response.length());
OutputStream out = httpExchange.getResponseBody();
out.write(response.getBytes());
out.close();
}
}
然後,在瀏覽器中訪問 http://localhost:7778/myapp/