mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 14:40:55 +08:00
add c4-task
This commit is contained in:
parent
f71f5197c5
commit
f08ec28fa1
50
docs/tasks/chapter04-tasks.md
Normal file
50
docs/tasks/chapter04-tasks.md
Normal file
@ -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中使用这个类。
|
||||
- **添加错误页面**: 创建一个错误页面,在登录失败时重定向到该页面。
|
2
examples/chapter04/MANIFEST.MF
Normal file
2
examples/chapter04/MANIFEST.MF
Normal file
@ -0,0 +1,2 @@
|
||||
Manifest-Version: 1.0
|
||||
Created-By: openjdk 11.0.23
|
25
examples/chapter04/WEB-INF/web.xml
Normal file
25
examples/chapter04/WEB-INF/web.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
id="WebApp_ID" version="3.1">
|
||||
|
||||
<servlet>
|
||||
<servlet-name>com.lk.webapp.servlets.LoginServlet</servlet-name>
|
||||
<servlet-class>com.lk.webapp.servlets.LoginServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>com.lk.webapp.servlets.Login2Servlet</servlet-name>
|
||||
<servlet-class>com.lk.webapp.servlets.Login2Servlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>com.lk.webapp.servlets.LoginServlet</servlet-name>
|
||||
<url-pattern>/LoginServlet</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>com.lk.webapp.servlets.Login2Servlet</servlet-name>
|
||||
<url-pattern>/Login2Servlet</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
BIN
examples/chapter04/lib/javax.servlet-api-3.1.0.jar
Normal file
BIN
examples/chapter04/lib/javax.servlet-api-3.1.0.jar
Normal file
Binary file not shown.
18
examples/chapter04/login.jsp
Normal file
18
examples/chapter04/login.jsp
Normal file
@ -0,0 +1,18 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Login Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Login</h1>
|
||||
<form action="LoginServlet" method="post">
|
||||
帐号: <input type="text" name="username"><br>
|
||||
密码: <input type="password" name="password"><br>
|
||||
<input type="submit" value="登录1">
|
||||
</form>
|
||||
<% if (request.getAttribute("error") != null) { %>
|
||||
<p style="color:red;"><%= request.getAttribute("error") %></p>
|
||||
<% } %>
|
||||
</body>
|
||||
</html>
|
15
examples/chapter04/login2.jsp
Normal file
15
examples/chapter04/login2.jsp
Normal file
@ -0,0 +1,15 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Login Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Login</h1>
|
||||
<form action="Login2Servlet" method="post">
|
||||
帐号: <input type="text" name="username"><br>
|
||||
密码: <input type="password" name="password"><br>
|
||||
<input type="submit" value="登录2">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -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("<html><body>");
|
||||
writer.println("<h1>欢迎, " + username + "!</h1>");
|
||||
writer.println("<p>登录成功!</p>");
|
||||
writer.println("</body></html>");
|
||||
} else {
|
||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid credentials");
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
11
examples/chapter04/welcome.jsp
Normal file
11
examples/chapter04/welcome.jsp
Normal file
@ -0,0 +1,11 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>欢迎页面</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>热烈欢迎!</h1>
|
||||
<p><%= request.getAttribute("message") %></p>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user