diff --git a/docs/tasks/chapter04-tasks.md b/docs/tasks/chapter04-tasks.md new file mode 100644 index 0000000..0b31565 --- /dev/null +++ b/docs/tasks/chapter04-tasks.md @@ -0,0 +1,50 @@ +# 4. 基于Java的Web应用后端开发技术 + +## 任务目标 +- 实现一个简单的用户登录功能。 +- 使用Servlet技术处理用户的登录请求。 +- 理解MVC架构中的模型、视图和控制器的作用。 +- 学习如何在Servlet中处理HTTP请求和响应。 + +## 任务描述 +使用Servlet技术来实现一个用户登录功能。用户首先需要在登录页面输入用户名和密码,然后Servlet会验证这些信息是否正确。如果登录成功,则显示欢迎信息;如果失败,则显示错误提示。 + +## 技术栈 +- Java Servlet +- HTML +- JavaBean (可选) +- Apache Tomcat 作为Servlet容器 + +#### 任务步骤 + + + +1. **创建项目** + - 使用IntelliJ IDEA创建一个新的Java项目。 + +2. **编写登录页面 (`login.jsp`)** + + +3. **创建Servlet (`LoginServlet.java`)** + +4. **配置Servlet (`web.xml`)** + +5. **部署项目** + - 将项目导出为WAR文件。 + ```shell + jar cvfm my.war MANIFEST.MF -C . . + ``` + - 安装Tomcat。 + https://tomcat.apache.org/download-90.cgi + + - 将WAR文件放置在Tomcat的`webapps`目录下。 + - 重启Tomcat。 + +6. **测试** + - 访问 `http://localhost:8080/my/login.jsp`。 + - 输入用户名和密码,提交表单。 + - 观察不同的情况下的响应结果。 + +#### 扩展任务 +- **使用JavaBean**: 创建一个`User`类来封装用户名和密码,并在Servlet中使用这个类。 +- **添加错误页面**: 创建一个错误页面,在登录失败时重定向到该页面。 diff --git a/examples/chapter04/MANIFEST.MF b/examples/chapter04/MANIFEST.MF new file mode 100644 index 0000000..cec8377 --- /dev/null +++ b/examples/chapter04/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 +Created-By: openjdk 11.0.23 diff --git a/examples/chapter04/WEB-INF/web.xml b/examples/chapter04/WEB-INF/web.xml new file mode 100644 index 0000000..98cbce7 --- /dev/null +++ b/examples/chapter04/WEB-INF/web.xml @@ -0,0 +1,25 @@ + + + + + com.lk.webapp.servlets.LoginServlet + com.lk.webapp.servlets.LoginServlet + + + com.lk.webapp.servlets.Login2Servlet + com.lk.webapp.servlets.Login2Servlet + + + + com.lk.webapp.servlets.LoginServlet + /LoginServlet + + + com.lk.webapp.servlets.Login2Servlet + /Login2Servlet + + + \ No newline at end of file diff --git a/examples/chapter04/lib/javax.servlet-api-3.1.0.jar b/examples/chapter04/lib/javax.servlet-api-3.1.0.jar new file mode 100644 index 0000000..6b14c3d Binary files /dev/null and b/examples/chapter04/lib/javax.servlet-api-3.1.0.jar differ diff --git a/examples/chapter04/login.jsp b/examples/chapter04/login.jsp new file mode 100644 index 0000000..cfb2a6f --- /dev/null +++ b/examples/chapter04/login.jsp @@ -0,0 +1,18 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + Login Page + + +

Login

+
+ 帐号:
+ 密码:
+ +
+ <% if (request.getAttribute("error") != null) { %> +

<%= request.getAttribute("error") %>

+ <% } %> + + \ No newline at end of file diff --git a/examples/chapter04/login2.jsp b/examples/chapter04/login2.jsp new file mode 100644 index 0000000..bce4be6 --- /dev/null +++ b/examples/chapter04/login2.jsp @@ -0,0 +1,15 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + Login Page + + +

Login

+
+ 帐号:
+ 密码:
+ +
+ + \ No newline at end of file diff --git a/examples/chapter04/src/com/lk/webapp/servlets/Login2Servlet.java b/examples/chapter04/src/com/lk/webapp/servlets/Login2Servlet.java new file mode 100644 index 0000000..20e37f5 --- /dev/null +++ b/examples/chapter04/src/com/lk/webapp/servlets/Login2Servlet.java @@ -0,0 +1,36 @@ +package com.lk.webapp.servlets; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +@WebServlet("/Login2Servlet") +public class Login2Servlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=UTF-8"); + + String username = request.getParameter("username"); + String password = request.getParameter("password"); + + // 验证用户名和密码 + if ("admin".equals(username) && "123456".equals(password)) { + response.setStatus(HttpServletResponse.SC_OK); + PrintWriter writer = response.getWriter(); + writer.println(""); + writer.println("

欢迎, " + username + "!

"); + writer.println("

登录成功!

"); + writer.println(""); + } else { + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid credentials"); + } + } +} \ No newline at end of file diff --git a/examples/chapter04/src/com/lk/webapp/servlets/LoginServlet.java b/examples/chapter04/src/com/lk/webapp/servlets/LoginServlet.java new file mode 100644 index 0000000..46e5c38 --- /dev/null +++ b/examples/chapter04/src/com/lk/webapp/servlets/LoginServlet.java @@ -0,0 +1,32 @@ +package com.lk.webapp.servlets; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet("/LoginServlet") +public class LoginServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=UTF-8"); + + String username = request.getParameter("username"); + String password = request.getParameter("password"); + + // 验证用户名和密码 + if ("admin".equals(username) && "123456".equals(password)) { + request.setAttribute("message", "欢迎, " + username + "! 您已经成功登录."); + request.getRequestDispatcher("welcome.jsp").forward(request, response); + } else { + request.setAttribute("error", "Invalid credentials"); + request.getRequestDispatcher("login.jsp").forward(request, response); + } + } +} \ No newline at end of file diff --git a/examples/chapter04/welcome.jsp b/examples/chapter04/welcome.jsp new file mode 100644 index 0000000..08b990c --- /dev/null +++ b/examples/chapter04/welcome.jsp @@ -0,0 +1,11 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> + + + + 欢迎页面 + + +

热烈欢迎!

+

<%= request.getAttribute("message") %>

+ + \ No newline at end of file