❶ 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/