服務器驗證提交的表單程序 java – JAVA編程語言程序開發技術文章

//服務器驗證提交的表單程序   點擊下載源碼http://up.aiwalls.com/2012/0317/20120317112753397.rar
 

package cn.itcast.day_24;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;

public class Server {

 public static void main(String[] args) throws Exception {
  System.out.println("服務已開啟,監聽9999端口");
  //創建服務器
  ServerSocket serversocket = new ServerSocket(9999);
  while(true){
   //等待客服端的請求
   final Socket socket = serversocket.accept();
   new Thread(){
    @Override
    public void run(){
     try {
      //創建輸入流 輸出流
      InputStream in = socket.getInputStream();
      PrintStream ps = new PrintStream(socket.getOutputStream());
      //OutputStream out = socket.getOutputStream();
     
      //讀取從客服端發來的消息
      byte[] buffer = new byte[1024 * 1024];
      int len = in.read(buffer);
      String request = new String(buffer, 0, len);
      System.out.println(request);
     
      //從客服端發出的數據中查看到,關鍵的內容數據都是放在最後,並且換瞭兩次行,從而有瞭截取內容的方法
      int index = request.indexOf("\r\n\r\n");  //拿到換兩次行的索引
      String content = request.substring(index + 4);  //把索引加4就得到瞭內容數據
     
      //使用getParameter方法獲取鍵的值
      String username = getParameter(content,"username");
      String password = getParameter(content,"password");
      String gender = getParameter(content,"gender");
      String interest = getParameter(content,"interest");
      String city = getParameter(content,"city");
      String photo = getParameter(content,"photo");
     
      System.out.println("username=" + username);  //打印出讀取到的內容
      System.out.println("password=" + password);
      System.out.println("gender=" + gender);
      System.out.println("interest=" + interest);
      System.out.println("city=" + city);
      System.out.println("photo=" + photo);
     
      String mes = "註冊成功";                    //開始時默認設置註冊成功
     
      if("".equals(username) || username == null) //判斷用戶名 密碼等是否為空
       mes = "用戶名必須填寫";
      else if("".equals(password) || password == null)
       mes = "密碼必須填寫";
      else if("".equals(gender) || gender == null)
       mes = "性別必須選擇";
      else if("".equals(interest) || interest == null)
       mes = "興趣必須選擇";
      else if("".equals(city) || city == null)
       mes = "城市必須選擇";
      else if("".equals(photo) || photo == null)
       mes = "頭像必須上傳";
     
      //以寫代碼的方式,判斷是否成功
      //out.write(("<font size='7' color='" + ("註冊成功".equals(mes) ? "green" : "red")+ "'>").getBytes());
     
      /*ps.println("<html>");
      ps.println("<head>");
      ps.println("<title>" + "註冊結果"+ "</title>");
      ps.println("</head>");
     
      ps.println("<body>");
      ps.println(("<font size='7' color='" + ("註冊成功".equals(mes) ? "green" : "red")+ "'>") + mes + "</font>");
     
      ps.println("</body>");
      ps.println("</html>");*/
      
      //新建兩個文件,判斷是否註冊成功,讀取文件的內容,暫時存儲到ByteArrayOutputStream中
      FileInputStream fis = new FileInputStream("註冊成功".equals(mes) ? "page/success.html" : "page/fale.html");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      while((len = fis.read(buffer)) != -1)
       baos.write(buffer, 0, len);
     
      fis.close();
      baos.close();
     
      //ByteArrayOutputStream的toByteArray方法創建瞭一個新的數組,把新的數組傳到string中,創建瞭一個新的字符串
      String response = new String(baos.toByteArray());
      //使用新的字符串替換掉文件中的"變量"
      response = response.replace("變量", mes);
      //System.out.println(response);
      //打印出服務器返回的註冊成功或失敗的數據
      ps.println(response + "<br><br><br>");
     
      //打印出用戶註冊的信息
      ps.println("username=" + username + "<br>");
      ps.println("password=" + password +  "<br>");
      ps.println("gender=" + gender + "<br>");
      ps.println("interest=" + interest + "<br>");
      ps.println("city=" + city + "<br>");
      ps.println("photo=" + photo + "<br>");
     
      //關閉本線程的流
      socket.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    
    
    }
   }.start();
  }

 }
 
 //獲取鍵值的方法,並且對值進行解碼
 public static String getParameter(String content, String key) throws UnsupportedEncodingException{
  String[] entryArr = content.split("&");
  for (String contents : entryArr) {
   String[] arr = contents.split("=");
   if(arr[0].equals(key))
    return arr.length == 2 ? URLDecoder.decode(arr[1], "GBK") : "";
  }
  return null;
 }
}

 

摘自  代俊建的專欄 

發佈留言