国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP實現HTTP隧道

2019-11-17 04:10:12
字體:
來源:轉載
供稿:網友

共有兩個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.          }
     }
}

 


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 descr    }

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德阳市| 垦利县| 龙海市| 吴江市| 皮山县| 安乡县| 霍林郭勒市| 长汀县| 个旧市| 垫江县| 长乐市| 九龙城区| 呈贡县| 汪清县| 金川县| 榆中县| 平罗县| 汽车| 客服| 长岭县| 桂林市| 漯河市| 页游| 紫阳县| 平湖市| 金阳县| 苗栗县| 金华市| 米易县| 寻甸| 海兴县| 崇礼县| 利川市| 宜兰市| 普安县| 定安县| 铁岭市| 策勒县| 富民县| 罗源县| 黄陵县|