Java web 常用方法/类 ---- 第二天

继:Java web 常用方法/类 ---- 第一天

6.编码设置:setCharacterEncoding
//java
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//一般放在第一句
	response.setCharacterEncoding("utf-8");
}

7.接收post和get请求:service
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setCharacterEncoding("utf-8");
	…………
}

以上两种请求的方式登录下面这一种请求

//可以客户端发来的get或者post请求都可以接收
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setCharacterEncoding("utf-8");
	…………
}
8.获取访问服务器用户的信息
//获取客户端的IP地址的方法是
System.out.println(request.getRemoteAddr());//但是客户在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了
//获取客户端的主机名
System.out.println(request.getRemoteHost());
//获取发出请求的客户的端口号
System.out.println(request.getRemotePort());

在这里插入图片描述

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页