mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 22:50:54 +08:00
Compare commits
2 Commits
68036264bd
...
51ceb6f00f
Author | SHA1 | Date | |
---|---|---|---|
51ceb6f00f | |||
10d021a5d1 |
BIN
docs/resources/imgs/servlet.png
Normal file
BIN
docs/resources/imgs/servlet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 138 KiB |
@ -21,29 +21,76 @@
|
|||||||
|
|
||||||
1. **创建项目**
|
1. **创建项目**
|
||||||
- 使用IntelliJ IDEA创建一个新的Java项目。
|
- 使用IntelliJ IDEA创建一个新的Java项目。
|
||||||
|
- 根据servlet项目要求,创建目录和文件。
|
||||||
|
```markdown
|
||||||
|
project_name
|
||||||
|
|-- src
|
||||||
|
|-- webapp
|
||||||
|
|-- WEB-INF
|
||||||
|
|-- classes
|
||||||
|
|-- (源码编译后的class文件)
|
||||||
|
|-- lib
|
||||||
|
|-- (第三方库的jar文件)
|
||||||
|
|-- web.xml
|
||||||
|
|-- index.jsp
|
||||||
|
|-- other.jsp
|
||||||
|
|-- css
|
||||||
|
|-- style.css
|
||||||
|
|-- js
|
||||||
|
|-- script.js
|
||||||
|
|-- images
|
||||||
|
|-- logo.png
|
||||||
|
```
|
||||||
|
- 在IDEA中对项目进行必要的设置,例如:JDK版本、class文件输出路径等。
|
||||||
|
|
||||||
2. **编写登录页面 (`login.jsp`)**
|
2. **编写登录页面 (`login.jsp`和`welcome.jsp`)**
|
||||||
|
|
||||||
|
|
||||||
3. **创建Servlet (`LoginServlet.java`)**
|
3. **创建Servlet (`LoginServlet.java`)**
|
||||||
|
|
||||||
4. **配置Servlet (`web.xml`)**
|
4. **配置Servlet (`web.xml`)**
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<?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-mapping>
|
||||||
|
<servlet-name>com.lk.webapp.servlets.LoginServlet</servlet-name>
|
||||||
|
<url-pattern>/LoginServlet</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</web-app>
|
||||||
|
```
|
||||||
5. **部署项目**
|
5. **部署项目**
|
||||||
- 将项目导出为WAR文件。
|
- 将项目导出为WAR文件。
|
||||||
|
在idea中,打开控制台:
|
||||||
```shell
|
```shell
|
||||||
jar cvfm my.war MANIFEST.MF -C . .
|
cd webapp
|
||||||
|
jar cvf my.war -C . .
|
||||||
```
|
```
|
||||||
- 安装Tomcat。
|
- 安装Tomcat。
|
||||||
https://tomcat.apache.org/download-90.cgi
|
https://tomcat.apache.org/download-90.cgi
|
||||||
|
|
||||||
- 将WAR文件放置在Tomcat的`webapps`目录下。
|
- 将my.war文件放置在Tomcat的`webapps`目录下。
|
||||||
- 重启Tomcat。
|
- 启动Tomcat
|
||||||
|
```shell
|
||||||
|
cd <自己的tomcat解压所在的目录/bin>
|
||||||
|
.\startup.bat
|
||||||
|
```
|
||||||
|
|
||||||
6. **测试**
|
6. **测试**
|
||||||
- 访问 `http://localhost:8080/my/login.jsp`。
|
- 访问 `http://localhost:8080/my/login.jsp`。
|
||||||
- 输入用户名和密码,提交表单。
|
- 输入用户名和密码,提交表单
|
||||||
- 观察不同的情况下的响应结果。
|
|
||||||
|
|
||||||
#### 扩展任务
|
#### 扩展任务
|
||||||
- **使用JavaBean**: 创建一个`User`类来封装用户名和密码,并在Servlet中使用这个类。
|
- **使用JavaBean**: 创建一个`User`类来封装用户名和密码,并在Servlet中使用这个类。
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
Manifest-Version: 1.0
|
|
||||||
Created-By: openjdk 11.0.23
|
|
@ -8,7 +8,7 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
@WebServlet("/Login2Servlet")
|
//@WebServlet("/Login2Servlet")
|
||||||
public class Login2Servlet extends HttpServlet {
|
public class Login2Servlet extends HttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
@ -11,7 +11,7 @@ import java.io.BufferedReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
@WebServlet("/Login3Servlet")
|
//@WebServlet("/Login3Servlet")
|
||||||
public class Login3Servlet extends HttpServlet {
|
public class Login3Servlet extends HttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
@ -7,7 +7,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@WebServlet("/LoginServlet")
|
//@WebServlet("/LoginServlet")
|
||||||
public class LoginServlet extends HttpServlet {
|
public class LoginServlet extends HttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
<?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>
|
|
||||||
<servlet-name>com.lk.webapp.servlets.Login3Servlet</servlet-name>
|
|
||||||
<servlet-class>com.lk.webapp.servlets.Login3Servlet</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>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>com.lk.webapp.servlets.Login3Servlet</servlet-name>
|
|
||||||
<url-pattern>/Login3Servlet</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
</web-app>
|
|
Loading…
Reference in New Issue
Block a user