码迷,mamicode.com
首页 > Web开发 > 详细

HttpServletrequest、

时间:2019-07-03 13:35:59      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:insert   pac   acl   har   html   防盗链   else   ref   public   

 

HttpServletRequest

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/WEB06/LineServlet" method="get">
        <input type="text" name="username"><br>
        <input type="password" name="pwd"><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
------------------------------------
package com.oracle.demo01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LineServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//4.通过request获得请求行里面的信息
        //        获取请求方式
        String method=request.getMethod();
        System.out.println("请求方式为"+method);
//        获取请求URI
        String URI=request.getRequestURI();
        System.out.println("URL为"+URI);
//        获取URL
        StringBuffer URL=request.getRequestURL();
        System.out.println("URL为"+URL);
//        获取WEB应用名称
        String name=request.getContextPath();
        System.out.println("WEB应用名称为"+name);
//        获取get请求后url后的字符串
        String query=request.getQueryString();
        System.out.println("get请求参数为:"+query);
//        获取客户端的IP地址
        String ip=request.getRemoteAddr();
        System.out.println("ip地址为: "+ip);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

request获得请求头

referer头的作用 做防盗链

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 防盗链 -->
    <a href="/WEB06/RefereServlet">宋仲基和宋慧乔分手了</a>
</body>
</html>
--------------------------------
package com.oracle.demo01;
//通过request的 防盗链
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RefereServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        获取refere头
        String refere=request.getHeader("Referer");
        String content=null;
//        判断是否以谁开头  必须加上http://
        if(refere.startsWith("http://localhost:8080")){
            content="真的离婚了!";
        }else{
            content="你是小偷";
        }
//        解决response的中文乱码
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(content);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

通过request获得请求体

技术图片

以上面参数为例,通过一下方法获得请求参数:

String getParameter(String name)

String[] getParameterValues(String name)

Enumeration getParameterNames()

技术图片

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/WEB06/BodyServlet" method="post">
        <input type="text" name="username">
        <input type="radio" name="sex" value="woman"><input type="radio" name="sex" value="man"><br>
        <input type="checkbox" name="hobby" value="pqq">
        乒乓球
        <input type="checkbox" name="hobby" value="pq">
        皮球
        <input type="checkbox" name="hobby" value="wq">
        网球
        <br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
--------------------------------------------
package com.oracle.demo01;
//request获得请求体的方法获得参数
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.synth.SynthSeparatorUI;

public class BodyServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      post请求解决中文乱码问题  解决的是请求体里的乱码
        request.setCharacterEncoding("utf-8");
//        获取请求参数
//        1.获取单个值的参数  获取单个值就用 getParameter方法 
        String name=request.getParameter("username");
//      get提交方式解决乱码
        name=new String(name.getBytes("ISO-8859-1"),"UTF-8"); String sex
=request.getParameter("sex"); System.out.println(name+"..."+sex); // 2.获取多个值 String[] hobbys=request.getParameterValues("hobby"); // 遍历 for(String s:hobbys){ System.out.println(s); } // 3.获取所有请求参数Map Map<String,String[]> map=request.getParameterMap(); // 遍历 Set<String> set=map.keySet(); for(String str:set){ String[] value=map.get(str); for(String v:value){ System.out.println(str+"..."+v); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

技术图片

解决post提交中文乱码:request.setCharacterEncoding("UTF-8");

 技术图片

解决get提交乱码:parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");

一般人不会用这个 麻烦

技术图片

 

HttpServletrequest、

标签:insert   pac   acl   har   html   防盗链   else   ref   public   

原文地址:https://www.cnblogs.com/zs0322/p/11125656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!