• request对象学习


    import java.io.IOException;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * request对象学习:
     * 		作用:request对象中封存了当前所有请求的信息
     * 		使用:
     * 			获取请求头数据
     * 				req.getMethod();//获取请求方式
     * 				req.getRequestURI();//获取请求URI
     * 				req.getRequestURL();//获取请求URL
     *  			req.getScheme();		//获取协议
     * 
     * 			获取请求行数据
     * 				req.getHeader("键名");//返回指定的请求头信息
     * 				req.getHeaderNames("User-Agent");//返回请求头键名的枚举集合
     * 
     * 
     * 				Enumeration e = req.getHeaderNames();
    			 while(e.hasMoreElements()){
    				 String name = (String) e.nextElement();
    				 String value2 = req.getHeader(name);
    				 System.out.println(e.nextElement()+":"+name);
     * 			获取用户数据 
     * 				req.getParameter("键名");//返回指定的用户数据
     * 				req.getParameterValues("键名");//返回同键不同值的请求数据(多选),返回的数组
     * 				req.getParameterNames();//返回所有用户请求数据的枚举集合
     * 				
     * 			注意:如果要获取的数据请求不存在不会报错,返回null
     * 
     * 		注意:
     * 			request对象时由Tomcat服务器创建,并作为实参传递给处理请求的servlet的service方法。
     * 
     * 
     * @author Administrator
     *
     */
    public class RequestServlet extends HttpServlet {
    	@Override
    	protected void service(HttpServletRequest req, HttpServletResponse resp)
    			throws ServletException, IOException {
    			//获取请求头数据 
    				//获取请求方式
    			String method = req.getMethod();
    			System.out.println(method);
    				//获取请求URL
    			 StringBuffer url= req.getRequestURL();
    			 System.out.println(url);
    			 //获取URI
    			 String uri =req.getRequestURI();
    			 System.out.println(uri);
    				//获取请求协议
    			 String schem =	 req.getScheme();
    			 System.out.println(schem);
    	//获取请求行数据
    			 
    			 String value = req.getHeader("User-Agent");
    			 System.out.println(value);
    		//获取所有请求行数据的枚举
    			 Enumeration e = req.getHeaderNames();
    			 while(e.hasMoreElements()){
    				 String name = (String) e.nextElement();
    				 String value2 = req.getHeader(name);
    				 System.out.println(e.nextElement()+":"+name);
    		//获取用户数据
    				 String name1= req.getParameter("uname");
    				 String pwd  = req.getParameter("pwd");
    				 System.out.println(name1+":"+pwd);
    				// String fav = req.getParameter("fav");会漏值要用数组提取
    				 String[] favs=req.getParameterValues("fav");
    				 if(favs!=null){
    					 for(String fav:favs){
    						 System.out.println(fav);
    						 
    					 }	 
    				 }
    				 //获取用户请求数据所有键的枚举的集合------req.getParameterNames();
    				 
    			 }
    		
    		 
    		
    	}
    
    }










      requestservlet.java

     <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'MethodJsp.jsp' starting page</title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    
      </head>
      
      <body>
       
       <form action="req" method="post">
       	用户名:<input type="text" name="uname" value=""><br />
      	密码:<input type="password" name="pwd" value=""><br />
      	爱好:<br />
      	<input type="checkbox" name="fav" value="1">唱歌<br />
      	<input type="checkbox" name="fav" value="2">跳舞<br />
      	<input type="checkbox" name="fav" value="3">泡妞<br />
      	<input type="submit" value="登录">
       
       </form>
      </body>
    </html>
    

      jsp页面

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>servletlife</servlet-name>
        <servlet-class>com.sxt.in.servletlife</servlet-class>
        <load-on-startup>1</load-on-startup><!-- 加载服务器启动流 其中的数字1表示项目的加载顺序-->
      </servlet>
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>ServletMethod</servlet-name>
        <servlet-class>com.sxt.in.ServletMethod</servlet-class>
      </servlet>
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>RequestServlet</servlet-name>
        <servlet-class>com.sxt.in.RequestServlet</servlet-class>
      </servlet>
    
    
    
      <servlet-mapping>
        <servlet-name>servletlife</servlet-name>
        <url-pattern>/life</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>ServletMethod</servlet-name>
        <url-pattern>/method</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>RequestServlet</servlet-name>
        <url-pattern>/req</url-pattern>
      </servlet-mapping>
    
    
    </web-app>
    

      xml页面

  • 相关阅读:
    win7 删除Windows服务的方法
    如何对SQL Server 2005进行设置以允许远程连接(转载)
    MySql实现远程连接
    OpenCV训练分类器制作xml文档
    Ms SQL Server 约束和规则
    PowerDesigner实用技巧小结(4)
    企业级技术解决方案:hbase+es
    ES的聚合操作
    es之过滤器
    es之得分(加权)
  • 原文地址:https://www.cnblogs.com/dream2060/p/10942251.html
Copyright © 2020-2023  润新知