Web服務器須要對客戶端的請求提供響應內容,Web瀏覽器須要將這些響應內容呈現給用戶。二者須要造成統一的對話語言,Web應用中造成的共同的語言被稱爲HTML(HyperText Markup Language:超文本標記語言)。html
HTML包含數十個標記,數千種標記屬性,能過這些標記定義了網頁內容的含義和結構;正是無數個具備相互連接的HTML網頁構成了咱們如今的互聯網世界。java
標記 | 描述 |
---|---|
html | 定義HTML文檔的邊界 |
head | 定義HTML文檔頭部的邊界 |
body | 定義HTML文檔本體的邊界 |
tile | 定義HTML文檔本體的標題 |
form | 定義一個表單 |
a | 定義一個超連接 |
... | ... |
<!-- HTML基礎 --> <html> <head> <title>歡迎</title> </head> <body> <a href="https://www.baidu.com">百度</a> </body> </html>
curl https://www.baidu.com -v <!-- 經過HTTP向URL地址www.baidu.com發起 GET請求--> > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: www.baidu.com > Accept: */* <!-- Web服務器HTTP響應--> < HTTP/1.1 200 OK < Accept-Ranges: bytes < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform < Connection: keep-alive < Content-Length: 2443 < Content-Type: text/html < Date: Tue, 25 Aug 2020 04:04:01 GMT < Etag: "588603eb-98b" < Last-Modified: Mon, 23 Jan 2017 13:23:55 GMT < Pragma: no-cache < Server: bfe/1.0.8.18 < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ < <!DOCTYPE html> <!--STATUS OK--> <html> <head> ... <title>百度一下,你就知道</title> </head> <body link=#0000cc> ... </body> </html>
Java Servlet是一個接口,定義了Java類被瀏覽器訪問到的規則。程序員
Java程序員自定義一個類,實現Servlet接口並重寫接口中的方法,Web服務器(如Tomcat等)就能夠識別並執行這個程序。web
建立JavaEE項目
小程序
定義一個類,實現Servlet接口,實現或重寫接口中的方法瀏覽器
/** * HttpServlet * * @author zhuhuix * @date 2020-08-25 */ public class HttpServletDemo extends HttpServlet { // 重寫Get請求方法 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 業務邏輯處理 LocalDate date = LocalDate.now(); // 動態輸出HTML頁面 PrintWriter out = resp.getWriter(); out.println("<html>" + "<body>" + "<h1>"+date.toString()+"</h1> " + "<h2>hello world</h2> " + "</body>" + "</html>" ); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置Servlet --> <servlet> <servlet-name>http-demo</servlet-name> <servlet-class>demo.servlet.HttpServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>http-demo</servlet-name> <url-pattern>/http-demo</url-pattern> </servlet-mapping> </web-app>
<%@ page import="java.time.LocalDate" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h1><%= LocalDate.now()%></h1> <h2>hello world!!!</h2> </body> </html>
public class HttpServletDemo extends HttpServlet { // 重寫Get請求方法 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 調用jsp頁面 req.getRequestDispatcher("/index.jsp").forward(req,resp); } }
/** * HttpServlet * * @author zhuhuix * @date 2020-08-25 */ public class HttpServletDemo extends HttpServlet { // 重寫Get請求方法,返回一個表單頁面 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.getRequestDispatcher("/form.jsp").forward(req, resp); } // 重寫Post請求方法,提交表單上的數據,並返回結果頁面 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); Integer age = Integer.parseInt(req.getParameter("age")); if (name != null && age != null) { User user = new User(name, age); req.setAttribute("name", name); req.setAttribute("age", age); if (user.checkName()) { req.setAttribute("result","登記成功"); }else { req.setAttribute("result","登記失敗:名稱中包含非法字符"); } RequestDispatcher view = req.getRequestDispatcher("/result.jsp"); view.forward(req, resp); } } }
/** * 用戶類 */ public class User { private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } // 判斷名稱是否非法 public Boolean checkName() { return !this.name.contains("admin"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>http-demo</servlet-name> <servlet-class>demo.servlet.HttpServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>http-demo</servlet-name> <url-pattern>/http-demo</url-pattern> </servlet-mapping> </web-app>
<!--表單頁面--> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登記用戶</title> </head> <body> <form method="post" action="http-demo"> 姓名:<input name="name" type="text"/><br> 年齡:<input name="age" type="text"/><br> <input type="submit" title="提交" > </form> </body> </html>
<!--結果頁面--> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登記結果</title> </head> <body> <h2>姓名:${name} 年齡:${age} ${result} </h2><br> </body> </html>