3.2 偏后端内容
在Java Web开发中,后端技术主要涉及Servlet、JSP、Request、Response、HTTP、Session、Cookie、Filter和Listener等。
Servlet
Servlet是Java编写的服务器端程序,用于处理客户端请求并生成动态内容。Servlet运行在Servlet容器(如Tomcat)中。
创建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("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, World!</h1>");
}
}
JSP
JSP(JavaServer Pages)是一种用于创建动态Web页面的技术。JSP页面在服务器端被编译成Servlet。
创建JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Example</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>Current time: <%= new java.util.Date() %></p>
</body>
</html>
Request 和 Response
HttpServletRequest
和HttpServletResponse
是Servlet API中的两个核心接口,分别用于处理客户端请求和服务器响应。
获取请求参数
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, " + name + "!</h1>");
}
设置响应内容
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, World!</h1>");
}
HTTP
HTTP(HyperText Transfer Protocol)是用于在Web浏览器和服务器之间传输数据的协议。HTTP请求包括GET、POST、PUT、DELETE等方法。
GET 请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("<h1>GET Request</h1>");
}
POST 请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, " + name + "!</h1>");
}
Session
HttpSession
用于在服务器端存储用户会话数据。
创建和使用Session
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("username", "JohnDoe");
response.setContentType("text/html");
response.getWriter().println("<h1>Session created</h1>");
}
Cookie
Cookie是存储在客户端的小型文本文件,用于在客户端和服务器之间传递数据。
创建和使用Cookie
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("username", "JohnDoe");
response.addCookie(cookie);
response.setContentType("text/html");
response.getWriter().println("<h1>Cookie created</h1>");
}
Filter
Filter用于在请求到达Servlet之前或响应返回客户端之前对请求和响应进行预处理或后处理。
创建Filter
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter("/*")
public class MyFilter implements Filter {
public void init(FilterConfig config) throws ServletException {
// 初始化
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 预处理
System.out.println("Filter processing request");
// 继续处理请求
chain.doFilter(request, response);
// 后处理
System.out.println("Filter processing response");
}
public void destroy() {
// 销毁
}
}
Listener
Listener用于监听Web应用程序中的事件,如ServletContext、HttpSession和ServletRequest的生命周期事件。
创建Listener
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContext initialized");
}
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContext destroyed");
}
}
总结
在Java Web开发中,后端技术主要涉及Servlet、JSP、Request、Response、HTTP、Session、Cookie、Filter和Listener等。Servlet用于处理客户端请求并生成动态内容,JSP用于创建动态Web页面。Request和Response用于处理客户端请求和服务器响应。HTTP是用于在Web浏览器和服务器之间传输数据的协议。Session和Cookie用于在服务器端和客户端存储用户数据。Filter用于对请求和响应进行预处理或后处理,Listener用于监听Web应用程序中的事件。通过掌握这些后端技术,可以构建出功能强大、性能优越的Web应用程序。