学习笔记,仅供参考,有错必纠
文章目录
- 举个例子
- 举个例子
request
Request的功能
//1.获取请求方式
String getMethod()
//2.获取虚拟目录
String getContextPath()
//3.获取Servlet路径
String getServletPath()
//4.获取Get方式请求参数
String getQueryString()
//5.获取请求UrI
//String getRequestURI()
//5.获取请求UrL
//String getRequestURL()
//6.获取协议及版本
String getProtocol()
//7.获取客户机IP地址
String getRemoteAddr()
举个例子
利用postman向如下url发送post请求:
http://localhost:8080/ServletDemo/demo05?name=goatbishop
控制台输出如下结果:
获取请求方式 : GET
获取虚拟目录 : /ServletDemo
获取Servlet路径 /demo05
获取Get请求参数 : name=goatbishop
获取请求URI : /ServletDemo/demo05
获取请求URL : http://localhost:8080/ServletDemo/demo05
获取协议及版本 : HTTP/1.1
获取客户机IP地址 : 0:0:0:0:0:0:0:1
获取请求头数据 : PostmanRuntime/7.25.0
获取请求参数通用方式(get,post都可用)
根据参数名称获取参数值:
String getParameter(String name)
根据参数名称获取参数值数组,通常用于复选框之类:
String[] getParametervalues(String name)
举个例子
- 例子1
package com.servlet.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/demo06")
public class Demo06 extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(req, resp);
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
//获取参数名为user_name的参数值
System.out.println(req.getParameter("user_name"));
}
}
向http://localhost:8080/ServletDemo/demo06?user_name=goatbishop发送POST请求:
控制台输出:
goatbishop
- 例子2
package com.servlet.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
("/demo07")
public class Demo07 extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String[] bunny = req.getParameterValues("bunny");
for (String b : bunny) {
System.out.println(b);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
}
}
向http://localhost:8080/ServletDemo/demo07?bunny=Huang&bunny=Bai发起请求:
控制台输出:
Huang
Bai
中文乱码问题
Tomcat8已经把get方式的中文乱码问题解决了
Post方式乱码解决:
//在获取参数前设置request的编码,使之和页面保持一致
Request.setCharacterEncoding(“utf-8”)
请求转发
图式:
请求转发是一种在服务器内部的资源跳转方式:
req.getRequestDispatcher(String path).forward(ServletRequest request,ServletResponse response);
请求转发的特点
- 浏览器地址栏路径不发生变化
- 只能转发到当前服务器内部资源
- 转发是一次请求
举个例子
Demo08.html
package com.servlet.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
("/demo08")
public class Demo08 extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
System.out.println("这是demo08");
req.getRequestDispatcher("/demo09").forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(req, resp);
this.doGet(req, resp);
}
}
Demo09.html:
package com.servlet.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
("/demo09")
public class Demo09 extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
System.out.println("这是demo09");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(req, resp);
this.doGet(req, resp);
}
}
向http://localhost:8080/ServletDemo/demo08发起POST请求,得到控制台输出:
这是demo08
这是demo09