request对象转发的使用
该对象主要处理客户端的请求(一次请求)
包含方法
- getParameter() 根据表单组件名称获取提交数据
- getParameterValues() 获取表单组件对应多个值时的请求数据
- setCharacterEncoding() 指定每个请求的编码
- getRequestDispatcher() 返回一个RequestDispatcher对象,该对象的forward()方法用于转发请求
Dispatcher就是分发,转发
request只在当前的栈内进行跳转,即转发,相对来讲快一点,且可以携带数据。
sendredirect()会发起两次请求,第一次请求,第二次跳转,性能相对较低
response:处理响应客户请求,并向客户端输出信息
sendredirect():将请求重新定位到一个不同的URL,即页面重定向
setCharacterEncoding() :设置响应的编码格式
重定向不会带值跳转,因为安全性不允许,request请求会被其他网站拦截
两者区别
转发:URL 地址是不变的。内部资源完成一个跳转过程能够通过 request 实现带值跳转请求发起后,跳转到目标过程只发起一次请求请求跳转速度又有是在站内,所以速度也比较快。
重定向:URL 地址会发生改变。由浏览器发起两次 请求,第一次是请求的目标(30x) ,第二次会有浏览器跳转到目标页面,该过程是一个重定向的过程。不能够通过 request 实现带值跳转。由于两次跳转,所以速度相比较转发稍慢
session :代表一次会话。
setAttribute 以key/value的形式保存对象值
getAtrribute 从session范围内获取到某个对象
removeAttribute 从session范围内,删除一个对象
invalidate() 将所有的session全部设置失效
setMaxInactiveInterval 设置session最大非活动时间,单位是秒
getMaxInactiveInterval 获取到最大的session时长
getId
session
的
id
名称为
JSESSIONID
,该
id
默认情况下,是由cookie
创建的。如果cookie被禁用,那么就会通过容器创建一个对应唯一性id
,交给
session
,作为sessionid。
application :应用
MAVEN项目的搭建
构建web工程......
配置
pom.xml
的文件,添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>newsmgrsys</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> </build> </project>
连接
package com.csi.newsmgrsys.utils; import java.sql.*; /** * JDBC资源管理工具类 */ public class JDBCUtils { /** * 创建连接 * java.sql.Connection */ public Connection getConnection() throws SQLException { try { Class.forName("com.mysql.cj.jdbc.Driver") ; } catch (ClassNotFoundException e) { e.printStackTrace(); } //2. 建立连接 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool","root","root") ; return connection ; } /** * 释放连接 */ protected void release(Connection connection) { if(connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } protected void release(Statement statement) { if(statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } } protected void release(ResultSet rs) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 增删改实现关闭 * @param ps * @param connection */ protected void release(PreparedStatement ps,Connection connection) { release(ps); release(connection); } /** * 查询关闭 * @param rs * @param ps * @param connection */ protected void release(ResultSet rs, PreparedStatement ps, Connection connection) { release(rs); release(ps,connection); } }