您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
10_Request_获取请求行数据_代码演示
发布时间:2022-08-07 18:22:22编辑:雪饮阅读()
自动创建servlet
在项目中原本我们是需要新建Java Class然后去继承HttpServlet,那么如果我们一开始就直接New=>Create New Servlet
则在输入了类名后
会发现上面继承HttpServlet的工作自动给你完成了,并且默认就帮你覆写了最常用的doPost和doGet方法,只是我们需要修改下这里注解,把注解中“name =”去除然后给这个urlpartten加上“/”即可
request获取请求行数据
其实就是获取请求数据,说请求行,反而太过谨慎而有点不合适的感觉。
这里主要以获取请求方式、虚拟目录、查询字符串、URI、protocol及版本、远端地址、servlet地址等。
具体的实例如:ServletDemo1.java:
package day13;
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("/ServletDemo1")
public class ServletDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求方式:"+request.getMethod());
System.out.println("虚拟目录:"+request.getContextPath());
System.out.println("queryString:"+request.getQueryString());
System.out.println("获取URI(域名及端口后面那部分):"+request.getRequestURI());
System.out.println("protocol(http协议及版本):"+request.getProtocol());
System.out.println("获取远端(访问端)的地址:"+request.getRemoteAddr());
System.out.println("获取servlet路径:"+request.getServletPath());
}
}
则为了能够有查询字符串被获取到值,这里Restart Server之后,我们的访问url如:
http://localhost:8080/untitled3_war_exploded/ServletDemo1?a=1&b=2&c=3
然后控制台output输出如:
请求方式:GET
虚拟目录:/untitled3_war_exploded
queryString:a=1&b=2&c=3
获取URI(域名及端口后面那部分):/untitled3_war_exploded/ServletDemo1
protocol(http协议及版本):HTTP/1.1
获取远端(访问端)的地址:0:0:0:0:0:0:0:1
获取servlet路径:/ServletDemo1
关键字词:Request,获取,请求行,数据,代码,演示
上一篇:08_Request_继承体系
相关文章
- 08_Request_继承体系
- 05_HTTP_请求消息_请求行
- 14_JDBC工具类(从配置文件中加载数据库配置)
- 05_JDBC各个类详解_DriverManager_获取数据库连接
- 踩坑系列-php的max_input_vars, php post 最大接收数
- 解決avformat_open_input出現(返回錯誤(code為99(-99)
- webman-session管理-判断对应session数据是否存在
- webman-session管理-删除所有session数据
- workerman的http服务-session会话-判断对应session数
- workerman的http服务-session会话-删除所有session数