HttpServletrequest、

 

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("Request mode is" + Method);
 //         Get request the URI 
        String the URI = Request.getRequestURI (); 
        System.out.println ( "as the URL" + the URI);
 //         Get the URL 
        the StringBuffer the URL = request.getRequestURL (); 
        System.out.println ( "as the URL of" + the URL of);
 //         Gets WEB application name 
        String name = request.getContextPath (); 
        System.out.println ( "WEB application name is" + name);
 //         get get request after string URL 
        string Query = request.getQueryString (); 
        System.out.println ( "GET request parameters:" + Query);
//         Get the IP address of the client 
        String IP = request.getRemoteAddr (); 
        System.out.println ( "IP address:" + IP); 
    } 

    public  void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { 
        the doGet ( Request, Response); 
    } 
}

request acquisition request header

The role of anti-hotlinking do referer header

<%@ 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;
javax.servlet.ServletException;
Importjava.io.IOException;
Importby anti-hotlinking request of//

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 = "really divorced!" ; 
        } The else { 
            Content = "You are a thief" ; 
        } 
//         solve the Chinese garbled response 
        response.setContentType ( "text / HTML; charset = UTF-8" ); 
        response .getWriter () Write (Content);. 
    } 

    public  void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { 
        the doGet (Request, Response); 
    } 
}

Obtaining request through the request body

To the above parameters, for example, access to request parameters by what method:

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;
importjavax.servlet.http.HttpServletResponse;
 Import javax.swing.plaf.synth.SynthSeparatorUI; 

public  class BodyServlet the extends the HttpServlet { 

    public  void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
 //       POST request addressed to solve the Chinese garbled is a distortion in the request body 
        Request.setCharacterEncoding ( "UTF-. 8" );
 //         Get request parameter
 //         1. obtain a single value parameter value is used to retrieve a single method getParameter 
        String name = request. getParameter ( "username" ); 
//      GET submission solve the garbage
        = new new String name (name.getBytes ( "the ISO-8859-1"), "UTF-. 8"); String Sex
= request.getParameter ( "Sex" ); System.out.println (name + "..." + Sex); // 2. obtaining a plurality of values String [] = hobbys Request. the getParameterValues ( "Hobby" ); // iterate for (String S: hobbys) { System.out.println (S); } // . 3 Get all request parameters the Map the Map <String, String []> = Map request. getParameterMap (); // iterate the 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); } }

Resolve post submission Chinese garbled : request.setCharacterEncoding ( "UTF-8" );

 

Resolve get submitted garbled : parameter = new String (parameter.getbytes ( "iso8859-1"), "utf-8");

Most people will not bother with this

 

Guess you like

Origin www.cnblogs.com/zs0322/p/11125656.html
Recommended