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>