web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  5. http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  6. version="4.0"
  7. metadata-complete="true">
  8. </web-app>

网页映射

  1. <servlet>
  2. <servlet-name>CookieDemo03</servlet-name>
  3. <servlet-class>com.dyq.servlet.CookieDemo03</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>CookieDemo03</servlet-name>
  7. <url-pattern>/c3</url-pattern>
  8. </servlet-mapping>

jsp初始化

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <body>
  4. <h2>Hello World!</h2>
  5. </body>
  6. </html>

处理前端乱码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


Servlet

  1. public class SessionDemo03 extends HttpServlet {
  2. @Override
  3. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  4. }
  5. @Override
  6. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  7. doGet(req, resp);
  8. }
  9. }

后台接收中文乱码问题 (请求和响应

  1. req.setCharacterEncoding("utf-8");
  2. resp.setContentType("text/html;charset=utf-8");

解决cookie乱码

编码:
Cookie cookie = new Cookie("name", URLEncoder.encode("段雨晴","utf-8"));
解码:
out.write(URLDecoder.decode(cookie.getValue(),"utf-8"));

pom+Maven

初始化只留这些,需要再Maven导包

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.dyq</groupId>
  6. <artifactId>javaweb-session-cookie</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>javaweb-session-cookie Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. </properties>
  17. </project>

Maven

Servlet依赖

  1. <dependencies>
  2. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  3. <dependency>
  4. <groupId>javax.servlet</groupId>
  5. <artifactId>javax.servlet-api</artifactId>
  6. <version>4.0.1</version>
  7. <scope>provided</scope>
  8. </dependency>
  9. <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
  10. <dependency>
  11. <groupId>javax.servlet.jsp</groupId>
  12. <artifactId>javax.servlet.jsp-api</artifactId>
  13. <version>2.3.3</version>
  14. <scope>provided</scope>
  15. </dependency>
  16. </dependencies>

MySQL连接依赖 测试依赖

  1. <!-- 单元测试-->
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>8.0.23</version>
  12. </dependency>

文件上传

image.png

  1. <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
  2. <dependency>
  3. <groupId>commons-fileupload</groupId>
  4. <artifactId>commons-fileupload</artifactId>
  5. <version>1.4</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
  8. <dependency>
  9. <groupId>commons-io</groupId>
  10. <artifactId>commons-io</artifactId>
  11. <version>2.8.0</version>
  12. </dependency>

邮箱依赖

  1. <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
  2. <dependency>
  3. <groupId>javax.mail</groupId>
  4. <artifactId>mail</artifactId>
  5. <version>1.4.7</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
  8. <dependency>
  9. <groupId>javax.activation</groupId>
  10. <artifactId>activation</artifactId>
  11. <version>1.1.1</version>
  12. </dependency>

JSTL表达式的依赖 / standard标签库

  1. <!-- JSTL表达式的依赖-->
  2. <dependency>
  3. <groupId>javax.servlet.jsp.jstl</groupId>
  4. <artifactId>jstl-api</artifactId>
  5. <version>1.2</version>
  6. </dependency>
  7. <!--standard标签库-->
  8. <dependency>
  9. <groupId>taglibs</groupId>
  10. <artifactId>standard</artifactId>
  11. <version>1.1.2</version>
  12. </dependency>