您现在的位置: 365建站网 > 365文章 > JSP实现HTTP隧道

JSP实现HTTP隧道

文章来源:365jz.com     点击数:287    更新时间:2009-10-14 10:51   参与评论

共有两个java类和一个servlet(在同一个java包JavaSerializable中):

java:StudentList   StudentListTunnetApp(客户端)

servlet:StudentListTunnetServlet(服务器端)


StudentList(java类)定义一个类,StudentListTunnetApp(java类)运行在客户端,实例化一个StudentList对象并将其写到与StudentListTunnetServlet连接的HTTP中,然后发送请求到服务器端StudentListTunnetServlet(servlet)读取先前写入连接的StudentList对象


1.StudentList.java:


/**
 *
 * @author lucifer
 */


package JavaSerializable;

import java.util.*;
import java.io.*;

public class StudentList implements Serializable{
     Vector list = new Vector(6);

     public StudentList(){}

     public void addStudent(String name){
          if(name != null)
               list.addElement(name);
     }

     public void listStudent(){
          for(int i = 0;i < list.size();i++){
               System.out.println("Student" + i + ":" + (String)list.elementAt( i ));
          }
     }
}


2.StudentListTunnetApp.java:


/**
 *
 * @author lucifer
 */


package JavaSerializable;

import java.io.*;
import java.net.*;


public class StudentListTunnetApp {

     public StudentListTunnetApp(){}

     public void buildStudentList(StudentList list){
          list.addStudent("Bob Robinson");
          list.addStudent("Steve Robinson");
          list.addStudent("Rob Stevinson");
          list.addStudent("Tod Thomson");
          list.addStudent("Jack Jones");
          list.addStudent("Micheal Jackson");
     }

     public void writeStudentList(URLConnection connection,StudentList list){
          try{

               //ignore caching
               connection.setUseCaches(false);                     
                  connection.setRequestProperty("CONTENT_TYPE","application/octet-stream");

                  //使得发送和接收能使用用一个连接
                  connection.setDoInput(true);                         
                  connection.setDoOutput(true);


               ObjectOutputStream os = new ObjectOutputStream(connection.getOutputStream());
               System.err.println("Writing an object.");
               os.writeObject( list );
               os.flush();
               os.close();
          }
          catch(IOException e){
               System.err.println(e.getMessage());
          }
     }

     public StudentList readStudentList(URLConnection connection){
          StudentList list = null;
          try{
               ObjectInputStream is = new ObjectInputStream(connection.getInputStream());
               System.err.println("Waiting for response.");
               list = (StudentList)is.readObject();
               is.close();
          }
          catch(IOException e){
               System.err.println(e.getMessage());
          }
          catch(ClassNotFoundException e){
               System.err.println(e.getMessage());
          }
          return list;
     }

     public void invoke(){
          try{
               URL url = new URL("http://localhost:8084/LearnServlet/StudentListTunnetServlet");
               StudentList list = new StudentList();
               buildStudentList(list);
               list.listStudent();
               System.err.println("Opening an connection.");
               URLConnection connection = url.openConnection();

               writeStudentList(connection,list);

               StudentList inlist = readStudentList(connection);
               if(inlist != null){
                    inlist.listStudent();
               }
               else{
                    System.err.println("readObject failed.");
               }
               System.out.println("press enter to quit.");
               System.in.read();
          }
          catch(MalformedURLException e){
               System.err.println(e.getMessage());
          }
          catch(Exception e){
               System.err.println(e.getMessage());
          }
     }

     public static void main(String[] args){
          StudentListTunnetApp studentlist = new StudentListTunnetApp();
          studentlist.invoke();
     }
}


3.StudentListTunnetServlet.java(servlet):


/**
 *
 * @author lucifer
 */


package JavaSerializable;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class StudentListTunnetServlet extends HttpServlet {
  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet StudentListTunnetServlet</title>"); 
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet StudentListTunnetServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally {
            out.close();
        }
    }

    @Override

    //用的是这个方法
    public void service (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
         try{
              ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
              StudentList list = (StudentList)ois.readObject();
              response.setContentType("application/octet-stream");

              ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
              oos.writeObject(list);
              oos.flush();
              oos.close();
         }
         catch(ClassNotFoundException e){
               System.err.println(e.getMessage());
         }
    }

    @Override
    public void init(ServletConfig config)throws ServletException{
         super.init(config);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (287人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号