java获取response地址与参数_HttpServletResponse获取请求参数
![](https://box.kancloud.cn/33de5fd7d0e5041bb6452107d8bcc4a0_491x243.png)
HelloServlet.java
~~~
package zyw.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("接收到get请求");
//获取请求行
/* System.out.println("请求方式:"+req.getMethod());
System.out.println("URI:"+req.getRequestURI());
System.out.println("发出请求客户端IP地址:"+req.getRemoteAddr());
System.out.println("服务点接收请求的IP地址:"+req.getLocalAddr());
System.out.println("访问客户端的端口号:"+req.getRemotePort());
System.out.println("web应用路径:"+req.getContextPath());
System.out.println("http协议和版本:"+req.getProtocol());*/
//获取请求头
// Enumeration枚举类型
/* Enumeration headerNames = req.getHeaderNames();//自动补全变量名称 : Ctrl + Alt + v
while (headerNames.hasMoreElements()){
String element = headerNames.nextElement();
System.out.println(element+":"+req.getHeader(element));
}*/
//获取请求参数
String name = req.getParameter("name");
String passWord = req.getParameter("passWord");
System.out.println("用户名:"+name);
System.out.println("密码:"+passWord);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("接收到post请求");
}
}
~~~
get.html
~~~
Title发起HTTP get请求
账号:
密码:
~~~
![](https://box.kancloud.cn/e48e3bf8c3866c89d38be683f9666f79_1026x230.png)
结果:
![](https://box.kancloud.cn/a5ccf7fb038d22c0ff12206fc5459d7b_600x89.png)