标记 | 描述 |
---|---|
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>
/**
* 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>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。