-
1 # 隨風而行00
-
2 # 盛文天下
在講html與servlet進行資料互傳之間,有必要先了解傳統的html與servlet之間引數的傳統,為了能夠更好地說明,此種傳統之間的特點,現在用一個登入的案例來說明此種之間引數的傳遞。
1)首先建立一個用於登入的login.html頁面,程式碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登入介面</title>
<style>
body{width:300px; height:200px; margin:0 auto;}
fieldset{ width:300px; }
table{ width:300px; height:100px;}
</style></head><body>
<fieldset><legend>登入介面</legend>
<form action="../LoginServlet" method="post">
<table>
<tr><td>姓名:</td><td><input type="text" name="name"/></td></tr>
<tr><td>密碼:</td><td><input type="text" name="password"/></td></tr>
</table>
<input type="submit" value="提交">
<input type="reset" value="重置"/>
</form>
</fieldset>
</body>
</html>
需要注意的是表單中的name屬性必須填寫
2)建立一個用於接收引數的LoginServlet.java,具體程式碼如下:
package zjh.javaee.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; @WebServlet("/LoginServlet")public class LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L; public LoginServlet() { super();} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//解決亂碼問題request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");//接收請求的引數String userName = request.getParameter("name"); //這裡的name指的是標籤中的name屬性String userPassword = request.getParameter("password"); //這裡的password指的也是標籤中的name屬性System.out.println("姓名為:"+userName+":"+"密碼為:"+userPassword);//判斷輸入的姓名、密碼是否為空,如果不為空則跳轉到顯示結果的show.html頁面if(userName.equals("")||userName==null||userPassword.equals("")||userPassword==null){System.out.println("error");}else{//伺服器端跳轉request.getRequestDispatcher("/html/show.html").forward(request, response); }}}3)用於顯示登入成功的show.html頁面
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登入成功</title></head><body<h1>登入成功</h1></body></html>由上面的案例不難看出,如果用html與servlet進行引數的傳遞,有一個明顯的問題,就是隻能將html中的引數傳到servlet後臺中,卻不能從後臺將資料傳回到前端的html頁面中,另外,如果用html與servlet進行資料傳遞,在html頁面中只能透過表單的形式來傳遞引數到後臺中,這就在一定程度上限制了html的功能。
學前端,入門是最容易的了!
但是也只是入門容易,其他的難啊! 想學好更難,JavaScript和JQ就是困難的第一關,如果是自己學不知道要學多久,不如加入一個實戰學習裙然後學習,我推薦一個群給你,這個裙是前端基礎實戰學習群,這個群有很多的資源,可以進入看看,裙號是六六開頭,然後就是壹肆七三,最後面的就是壹零八,加在一起就好,其實前端並沒有你想象那麼難!
我說一下前端具體學習路線和學習內容:
第一個月:HTML+CSS 做出專案實戰小米,華為這樣的官網
第二個月+第三個月:JavaScript+jQuery+ajax 這塊邏輯很重要 ,做出專案實戰推箱子 飛機大戰
第四個月:HTML5+CSS3 做出手機移動網站
第五個月+第六個月:bootstrat+vue+react+node 前端主流框框
回覆列表
很簡單 透過模板引擎使用http傳輸協議將html /css/js傳輸到前端(其實就是字串傳輸).前端瀏覽器解析html為dom樹結構,解析css為cssdom,並解釋執行js指令碼程式碼。