//获取Map凑集中所有的keyEnumeration<String> getParameterNames();//获取MapMap<String, String[]> getParameterMap(); //根据key获取Map凑集中的vale (常用)String[] getParameterValues(String s);//获取value一维数组的第一个元素 (常用)String getParameter(String name); 浏览器向做事器提交的是String类型
//getParameterNames()获取所有key值Enumeration<String> keys = request.getParameterNames();while (keys.hasMoreElements()){ String key = keys.nextElement(); System.out.print("key: "+key +" "); //getParameterValues(key) 、据key获取Map凑集中的vale String[] Values = request.getParameterValues(key); if (Values.length>1){ for (String value : Values) { System.out.print("value:"+value+" "); } }else { System.out.print(Values[0]); } System.out.println();}
如果html页面的数据有变动,浏览器打消过缓存在实行。
//通过标签中的name获取value一维数组String[] usernames = request.getParameterValues("username");String[] pwds = request.getParameterValues("pwd");String[] hobbies = request.getParameterValues("hobby");for (String username : usernames) { System.out.print(username);}System.out.println();for (String pwd : pwds) { System.out.print(pwd);}System.out.println();for (String hobby : hobbies) { if (hobby.isEmpty()){ System.out.println("null"); } System.out.print(hobby);}System.out.println();//获取数组的第一个参数String username = request.getParameter("username");String pwd = request.getParameter("pwd");String hobby = request.getParameter("hobby");System.out.println("getParameter :"+username+" "+pwd+" "+hobby);
getParameter获取数组的第一个参数
//获取数组的第一个参数String username = request.getParameter("username");String pwd = request.getParameter("pwd");String hobby = request.getParameter("hobby");
要求域工具
Request又称“要求域”运用域工具ServletContext(Servlet高下文工具)、当用户的共享数据很少修正操作并且数据量少的时候,利用ServletContext能够提升程序的实行效率(运用域绑天命据,看作将数据放到Cache当中,用户访问时直接从Cache中提取,减少IO等操作)。运用域工具ServletContext的操作方法(类似Map凑集的操作)//向域绑天命据setAttribute(String name , Object obj)//从域获取数据,根据name(key)获取数据Object getAttribute(String name)//移除数据,根据name(key)removeAttribute(String name)要求域工具要求域比运用域的范围小, 占用资源小,生命周期短,要求域工具只在一次要求内有效。要求域工具ServletContext的操作方法(类似Map凑集的操作)//向域绑天命据setAttribute(String name , Object obj)//从域获取数据,根据name(key)获取数据Object getAttribute(String name)//移除数据,根据name(key)removeAttribute(String name)
案例
//获取系统当前韶光Date nowTime =new Date();//向request域 中绑天命据request.setAttribute("NowTime",nowTime);//从request域 获取数据Object obj = request.getAttribute("NowTime");response.setContentType("text/html;charset=utf-8");response.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String timeStr = sdf.format((Date)obj);out.print("当前韶光: "+ timeStr);
Servlet转发机制转发servlet类
public class ServletA extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //利用Servlet转发机制。实行ServletA后,跳转至ServletB,调用要求转发器,将request,response参数通报给另一个HttpServlet子类 request.getRequestDispatcher("/servletB").forward(request,response); }}
public class ServletB extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取系统当前韶光 Date nowTime =new Date(); //向request域 中绑天命据 request.setAttribute("NowTime",nowTime); //从request域 获取数据 Object obj = request.getAttribute("NowTime"); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timeStr = sdf.format((Date)obj); out.print("当前韶光: "+ timeStr); }}
//既可以转发Servlet类也可以转发html(属于Web容器当中合法的资源都可以转发)request.getRequestDispatcher("/share.html").forward(request,response);
//获取客户真个IP地址String remoteAddr = request.getRemoteAddr();//获取远程的用户String remoteUser = request.getRemoteUser();//获取远程的主机IPString remoteHost = request.getRemoteHost();//获取远程的的端口int remotePort = request.getRemotePort();//获取主机做事名String serverName = request.getServerName();//获取做事路径(项目名称)String servletPath = request.getServletPath();//获取做事端口int serverPort = request.getServerPort();//获取Servlet高下文 或者this.getServletContext();ServletContext servletContext = request.getServletContext();//指定字符集(办理不同字符集乱码问题)response.setCharacterEncoding("utf-8");
点击下方,第一韶光理解华为云新鲜技能~
华为云博客_大数据博客_AI博客_云打算博客_开拓者中央-华为云
#华为云开拓者同盟#