HttpServletRequest学习
package cn.request; import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.Map; import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.commons.beanutils.BeanUtils; import cn.response.com.ResDemo2; public class ReqDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { test8(request,response);
} //利用requset 设置请求头 referer 放盗链
private void test8(HttpServletRequest request, HttpServletResponse response) throws IOException {
String data = "aaaa";
String referer = request.getHeader("referer"); if (referer == null || !referer.startsWith("http://localhost")) {
response.sendRedirect("/Test/index.jsp"); // 如果不是从我的网站来请求的资源,那就跳到主页, 然胡再在主页提供连接再回来请求
} else {
response.getWriter().print(data);
}
} //跳转之前数据不能写给客户端 java.lang.IllegalStateException: Cannot forward after response has been committed
//forward会清空response中的数据
//请求转发特点: 1: 客户端只发送一次请求,而服务端会产生多个资源的调用 2:客户机地址栏没有变化 private void test7(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String data = "aa";
PrintWriter pw = response.getWriter();
pw.write(data);
pw.close(); request.getRequestDispatcher("/Test/index.jsp").forward(request, response); } //request的请求转发 实现MVC框架的设计思想
private void test6(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data = "aaa";
HttpSession session = request.getSession();
session.setAttribute("data", data);
request.getRequestDispatcher("index.jsp").forward(request, response);
} //解决中文乱码问题
private void test5(HttpServletRequest request) throws UnsupportedEncodingException {
/*request.setCharacterEncoding("UTF-8"); //只对post提交有效
String name = request.getParameter("name");
System.out.println("name = " + name);*/ //手工解决都get post 都可以..
String name = request.getParameter("name");
String myName = new String(name.getBytes("ISO-8859-1"),"UTF-8");
System.out.println("myName = " + myName);
} //利用request收集用户提交的数据
private void test4(HttpServletRequest request) {
String username = request.getParameter("username");
System.out.println("username = " + username); String password = request.getParameter("password");
System.out.println("password = " + password); String gender = request.getParameter("gender");
System.out.println("gender = " + gender); String address = request.getParameter("address");
System.out.println("address = " + address); String[] likes = request.getParameterValues("likes");
for (int i = 0; likes != null && i < likes.length; ++i) {
System.out.println("likes :" + likes[i]);
} String des = request.getParameter("des");
System.out.println("des = " + des); String id = request.getParameter("id");
System.out.println("id = " + id);
} //获得请求数据
private void test3(HttpServletRequest request) {
String name1 = request.getParameter("name"); Enumeration<String> e = request.getParameterNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String value = request.getParameter(name);
System.out.println("name = " + name + " value = " + value);
}
String[] v = request.getParameterValues("name");
for (int i = 0; v != null && i < v.length; ++i) {
System.out.println(v[i]);
}
Map<String, String[]> entity = request.getParameterMap();
User u = new User();
//这样可以利用BeanUtils 简单操作
try {
BeanUtils.populate(u, entity);
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvocationTargetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} //获取请求头
private void test2(HttpServletRequest request) {
String headValue = request.getHeader("Accept-Encoding");
System.out.println(headValue); Enumeration<String> e = request.getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
String value = e.nextElement();
System.out.println(value);
} e = request.getHeaderNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String value = request.getHeader(name);
System.out.println("name = " + name + " value = " + value);
}
} //获得用户请求的URI和URL 可以实现用户权限的控制, 统计某个web资源被访问的次数
private void test1(HttpServletRequest request) {
System.out.println("URI : " + request.getRequestURI());
System.out.println("URL : " + request.getRequestURL());
http://localhost:8080/Test/ReqDemo1?name=gbx
System.out.println("查询参数: " + request.getQueryString());
System.out.println("IP :" + request.getRemoteAddr());
System.out.println("Port :" + request.getRemotePort());
System.out.println("Host :" + request.getRemoteHost());
System.out.println("客户机请求方式 :" + request.getMethod());
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} } class User{ }
这里是测试request手机数据时的jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<meta http-equiv="content-tyoe" content="text/html; charset=UTF-8">
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="/Test/ReqDemo1" method="post">
用户名: <input type="text" name="username"> <br/>
密码: <input type="password" name="password"> <br/>
性别 : <input type="radio" name="gender" value="男"><br/>
<input type="radio" name="gender" value="女"><br/>
住址: <select name="address">
<option value="bj">北京</option>
<option value="sd">山东</option>
</select> <br/> 爱好: <input type="checkbox" name="likes" value="bas">篮球
<input type="checkbox" name="likes" value="sing">唱歌
<input type="checkbox" name="likes" value="dance">跳舞
<br/>
<input type="hidden" name = "id" value="123"> <br/> 简介:<textarea rows="6" cols="60" name="des"></textarea><br/>
照片: <input type="file" name="image"><br/> <input type="submit" value="提交">
</form>
</body> </html>
利用Http头的referer 实现图片的防盗链, 不是本网张的不给显示
jsp
<body>
<img alt="" src="/Test/ImgServlet?filename=1.jpg">
</body>
Servlet
package cn.request; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ImgServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String referer = request.getParameter("referer");
//如果非法就不给资源
if (referer == null || !referer.startsWith("http://localhost")) {
return ; } else { //合法
String filename = request.getParameter("filename");
System.out.println(referer + " " + filename);
String path = this.getServletContext().getRealPath("/download/" + filename);
System.out.println("path : " + path);
InputStream in = new FileInputStream(path);
OutputStream out = response.getOutputStream();
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
HttpServletRequest学习的更多相关文章
- 【JAVAWEB学习笔记】15_request
HttpServletRequest 学习目标 案例一.完成用户注册 案例二.完成登录错误信息的回显 1.HttpServletRequest概述 我们在创建Servlet时会覆盖service()方 ...
- [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Java第三阶段学习(十二、HttpServletRequest与HttpServletResponse)
一.HttpServletRequest 1.概述: 我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和 ...
- java WEB学习笔记32:HttpSession 接口常用方法 及 HttpServletRequest接口中的Session方法 Demo
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记11:HttpServlet(HttpServletRequest HttpServletRsponse) 以及关于 Servlet 小结
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创]java WEB学习笔记08:HttpServletRequest & ServletRequest
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- java web学习总结(十) -------------------HttpServletRequest对象
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- javaweb学习总结(十)——HttpServletRequest对象(一)
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- JavaWeb学习总结(七)—HttpServletRequest
一.Request概述 request是Servlet.service()方法的一个参数,类型为javax.servlet.http.HttpServletRequest.在客户端发出每个请求时,服务 ...
随机推荐
- Java中删除指定文件夹文件夹下面有内容也删除使用递归方案
import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java ...
- OS开发拓展篇—应用之间的跳转和数据传
iOS开发拓展篇-应用之间的跳转和数据传 说明:本文介绍app如何打开另一个app,并且传递数据. 一.简单说明 新建两个应用,分别为应用A和应用B. 实现要求:在appA的页面中点击对应的按钮,能够 ...
- C#:文件、文件夹特别操作
1.过滤特殊字符 public class CharService:IDisposable { private List<char> _invalidChars; public CharS ...
- js获取随机色
方法一: var getRandomColor = function(){ return '#' + (function(color){ return (color += '0123456789abc ...
- js post提交页面
function post(URL, PARAMS) { var temp = document.createElement("form"); temp.action = URL; ...
- Scrapy集成selenium+PhantomJS+代理IP 解析js动态内容
转载于:http://blog.aizhet.com/web/16523.html
- php 日期时间操作-可算出几天后的时间
本文为大家介绍一下根据PHP时间戳获取当前时期的具体方式.strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实 ...
- sql通用分页自定义表条件存储过程
create PROCEDURE PrcTestByPage ( @tablename varchar(50), @selectfilter varchar(100), @orderbyfilter ...
- django下的ckeditor 5.0 文本编辑器上传功能。
完整的后台界面怎么可以没有文本编辑器,但是django的admin界面很疑惑,没有自带文本编辑器,好在网上有不少成型的库可以用 我用的是ckeditor编辑器,安装和配置我引用别人的博客 这篇博客配置 ...
- java初始化笔记
构造器初始化static块与非static块区别:对象创建时都做块的初始化,非static块仅在实例创建时初始化,static块在类被第一次加载时初始化数组初始化注意点:1.数组定义时最好在定义的同时 ...