cgb2110-day17
Posted cgblpx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cgb2110-day17相关的知识,希望对你有一定的参考价值。
文章目录
一,Request对象
–1,概述
1,用来接受请求,并且能够解析 请求参数.
GET方式: http://localhost:8080/user/userServlet?id=10&name=jack
POST方式: 数据不会在地址栏拼接的
2,常用的有两种:
ServletRequest 父接口
HttpServletRequest 子接口
–2,测试
package cn.tedu.cgb2110boot03.request;
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.util.Arrays;
@WebServlet
public class RequestServlet01 extends HttpServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
doGet(request, response);//和doGet干一样的活
//http://localhost:8080/RequestServlet01?name=jack&pwd=123
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
System.out.println("恭喜您,请求成功~~");
// Servlet解析请求参数HttpServletRequest
//1.getParameter()--根据参数名获取参数值
String s = request.getParameter("name");
String p = request.getParameter("pwd");
System.out.println(s+p);
//2.getParameterValues()--根据参数名获取参数值,可以获取多个值,存入数组
String[] datas = request.getParameterValues("like");
System.out.println( Arrays.toString(datas) );//[足球, 篮球]
//3.HttpServletRequest的其他API
System.out.println( request.getCharacterEncoding() );//获取请求使用的字符集UTF-8
System.out.println( request.getMethod() );//获取请求方式GET
System.out.println( request.getContextPath() );//获取项目名称(被缺省)
System.out.println( request.getRequestURI() );//要访问的资源路径/RequestServlet01
System.out.println( request.getRequestURL() );//包含着http://localhost:8080/RequestServlet01
System.out.println( request.getRemotePort() );//发起请求时的端口号
System.out.println( request.getQueryString() );//获取请求时携带的参数
二,练习Request解析请求参数
–0,项目结构
–1,创建html网页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试 Servlet解析请求参数</title>
</head>
<body>
<h1>1.以get方式提交的数据</h1>
<form action="http://localhost:8080/user/addServlet">
部门编号:<input type="number" name="deptno" placeholder="在这里输入部门编号.."/> <br />
部门名称:<input type="text" name="deptname" placeholder="在这里输入部门名称.."/> <br />
部门地址:<input type="text" name="deptloc" placeholder="在这里输入部门地址.."/> <br />
<button type="submit">提交</button>
<button type="reset">取消</button>
</form>
<h1>2.以post方式提交的数据</h1>
<form method="post" action="http://localhost:8080/user/addServlet">
账号:<input type="text" name="user" placeholder="在这里输入用户名.."/> <br />
密码:<input type="password" name="pwd" placeholder="在这里输入密码.."/> <br />
年龄:<input type="number" name="age" placeholder="在这里输入年龄.."/> <br />
<button type="submit">提交</button>
<button type="reset">取消</button>
</form>
</body>
</html>
–2,创建Servlet程序
package cn.tedu.cgb2110boot03.request;
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("/user/addServlet")
public class RequestServlet02 extends HttpServlet
//处理那些post请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
System.out.println("恭喜您,成功的提交了用户的数据!");
//TODO 解析请求参数
//TODO jdbc入库
//处理那些get请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
System.out.println("恭喜您,成功的提交了部门的数据!");
//TODO 解析请求参数
//TODO jdbc入库
三,改造Servlet程序实现入库
–1,导入jar包
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>cgb2110boot03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cgb2110boot03</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!-- 。。。。。。。。。添加 jdbc的jar包,需要指定坐标 。。。。。。。。。-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>$spring-boot.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>cn.tedu.cgb2110boot03.Cgb2110boot03Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
–2,修改Servlet程序
package cn.tedu.cgb2110boot03.request;
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.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@WebServlet("/user/addServlet")
public class RequestServlet02 extends HttpServlet
//处理那些post请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
System.out.println("恭喜您,成功的提交了用户的数据!");
//TODO 解析请求参数
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
String age = request.getParameter("age");
System.out.println(user+pwd+age);
//TODO jdbc入库
try
//1,导入jar包(在pom.xml里添加jar包的坐标依赖)
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url="jdbc:mysql:///cgb211001?characterEncoding=utf8";
Connection c = DriverManager.getConnection(url, "root", "root");
//获取传输器
String sql = "insert into user values(null,?,?,?)" ;//sql骨架
PreparedStatement ps = c.prepareStatement(sql);
ps.setObject(1,user);
ps.setObject(2,pwd);
ps.setObject(3,age);
//执行SQL
ps.executeUpdate();//执行增删改的SQL
//释放资源
ps.close();
c.close();
System.out.println("恭喜您,用户信息已经入库成功!");
catch (Exception e)
e.printStackTrace();
//处理那些get请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
System.out.println("恭喜您,成功的提交了部门的数据!");
//TODO 解析请求参数(部门编号,名称,地址)
String deptno = request.getParameter("deptno");
String deptname = request.getParameter("deptname");
String deptloc = request.getParameter("deptloc");
//TODO jdbc入库
try
//1,导入jar包(在pom.xml里添加jar包的坐标依赖)
//注册驱动
Class.forName(以上是关于cgb2110-day17的主要内容,如果未能解决你的问题,请参考以下文章