JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--技术新闻--缓存区  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
Http server学习版以及通过代理服务器认证
作者:     文章来源:
访问次数:1次     加入时间:2005年01月01日
一、Http Server的最简单版本

这个版本不能在简单了,有助于学习
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. public class httpd extends Thread {
  5.     public static final int HTTP_PORT = 8999;
  6.     protected ServerSocket listen;
  7.     public static void main (String args[]) throws IOException {
  8.         System.out.println("Starting Server...");
  9.         new httpd();
  10.         System.out.println("Http Server Started,OK!");
  11.     }
  12.     public httpd () {
  13.         try {
  14.             listen = new ServerSocket(HTTP_PORT);
  15.         } catch (IOException e) {
  16.             System.out.println("Error in creating server socket");
  17.         }
  18.         this.start();
  19.     }
  20.     public void run () {
  21.         try {
  22.             while (true) {
  23.                 Socket client = listen.accept();
  24.                 Connect cc = new Connect(client);
  25.             }
  26.         } catch (IOException e) {
  27.         }
  28.     }
  29. }
  30. /**
  31.  *  socket连接
  32.  */
  33. class Connect extends Thread {
  34.     Socket client;
  35.     DataInputStream is;
  36.     public OutputStream rawOut = null;
  37.     public DataOutputStream out = null;
  38.     public Connect (Socket s) {
  39.         client = s;
  40.         try {
  41.             is = new DataInputStream(client.getInputStream());
  42.             rawOut = client.getOutputStream();
  43.             out = new DataOutputStream(rawOut);
  44.         } catch (IOException e) {
  45.             try {
  46.                 client.close();
  47.             } catch (IOException ex) {
  48.             }
  49.         }
  50.         this.start();
  51.     }
  52.     public void run () {
  53.         try {
  54.             String request = is.readUTF();
  55.             System.out.println("请求:" + request);
  56.             StringTokenizer st = new StringTokenizer(request);
  57.             if ((st.countTokens() >= 2) && st.nextToken().equals("GET")) {
  58.                 if ((request = st.nextToken()).startsWith("/"))
  59.                     request = request.substring(1).trim();
  60.                 if (request.endsWith("/") || request.equals(""))
  61.                     request = request + "index.html";
  62.                 File f = new File(request.trim());
  63.                 shipDocument(out, f);
  64.             } else {
  65.                 out.writeBytes("400 Bad Request");
  66.             }
  67.             client.close();
  68.             System.out.println("关闭:" + client.getPort());
  69.         } catch (IOException ex1) {
  70.         }
  71.     }
  72.     synchronized public static void shipDocument (DataOutputStream out, File f) {
  73.         DataInputStream in = null;
  74.         try {
  75.             in = new DataInputStream(new FileInputStream(f));
  76.             int len = (int) f.length();
  77.             byte buf[] = new byte[8192];
  78.             System.out.println("web文件名:" + f.getName() + " length:" + f.length());
  79.             FileInputStream file = new FileInputStream(f);
  80.             out.writeBytes("HTTP/1.1 200 OK  ");
  81.             out.writeBytes("Content-Length:" + buf.length + "  ");
  82.             out.writeBytes("Content-Type:texthtml ");
  83.             for (int i = 0; i < len; i += 8192) {
  84.                 int bytesRead = file.read(buf);
  85.                 out.write(buf, 0, bytesRead);
  86.             }
  87.             out.writeBytes(" ");
  88.             out.flush();
  89.             in.close();
  90.             file.close();
  91.         } catch (FileNotFoundException ex) {
  92.             try {
  93.                 out.writeBytes("404 not found!");
  94.             } catch (IOException e) {
  95.             }
  96.         } catch (IOException e) {
  97.             System.out.println("err");
  98.         }
  99.     }
  100. }



二、 代理服务器认证的实现
     扩展JR开源项目的网络蚂蚁,通过代理服务器,弹出认证窗口。

  1. import java.io.*;
  2. import java.net.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. public class URLPassword extends Frame {
  7.     private TextField tf = new TextField();
  8.     private TextArea ta = new TextArea();
  9.     public URLPassword () {
  10.         super("URL Password");
  11. // 安装 Authenticator
  12.         MyAuthenticator myAuth = new MyAuthenticator();
  13.         Authenticator.setDefault(myAuth);
  14. // 设置屏幕
  15.         add(tf, BorderLayout.NORTH);
  16.         ta.setEditable(false);
  17.         add(ta, BorderLayout.CENTER);
  18.         tf.addActionListener(new ActionListener() {
  19.             public void actionPerformed (ActionEvent e) {
  20.                 String s = tf.getText();
  21.                 if (s.length() != 0)
  22.                     ta.setText(fetchURL(s));
  23.             }
  24.         });
  25.         addWindowListener(new WindowAdapter() {
  26.             public void windowClosing (WindowEvent e) {
  27.                 dispose();
  28.                 System.exit(0);
  29.             }
  30.         });
  31.     }
  32.     private String fetchURL (String urlString) {
  33.         StringWriter sw = new StringWriter();
  34.         PrintWriter pw = new PrintWriter(sw);
  35.         try {
  36.             URL url = new URL(urlString);
  37.             InputStream content = (InputStream) url.getContent();
  38.             BufferedReader in =
  39.                     new BufferedReader(new InputStreamReader(content));
  40.             String line;
  41.             while ((line = in.readLine()) != null) {
  42.                 pw.println(line);
  43.             }
  44.         } catch (MalformedURLException e) {
  45.             pw.println("Invalid URL");
  46.         } catch (IOException e) {
  47.             pw.println("Error reading URL");
  48.         }
  49.         return sw.toString();
  50.     }
  51.     public static void main (String args[]) {
  52.         Properties systemSettings = System.getProperties();
  53.         systemSettings.put("proxySet""true");
  54.         systemSettings.put("proxyHost""202.112.209.106");
  55.         systemSettings.put("proxyPort""8888");
  56.         System.setProperties(systemSettings);
  57.         Frame f = new URLPassword();
  58.         f.setSize(300, 300);
  59.         f.setVisible(true);
  60.     }
  61.     class MyAuthenticator extends Authenticator {
  62.         protected PasswordAuthentication getPasswordAuthentication () {
  63.             final Dialog jd = new Dialog(URLPassword.this"Enter password"true);
  64.             jd.setLayout(new GridLayout(0, 1));
  65.             Label jl = new Label(getRequestingPrompt());
  66.             jd.add(jl);
  67.             TextField username = new TextField();
  68.             username.setBackground(Color.lightGray);
  69.             jd.add(username);
  70.             TextField password = new TextField();
  71.             password.setEchoChar('*');
  72.             password.setBackground(Color.lightGray);
  73.             jd.add(password);
  74.             Button jb = new Button("OK");
  75.             jd.add(jb);
  76.             jb.addActionListener(new ActionListener() {
  77.                 public void actionPerformed (ActionEvent e) {
  78.                     jd.dispose();
  79.                 }
  80.             });
  81.             jd.pack();
  82.             jd.setVisible(true);
  83.             return new PasswordAuthentication(username.getText(), password.getText().toCharArray());
  84.         }
  85.     }
JSP虚拟主机 | JSP空间 | JSP主机 | JSP双线虚拟主机 | JAVA空间 | JAVA虚拟主机 | 虚拟服务器 | JSP 虚拟服务器 | VPS
Virtual Private Server | JAVA虚拟服务器 | VM服务器 | VHOST | 虚拟操作系统 | JSP论坛 | JAVA论坛 | JSP站点论坛
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号