Get请求与Post请求
请求方法
- Get方式是将数据通过在URL附加数据显性向服务器发送数据
Post方式会将数据存放在请求体中隐性向服务器发送数据
- 如:http://localhost:8080/testServlet/sample
- 请求体:name=occ
应用场景
Get常用于不包含敏感信息的查询
Post常用于安全性要求较高的功能或者服务器“写”操作
-
请求参数的发送与接收
请求参数是指浏览器通过请求向Tomcat提交的数据
- 请求参数通常是用户输入的数据,待Servlet进行处理
- 参数名1=值1&参数名2=值2&参数名n=值n=…
Servlet接收Get参数
```java package com.ouchaochao;
import javax.servlet.; import javax.servlet.http.; import javax.servlet.annotation.*; import java.io.IOException; import java.io.PrintWriter;
@WebServlet(name = “sampleServlet”, value = “/sampleServlet”) public class sampleServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String methodName = request.getMethod(); String name = request.getParameter(“name”); String mobile = request.getParameter(“mobile”); String sex = request.getParameter(“sex”); String[] specs = request.getParameterValues(“spec”);
PrintWriter out = response.getWriter();//向浏览器输出的数据流
out.println("<h1>method:" + methodName + "</h1>");
out.println("<h1>name:" + name + "</h1>");
out.println("<h1>mobile:" + mobile + "</h1>");
out.println("<h1>sex:" + sex + "</h1>");
for (int i = 0; i < specs.length; i++) {
out.println("<h2>spec:" + specs[i] + "</h2>");
}
}
}
<a name="HC203"></a>
#### web.xml配置
```java
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.ouchaochao.helloServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>com.ouchaochao.sampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/sample</url-pattern>
</servlet-mapping>
</web-app>
student.html配置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学员信息登记表</title>
</head>
<body>
<h1>学员信息登记表</h1>
<form action="/testServlet/sample" method="get" >
姓名:<input name="name"/>
<br/>
电话:<input name="mobile"/>
<br/>
性别:
<select name="sex" style="width:100px;padding:5px;">
<option value="male" >男</option>
<option value="female">女</option>
</select>
<br/>
特长:
<input type="checkbox" name="spec" value="English"/>英语
<input type="checkbox" name="spec" value="Program"/>编程
<input type="checkbox" name="spec" value="Speech"/>演讲
<input type="checkbox" name="spec" value="Swimming"/>游泳
<br/>
<input type="submit" value="提交">
<br/>
</form>
</body>
</html>
结果展示
Servlet接受Post参数
package com.ouchaochao;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "sampleServlet", value = "/sampleServlet")
public class sampleServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodName = request.getMethod();
String name = request.getParameter("name");
String mobile = request.getParameter("mobile");
String sex = request.getParameter("sex");
String[] specs = request.getParameterValues("spec");
PrintWriter out = response.getWriter();//向浏览器输出的数据流
out.println("<h1>method:" + methodName + "</h1>");
out.println("<h1>name:" + name + "</h1>");
out.println("<h1>mobile:" + mobile + "</h1>");
out.println("<h1>sex:" + sex + "</h1>");
for (int i = 0; i < specs.length; i++) {
out.println("<h2>spec:" + specs[i] + "</h2>");
}
}
}
web.xml配置
与Get的XML相同
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.ouchaochao.helloServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>com.ouchaochao.sampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/sample</url-pattern>
</servlet-mapping>
</web-app>
student.html配置
修改了
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学员信息登记表</title>
</head>
<body>
<h1>学员信息登记表</h1>
<form action="/testServlet/sample" method="post" >
姓名:<input name="name"/>
<br/>
电话:<input name="mobile"/>
<br/>
性别:
<select name="sex" style="width:100px;padding:5px;">
<option value="male" >男</option>
<option value="female">女</option>
</select>
<br/>
特长:
<input type="checkbox" name="spec" value="English"/>英语
<input type="checkbox" name="spec" value="Program"/>编程
<input type="checkbox" name="spec" value="Speech"/>演讲
<input type="checkbox" name="spec" value="Swimming"/>游泳
<br/>
<input type="submit" value="提交">
<br/>
</form>
</body>
</html>
结果展示
使用注解配置Servlet
可能有同学注意到了,在Servlet中有句注解,长的就像下面这个样子
@WebServlet(name = “sampleServlet”, value = “/sampleServlet”)
当在Servlet中写了有注解的情况下,我们不配置XML,也可以达到访问的效果,例如:
package com.ouchaochao;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "Anno", value = "/Anno")
public class Anno extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("I am annotation");
}
}
注
- 注解简化了Web应用程序的配置过程
- 使用注解配置了Servlet以后,web.xml文件中就不可以再配置相同访问的url,否则会报错;但是可以针对同一个Servlet再指定一个不同的访问URL