public class httpRequest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// req.getRequestURI获取请求的资源路径
System.out.println("获取请求的资源路径为:"+req.getRequestURI());
// req.getRequestURI获取请求的绝对路径
System.out.println("获取请求的绝对路径为:"+req.getRequestURL());
// req.getRemoteHost获取客户端请求的ip
System.out.println("获取客户端请求的ip为:"+req.getRemoteHost());
// req.getHeader获取http协议的请求头
System.out.println("http协议的请求头为:"+req.getHeader("Use-agent"));
// req.getMethod获取客户端的请求方式
System.out.println("获取客户端的请求方式:"+req.getMethod());
}
}
2.获取请求参数
注意当有多个参数值的时候,使用getParameterValues(“body”)
public class from_servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("用户名"+req.getParameter("username"));
System.out.println("密码"+req.getParameter("password"));
// 当获取的某个参数值为多个的时候,使用getParameterValues
// 在打印的时候使用Arrays.asList
System.out.println("爱好"+ Arrays.asList(req.getParameterValues("body")));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
在获取post请求参数之前要设置请求及字符集为UTF-8,防止中文乱码问题
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//在获取请求参数之前要设置请求及字符集为UTF-8,防止中文乱码问题
req.setCharacterEncoding("UTF-8");
System.out.println("用户名"+req.getParameter("username"));
System.out.println("密码"+req.getParameter("password"));
// 当获取的某个参数值为多个的时候,使用getParameterValues
// 在打印的时候使用Arrays.asList
System.out.println("爱好"+ Arrays.asList(req.getParameterValues("body")));
}
代码实现
请求装发的特点
base的作用
原因
解决的思路是通过base标签