• Response常见应用


    1.

    客户机服务端口是随机的

    package com.hsp;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import jdk.nashorn.internal.ir.RuntimeNode.Request;
    
    /**
     * Servlet implementation class GetInfoServlet
     */
    @WebServlet("/GetInfoServlet")
    public class GetInfoServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public GetInfoServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
            //得到完整URL
            String uri=request.getRequestURI().toString();
            System.out.println("URI== "+uri);
            //uri 资源名称
            String url=request.getRequestURL().toString();
            System.out.println("URL=="+url);
            //传递信息部分 接收以get方式提交的数据
            String queryString=request.getQueryString();
            System.out.println("queryString=="+queryString);
            //getRemonteAdd获取对方的IP
            String add=request.getRemoteAddr();
            System.out.println(add);
    //if(add.equals("0:0:0:0:0:0:0:1")){//可用于拒绝他人的请求
    //    response.sendRedirect("/encoding/Err");
    //}
    //
    String host=request.getRemoteHost();
    System.out.println(host);
    int port=request.getRemotePort();
    int serverport=request.getLocalPort();
    System.out.println("客户机"+add+"使用的端口"+port);
    System.out.println("服务器使用的端口"+serverport);
    
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doGet(request, response);
        }
    
    }
    View Code

     下载功能的实现:

    getHeaderNames()  通过消息头获取

    把整个http请求获取:

    package com.hsp;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class GetRequestHeader
     */
    @WebServlet("/GetRequestHeader")
    public class GetRequestHeader extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public GetRequestHeader() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;utf-8");
            response.setCharacterEncoding("utf-8");
            PrintWriter out=response.getWriter();
            String host=request.getHeader("host");
            System.out.println(host);
            //getHeaderNames
            Enumeration<String> header=request.getHeaderNames();
            while(header.hasMoreElements()){
                //
                String headername=header.nextElement();
                System.out.println(headername+":"+request.getHeader(headername));
            }
            
        }
        
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    View Code

    package com.hsp;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import jdk.nashorn.internal.ir.RuntimeNode.Request;
    
    /**
     * Servlet implementation class GetInfoServlet
     */
    @WebServlet("/GetInfoServlet")
    public class GetInfoServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public GetInfoServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
            //得到完整URL
            String uri=request.getRequestURI().toString();
            System.out.println("URI== "+uri);
            //uri 资源名称
            String url=request.getRequestURL().toString();
            System.out.println("URL=="+url);
            //传递信息部分 接收以get方式提交的数据
            String queryString=request.getQueryString();
            System.out.println("queryString=="+queryString);
    //        //通过split函数实现分割
    //        String[] querString=queryString.split("&");
    //        for(String s:querString){
    //            out.println(s);
    //
    //         }
    //        //getRemonteAdd获取对方的IP
            String add=request.getRemoteAddr();
            System.out.println(add);
    //if(add.equals("0:0:0:0:0:0:0:1")){//可用于拒绝他人的请求
    //    response.sendRedirect("/encoding/Err");
    //}
            
    String host=request.getRemoteHost();
    System.out.println(host);
    int port=request.getRemotePort();
    int serverport=request.getLocalPort();
    System.out.println("客户机"+add+"使用的端口"+port);
    System.out.println("服务器使用的端口"+serverport);
    
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doGet(request, response);
        }
    
    }
    View Code

    getHeader方法:

    package com.hsp;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class GetRequestHeader
     */
    @WebServlet("/GetRequestHeader")
    public class GetRequestHeader extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public GetRequestHeader() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;utf-8");
            response.setCharacterEncoding("utf-8");
            PrintWriter out=response.getWriter();
            String host=request.getHeader("host");
            System.out.println(host);
            //getHeaderNames
            Enumeration<String> header=request.getHeaderNames();
            while(header.hasMoreElements()){
                //
                String headername=header.nextElement();
                System.out.println(headername+":"+request.getHeader(headername));
            }
            
        }
        
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    View Code

    control.java

    package com.hsp;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Controler
     */
    @WebServlet("/Controler")
    public class Controler extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public Controler() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            PrintWriter out=response.getWriter();
            String u=request.getParameter("username");
            String p=request.getParameter("password");
            String sex=request.getParameter("sex");
            //接收复选框的内容
            String[] hobby=request.getParameterValues("hobby");
            String city=request.getParameter("city");
            String intro=request.getParameter("intro");
            String hidden=request.getParameter("hidden1");
            
            out.println("用户名"+u);
            out.println("密码"+p);
            out.println("性别"+sex);
            if(hobby!=null){
            for(int i=0;i<hobby.length;i++){
                out.println(hobby[i]);
            }
            }else {
                out.print("重写");
            }
            out.println("<br/>他们所在的城市"+city);
            out.println("<br/>个人介绍"+intro);
            out.println("隐藏控件传递的数据是"+hidden);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doGet(request, response);
        }
    
    }
    View Code

    MyForm.java

    package com.hsp;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class MyFrom
     */
    @WebServlet("/MyFrom")
    public class MyFrom extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public MyFrom() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            PrintWriter out=response.getWriter();
            
            out.println("<form action='/encoding/Controler' method='post'>");
            out.println("用户名:<input type='text' name='username'/><br/>");
            out.println("密码:<input type='password' name='password'/><br/>");
            out.println("性别:<input type='radio' name='sex' value='男'/>男<input type='radio' name='sex' value='女'/>女<br/>");
            out.println("爱好:<input type='checkbox' name='hobby' value='音乐'/>音乐<input type='checkbox' name='hobby' value='跑步'/>跑步");
            out.println("所在城市:<select name='city'> <option value='北京'>北京</option> <option value='重庆'>重庆</option> </select>");
            out.println("<input type='submit' value='提交'/><br/>");
            out.println("你的介绍:<textarea cols='20' rows='10' name='intro' value='abc'></textarea><br/>");
            out.println("提交照片:<input type='file' name='photo'/><br/>");
            out.println("<input type='hidden' value='abc' name='hidden1'>");
            //hidden数据不愿意让用户看到,不希望影响界面,同时业务逻辑使用该数据
            out.println("</form>");
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doGet(request, response);
        }
    
    }
    View Code
  • 相关阅读:
    塔吊滑车移动
    csv合并某几列
    合并两个csv
    QFile
    python alphashape
    pip install econml
    csv增加一列
    连接redis数据库日志
    yolov5训练识别钢筋模型
    redis查看某个key的类型
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10996137.html
Copyright © 2020-2023  润新知