網(wǎng)頁學習
目的
- 了解HTML語言纤房,PHP語言纵隔。
- 了解數(shù)據(jù)的兩種提交方式:get和post
- 后臺語言 使用URL
技術
1.使用HTML語言制作一個簡易網(wǎng)頁
2.做一個表單用于提交用戶的數(shù)據(jù)
3.使用代碼下載、上傳服務器數(shù)據(jù)
技術實現(xiàn)
1.使用HTML語言制作一個簡易網(wǎng)頁
<!-- XML HTML 標記語言:通過一個對應的字段來標識某種功能
-->
<!DOCTYPE html>
<html>
<head>
<!-- 頭部 -->
<meta charset="utf-8">
<title>我的第一個網(wǎng)頁</title>
</head>
<!-- 具體內容 -->
<body>
<!--顯示文字 -->
<h1 align="center">標題</h1>
<h2 align="center">這是一個子標題</h2>
<center><p> 棄我去者炮姨,昨日之日不可留捌刮;<br>
亂我心者,今日之日多煩憂舒岸。<br>
長風萬里送秋雁绅作,對此可以酣高樓。<br>
蓬萊文章建安骨蛾派,中間小謝又清發(fā)俄认。<br>
俱懷逸興壯思飛个少,欲上青天覽明月。<br>
抽刀斷水水更流眯杏,舉杯消愁愁更愁夜焦。<br>
人生在世不稱意,明朝散發(fā)弄扁舟岂贩。</p>
<img src ="D:\AndroidWeb\ApachePackage\Apache24\htdocs\1.jpg"
width="500" height="500" ></center>
<!-- 插入表格 -->
<center><table border="1" bgcolor="green">
<!-- tr表示一行數(shù)據(jù) td表示列 -->
<tr>
<td>姓名</td>
<td>班級</td>
<td>成績</td>
</tr>
<tr>
<td>小王</td>
<td>計科</td>
<td>89</td>
</tr>
</table>
<!-- 插入鏈接 -->
<a >這是一個百度的鏈接</a>
</center>
</body>
</html>
效果2.做一個表單用于提交用戶的數(shù)據(jù)
<!DOCTYPE html>
<html>
<head>
<title>登錄</title>
</head>
<body background="4634eba2abbbb47895e99096530471f5.jpg">
<!-- 表單的內容 -->
<!--
action:內容提交到服務器的哪個文件中
提交的內容由服務器的哪個文件來處理
method:提交的方式 get/post
-->
<form action="login.php" method="get">
<center>
<br><br><br><br>
用戶名:<input type="text" name="user_name"><br>
<br><br>
密  碼:<input type="password" name="user_password">
<br><br>
<input type="submit" name="提交">
</center>
</form>
</body>
</html>
使用PHP語言
<<?php
// 獲取提交的用戶名 get->$_GET post->$_POST
$name = $_GET["user_name"];
$password = $_GET["user_password"];
// 查詢數(shù)據(jù)庫
// 返回結果
echo "success";
?>
效果3.使用代碼訪問服務器數(shù)據(jù)
public static void getParams()throws IOException{
// 使用代碼訪問(提交/下載)服務器數(shù)據(jù)
// URL
// http://
// 1.創(chuàng)建URL
String path = "http://10.129.28.234/login.php?user_name=jack&user_password=123";
URL url = new URL(path);
// 獲取連接的對象
// URLConnection封裝了Socket
URLConnection connection = url.openConnection();
// 設置請求方式
HttpURLConnection httpConnection = (HttpURLConnection)connection;
httpConnection.setRequestMethod("GET");
// 接收服務器端的數(shù)據(jù)
// BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// System.out.println(br.readLine());
InputStream is = httpConnection.getInputStream();
byte[] buf = new byte[1024];
int len = -1;
while ((len=is.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
}
}
下載數(shù)據(jù)
public static void getImage() throws Exception {
// URL
URL url = new URL("http://10.129.28.234/1.jpg");
// 獲取與服務器連接的對象
URLConnection connection = url.openConnection();
// 讀取下載的內容 - 獲取輸入流
InputStream is = connection.getInputStream();
// 創(chuàng)建文件保存的位置
FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.jpg");
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf))!= -1){
fos.write(buf,0,len);
}
}
使用post上傳簡單數(shù)據(jù)(不是文件)
public static void post() throws Exception {
// 1. 創(chuàng)建URL
URL url = new URL("http://10.129.28.234/login.php");
// 獲取connection對象
// URLConnection
// HttpURLConnection 自己需要設定請求的內容
// 請求方式 上傳的內容
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
// 3.設置請求方式為post
connection.setRequestMethod("POST");
// 設置有輸出流 需要上傳
connection.setDoOutput(true);
// 設置有輸入流 需要下載
connection.setDoInput(true);
// 4.準備上傳的數(shù)據(jù)
String data = "user_name=jack&user_password=123";
// 5.開始上傳 輸出流對象
OutputStream os = connection.getOutputStream();
os.write(data.getBytes());
os.flush(); //寫完了
// 6.接收服務器端返回的數(shù)據(jù)
InputStream is = connection.getInputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1){
System.out.println(new String(buf,0,len));
}
}