// MyHttpServlet.java 文件
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;/**
* Servlet implementation class MyHttpServlet
*/
public class MyHttpServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置相应的字符集
response.setCharacterEncoding("UTF-8");
// 设置相应的MIME类型
response.setContentType("text/html; charset=utf-8");import org.eclipse.jdt.internal.compiler.ast.ThisReference;
// ServletConfig
ServletConfig A = this.getServletConfig();
String a = A.getInitParameter("MyHeepServletConfig");
// ServletContext
ServletContext B = this.getServletContext();
String b = B.getInitParameter("Database");
String c = B.getInitParameter("USER");
String d = B.getInitParameter("PASSWORD");PrintWriter out = response.getWriter();
out.write("1.输出Config的形式内容" + a + "<br/>"
+ "2.输出Context的形式内容"
+ "<br/>"
+ "数据库:" + b + "<br/>"
+ "用户名:" + c+ "<br/>"
+ "密码:" + d + "<br/>");
//setAttribute
ServletContext E = this.getServletContext();
E.setAttribute("Name", "getAttribute");// 设置内容
Object e = E.getAttribute("Name");
// 提出 Name 键 指对 设定的内容
out.write("3.创建在内存的值:" + e + "<br/>");// request
// 引入 jsp文件的标签 输入的字符串数据
String F = (String) request.getParameter("Intocont");ServletContext intoRe = this.getServletContext();
Object Intocont = intoRe.getAttribute("Intocont");if (Intocont == null) {
Intocont = "";
}
intoRe.setAttribute("Intocont", Intocont + "<br/>" + F);
Intocont = intoRe.getAttribute("Intocont");
out.write("4.弹幕功能极简版:<br/>" + Intocont.toString());
// 刷新输入的页面,弹幕会返回加载输出
}
}
< -- web.xmi 文件手动编辑 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="5.0">-----------编辑部分 顶部 ----------
<servlet>
<display-name>MyHttpServlet</display-name>
<servlet-name>MyHttpServlet</servlet-name>
<servlet-class>com.lo.MyHttpServlet</servlet-class>
<init-param>
<param-name>MyHeepServletConfig</param-name>
<param-value>MyHeepServletConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpServlet</servlet-name>
<url-pattern>/MyHttpServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>Database</param-name>
<param-value>MySql</param-value>
</context-param>
<context-param>
<param-name>USER</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>PASSWORD</param-name>
<param-value>root123456</param-value>
</context-param>
--------底部----------
<display-name>_20220930</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
// index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="MyHttpServlet" method="post">
<input type="text" name="Intocont" placeholder="请输入一条友善的弹幕">
<input type="submit" value="注册">
</form>
<!-- TODO 获取 时间和日期 -->
<tr>
<td><%=new java.util.Date().getHours()%>:<%=new java.util.Date().getMinutes()%>
</td>
<td></td>
<td style="float: right;"><%=new java.util.Date().getHours()%>-<%=new java.util.Date().getMinutes()%>
</td>
</tr>
</body>
</html>