沂南建设局网站,wordpress科技,战略咨询公司,网站建设功能解说一、概述
1、Request与Response Request 对象 和 Response 对象是由服务器#xff08;Tomcat#xff09;创建的Request 对象是来获取请求消息Response 对象是来设置响应消息 2、Request 体系结构 3、Servlet 体系结构 二、HttpServletRequest HttpServletRequest对象代表客户…一、概述
1、Request与Response Request 对象 和 Response 对象是由服务器Tomcat创建的Request 对象是来获取请求消息Response 对象是来设置响应消息 2、Request 体系结构 3、Servlet 体系结构 二、HttpServletRequest HttpServletRequest对象代表客户端的请求当客户端通过HTTP协议访问服务器时HTTP请求头中的所有信息都封装在这个对象中通过这个对象提供的方法可以获得客户端请求的所有信息
三、request 获取请求行的数据
先写一个 input.html
!DOCTYPE html
html langen
headmeta charsetUTF-8title文本输入框/title
/head
body
form actionservletRequestDemo01 methodgetinput typetext nameusername valuenamebrinput typetext namepassword valuepasswordbrinput typesubmit valueget 提交
/formbr
form actionservletRequestDemo01 methodpostinput typetext nameusername valuenamebrinput typetext namepassword valuepasswordbrinput typesubmit valuepost 提交
/formbr
/body
/html
再写一个 Servlet 类 类名为ServletRequestDemo01.java
package cn.lemon.request;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 java.io.IOException;WebServlet(/servletRequestDemo01)
public class ServletRequestDemo01 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding(utf-8);//解决请求乱码response.setCharacterEncoding(utf-8);//解决响应乱码doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取请求方式GetString method request.getMethod();System.out.println(获取的请求方式为 method);//获取虚拟目录String contextPath request.getContextPath();System.out.println(获取的虚拟目录为 contextPath);//获取请求的路径String servletPath request.getServletPath();System.out.println(获取请求的路径为 servletPath);//获取Get方式的请求参数String queryString request.getQueryString();System.out.println(获取Get方式的请求参数为 queryString);//获取URI相对服务器路径 及 URL全路径String requestURI request.getRequestURI();System.out.println(获取URI相对服务器路径为 requestURI);StringBuffer requestURL request.getRequestURL();System.out.println(获取URL全路径为 requestURL);//获取HTTP协议及版本号String protocol request.getProtocol();System.out.println(获取HTTP协议及版本号 protocol);//获取客户机的IP地址String remoteAddr request.getRemoteAddr();System.out.println(获取客户机的IP地址 remoteAddr);}
}
控制台显示 总结 String method request.getMethod();获取请求方式GetString contextPath request.getContextPath();获取虚拟目录String servletPath request.getServletPath();获取请求的路径String queryString request.getQueryString();获取Get方式的请求参数String requestURI request.getRequestURI();获取URI相对服务器路径StringBuffer requestURL request.getRequestURL();获取URL全路径String protocol request.getProtocol();获取HTTP协议及版本号String remoteAddr request.getRemoteAddr();获取客户机的IP地址String remoteHost request.getRemoteHost();int remotePort request.getRemotePort();String remoteUser request.getRemoteUser();String pathInfo request.getPathInfo();String localAddr request.getLocalAddr();获取WEB服务器的IP地址String localName request.getLocalName();获取WEB服务器的主机名 四、request 获取请求头的数据
1、获取请求头的方法 String getHeader(String name):通过请求头的名称获取请求头的值EnumerationString getHeaderNames():获取所有的请求头名称 2、实例新建文件 ServletRequestDemo03.java 执行 service 方法
package cn.lemon.request;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 java.io.IOException;
import java.util.Enumeration;WebServlet(/ServletRequestDemo03)
public class ServletRequestDemo03 extends HttpServlet {Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取所有请求头数据EnumerationString headerNames req.getHeaderNames();//遍历while (headerNames.hasMoreElements()){String name headerNames.nextElement();String value req.getHeader(name);//根据 neme 获取请求头的 valueSystem.out.println(请求头名为 name ---------对应请求头的值为 value);}}
}
查看控制台 3、实例 查看客户端是谷歌浏览器还是火狐浏览器
package cn.lemon.request;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 java.io.IOException;WebServlet(/ServletRequestDemo04)
public class ServletRequestDemo04 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取请求头数据 user_agentString agent request.getHeader(user-agent);//判断浏览器的 agent 版本if (agent.contains(Chrome)){System.out.println(客户端使用的是谷歌浏览器);}else if (agent.contains(Firefox)){System.out.println(客户端使用的是火狐浏览器);}}
}
4、实例防盗链
package cn.lemon.request;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 java.io.IOException;WebServlet(/ServletRequestDemo05)
public class ServletRequestDemo05 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取请求头的数据 refererString referer request.getHeader(referer);System.out.println(referer);//防盗链if (referer ! null){if (referer.contains(ServletRequestDemo04)){response.setContentType(text/html;charsetutf-8);response.getWriter().write(播放电影);}else {//盗链response.setContentType(text/html;charsetutf-8);response.getWriter().write(想看电影来快播.....);}}}
}
五、request 获取请求体的数据
1、提醒 请求体只有POST请求方式才有请求体在请求体中封装了POST请求的请求参数获取流对象再从流对象中拿数据。 2、方法 BufferedReader getReader()获取字符输入流只能操作字符数据ServletInputStream getInputStream()获取字节输入流可以操作所有类型数据 3、实例 先写一个 input.html
!DOCTYPE html
html langen
headmeta charsetUTF-8title文本输入框/title
/head
body
form actionServletRequestDemo06 methodpost!--只能使用POST方式才能获取请求体--input typetext nameusername value请输入姓名brinput typetext namepassword value请输入密码brinput typesubmit value提交
/form
/body
/html
再写一个 Java 类 ServleRequestDemo06.java 获取提交数据
package cn.lemon.request;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 java.io.BufferedReader;
import java.io.IOException;WebServlet(/ServletRequestDemo06)
public class ServletRequestDemo06 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取请求体消息请求参数BufferedReader bufferedReader request.getReader();//获取字符流String line null;while ((line bufferedReader.readLine()) ! null){//读取一行数据System.out.println(line);}}
}
六、获取请求的通用方式
不论get请求方式还是post请求方式都可以使用下列方法来获取请求参数 String getParameter(String name):根据参数名称获取参数值String[] getParameterValues(String name)根据参数名称获取参数值的数组如hobbyxxhobbygameEnumerationString getParameterNames():获取所有请求的参数名称MapString,String[] getParameterMap():获取所有参数的map集合 案例 先写一个 regist.html
!DOCTYPE html
html langen
headmeta charsetUTF-8title注册页面/title
/head
body
form action/project/ServletRequestDemo07 methodpostinput typetext placeholder请输入用户名 nameusernamebrinput typetext placeholder请输入密码 namepasswordbrinput typecheckbox namehobby valuegame游戏input typecheckbox namehobby valuestudy学习brinput typesubmit value注册
/form
/body
/html
再写一个测试类 ServletRequstDemo07.java
package cn.lemon.request;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 java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;WebServlet(/ServletRequestDemo07)
public class ServletRequestDemo07 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username request.getParameter(username);//根据参数名称获取参数值System.out.println(username);String[] hobby request.getParameterValues(hobby);//根据参数名称获取参数数组System.out.println(Arrays.toString(hobby));//只能得到一个参数String hobby2 request.getParameter(hobby);System.out.println(hobby2);System.out.println(--------------);//得到所有的参数名EnumerationString names request.getParameterNames();while (names.hasMoreElements()) {String name names.nextElement();String value request.getParameter(name);System.out.println(name :\t value);}System.out.println(-----------);//得到所有的参数的Map形式MapString, String[] parameterMap request.getParameterMap();for (String key : parameterMap.keySet()) {System.out.println(key Arrays.toString(parameterMap.get(key)));}}
}
七、中文乱码的解决 get方式tomcat 8 已经将get方式乱码问题解决了post方式在获取参数前设置request的编码request.setCharacterEncoding(utf-8); 案例 新建 input02.html
!DOCTYPE html
html langen
headmeta charsetUTF-8title登录/title
/head
body
form action/project/ServletRequestDemo08 methodpostinput typetext nameusername value请输入用户名brinput typetext namepassword value请输入密码brinput typesubmit value提交
/form
/body
/html
再建一个 ServletRequestDemo08.java
package cn.lemon.request;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 java.io.IOException;WebServlet(/ServletRequestDemo08)
public class ServletRequestDemo08 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置编码格式request.setCharacterEncoding(utf-8);//获取参数usernameString username request.getParameter(username);System.out.println(username);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response);}
}
八、请求转发
一种在服务器内部的资源跳转方式 通过request对象获取请求转发器对象RequestDispatcher getRequestDispatcher(String path) 使用RequestDispatcher对象来进行转发forward(ServletRequest request, ServletResponse response) 特点 浏览器地址栏路径不发生变化只能转发到当前服务器内部资源中转发是一次请求。 案例 新建一个ServletRequestDemo09.java
package cn.lemon.request;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 java.io.IOException;WebServlet(/ServletRequestDemo09)
public class ServletRequestDemo09 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//请求转发转发路径不需要加虚拟目录重定向需要加虚拟目录request.getRequestDispatcher(/ServletRequestDemo10).forward(request,response);}
}
新建一个ServletRequestDemo10.java
package cn.lemon.request;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 java.io.IOException;WebServlet(/ServletRequestDemo10)
public class ServletRequestDemo10 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.getWriter().print(ServletRequestDemo10 被访问了通过 ServletRequestDemo9 请求转发过来的);}
}
当访问ServletRequestDemo09.java 时它直接跳到ServletRequestDemo10.java
九、共享数据
域对象一个有作用范围的对象可以在范围内共享数据 request域代表一次请求的范围一般用于请求转发的多个资源中共享数据
方法 void setAttribute(String name,Object obj):存储数据Object getAttitude(String name):通过键获取值void removeAttribute(String name):通过键移除键值对 案例 新建一个ServletRequestDemo09.java
package cn.lemon.request;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 java.io.IOException;WebServlet(/ServletRequestDemo09)
public class ServletRequestDemo09 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//在request作用域存储数据request.setAttribute(name1,存入数据的Object01);request.setAttribute(name2,存入数据的Object02);request.setAttribute(name3,存入数据的Object03);request.setAttribute(name4,存入数据的Object04);//请求转发转发路径不需要加虚拟目录重定向需要加虚拟目录request.getRequestDispatcher(/ServletRequestDemo10).forward(request,response);}
}
新建一个ServletRequestDemo10.java
package cn.lemon.request;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 java.io.IOException;WebServlet(/ServletRequestDemo10)
public class ServletRequestDemo10 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Object obj request.getAttribute(name1);//取出数据response.getWriter().print(obj);//打印数据response.getWriter().print(ServletRequestDemo10 被访问了通过 ServletRequestDemo9 请求转发过来的);}
} 文章转载自: http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.tcxzn.cn.gov.cn.tcxzn.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.sxtdh.com.gov.cn.sxtdh.com http://www.morning.ysbhj.cn.gov.cn.ysbhj.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.jnhhc.cn.gov.cn.jnhhc.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.lqklf.cn.gov.cn.lqklf.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.dztp.cn.gov.cn.dztp.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.gfjgq.cn.gov.cn.gfjgq.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.dzqyn.cn.gov.cn.dzqyn.cn http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.mqwnp.cn.gov.cn.mqwnp.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn