網(wǎng)頁學(xué)習(xí)
目的
- 了解HTML語言,PHP語言。
- 了解數(shù)據(jù)的兩種提交方式:get和post
- 后臺(tái)語言 使用URL
技術(shù)
1.使用HTML語言制作一個(gè)簡(jiǎn)易網(wǎng)頁
2.做一個(gè)表單用于提交用戶的數(shù)據(jù)
3.使用代碼下載、上傳服務(wù)器數(shù)據(jù)
技術(shù)實(shí)現(xiàn)
1.使用HTML語言制作一個(gè)簡(jiǎn)易網(wǎng)頁
<!-- XML HTML 標(biāo)記語言:通過一個(gè)對(duì)應(yīng)的字段來標(biāo)識(shí)某種功能
-->
<!DOCTYPE html>
<html>
<head>
<!-- 頭部 -->
<meta charset="utf-8">
<title>我的第一個(gè)網(wǎng)頁</title>
</head>
<!-- 具體內(nèi)容 -->
<body>
<!--顯示文字 -->
<h1 align="center">標(biāo)題</h1>
<h2 align="center">這是一個(gè)子標(biāo)題</h2>
<center><p> 棄我去者,昨日之日不可留;<br>
亂我心者,今日之日多煩憂。<br>
長風(fēng)萬里送秋雁,對(duì)此可以酣高樓。<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>班級(jí)</td>
<td>成績(jī)</td>
</tr>
<tr>
<td>小王</td>
<td>計(jì)科</td>
<td>89</td>
</tr>
</table>
<!-- 插入鏈接 -->
<a >這是一個(gè)百度的鏈接</a>
</center>
</body>
</html>
效果
QQ圖片20190823193342.jpg
2.做一個(gè)表單用于提交用戶的數(shù)據(jù)
<!DOCTYPE html>
<html>
<head>
<title>登錄</title>
</head>
<body background="4634eba2abbbb47895e99096530471f5.jpg">
<!-- 表單的內(nèi)容 -->
<!--
action:內(nèi)容提交到服務(wù)器的哪個(gè)文件中
提交的內(nèi)容由服務(wù)器的哪個(gè)文件來處理
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ù)庫
// 返回結(jié)果
echo "success";
?>
效果
QQ圖片20190823192628.jpg
3.使用代碼訪問服務(wù)器數(shù)據(jù)
public static void getParams()throws IOException{
// 使用代碼訪問(提交/下載)服務(wù)器數(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);
// 獲取連接的對(duì)象
// URLConnection封裝了Socket
URLConnection connection = url.openConnection();
// 設(shè)置請(qǐng)求方式
HttpURLConnection httpConnection = (HttpURLConnection)connection;
httpConnection.setRequestMethod("GET");
// 接收服務(wù)器端的數(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");
// 獲取與服務(wù)器連接的對(duì)象
URLConnection connection = url.openConnection();
// 讀取下載的內(nèi)容 - 獲取輸入流
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上傳簡(jiǎn)單數(shù)據(jù)(不是文件)
public static void post() throws Exception {
// 1. 創(chuàng)建URL
URL url = new URL("http://10.129.28.234/login.php");
// 獲取connection對(duì)象
// URLConnection
// HttpURLConnection 自己需要設(shè)定請(qǐng)求的內(nèi)容
// 請(qǐng)求方式 上傳的內(nèi)容
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
// 3.設(shè)置請(qǐng)求方式為post
connection.setRequestMethod("POST");
// 設(shè)置有輸出流 需要上傳
connection.setDoOutput(true);
// 設(shè)置有輸入流 需要下載
connection.setDoInput(true);
// 4.準(zhǔn)備上傳的數(shù)據(jù)
String data = "user_name=jack&user_password=123";
// 5.開始上傳 輸出流對(duì)象
OutputStream os = connection.getOutputStream();
os.write(data.getBytes());
os.flush(); //寫完了
// 6.接收服務(wù)器端返回的數(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));
}
}