1.首先看ServletRequest的API

javax.servlet

Interface ServletRequest

All Known Subinterfaces:
HttpServletRequest
All Known Implementing Classes:
HttpServletRequestWrapper,ServletRequestWrapper
Method Summary
 Object getAttribute(String name)

          Returns the value of the named attribute as an Object, or
null
if no attribute of the given name exists.
 Enumeration getAttributeNames()

          Returns an Enumeration containing the names of the attributes available to this request.
 String getCharacterEncoding()

          Returns the name of the character encoding used in the body of this request.
 int getContentLength()

          Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known.
 String getContentType()

          Returns the MIME type of the body of the request, or null if the type is not known.
 ServletInputStream getInputStream()

          Retrieves the body of the request as binary data using a ServletInputStream.
 String getLocalAddr()

          Returns the Internet Protocol (IP) address of the interface on which the request was received.
 Locale getLocale()

          Returns the preferred Locale that the client will accept content in, based on the Accept-Language header.
 Enumeration getLocales()

          Returns an Enumeration of Locale objects indicating, in decreasing order starting with the preferred locale, the locales that are acceptable to the client based on the Accept-Language header.
 String getLocalName()

          Returns the host name of the Internet Protocol (IP) interface on which the request was received.
 int getLocalPort()

          Returns the Internet Protocol (IP) port number of the interface on which the request was received.
 String getParameter(String name)

          Returns the value of a request parameter as a String, or
null
if the parameter does not exist.
 Map getParameterMap()

          Returns a java.util.Map of the parameters of this request.
 Enumeration getParameterNames()

          Returns an Enumeration of String objects containing the names of the parameters contained in this request.
 String[] getParameterValues(String name)

          Returns an array of String objects containing all of the values the given request parameter has, ornull if the parameter does not exist.
 String getProtocol()

          Returns the name and version of the protocol the request uses in the formprotocol/majorVersion.minorVersion, for example, HTTP/1.1.
 BufferedReader getReader()

          Retrieves the body of the request as character data using a BufferedReader.
 String getRealPath(String path)

          Deprecated. As of Version 2.1 of the Java Servlet API, useServletContext.getRealPath(java.lang.String)
instead.
 String getRemoteAddr()

          Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.
 String getRemoteHost()

          Returns the fully qualified name of the client or the last proxy that sent the request.
 int getRemotePort()

          Returns the Internet Protocol (IP) source port of the client or last proxy that sent the request.
 RequestDispatcher getRequestDispatcher(String path)

          Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
 String getScheme()

          Returns the name of the scheme used to make this request, for example,
http
, https, or ftp.
 String getServerName()

          Returns the host name of the server to which the request was sent.
 int getServerPort()

          Returns the port number to which the request was sent.
 boolean isSecure()

          Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
 void removeAttribute(String name)

          Removes an attribute from this request.
 void setAttribute(String name,Object o)

          Stores an attribute in this request.
 void setCharacterEncoding(String env)

          Overrides the name of the character encoding used in the body of this request.

ServletRequest是一个接口,有一个子接口类和两个实现类,表格中显示的方法,由于本人精力和知识有限,只测试其中的几个方法:

(1).getParameter(String name):根据参数名,获取参数值;

(2).getParameterNames():获取所有参数名组成的 Enumeration

(3).getParameterValues(String name):根据参数名,获取参数值组成的String[],用于多参数值;

(4).getParameterMap():获取参数名和参数值的String[]组成的键值对,即返回的类型为Map(String,String[]),用于多参数值。





2.项目目录结构







3.LoginServlet



package com.dao.chu;

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class LoginServlet implements Servlet{ @Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
} @Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
} @Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("init..."); } @Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse)
throws ServletException, IOException { System.out.println("打印ServletRequest的值: "+servletRequest);
System.out.println("打印servletResponse的值: "+servletRequest); //获取表单中的内容
String userValue = servletRequest.getParameter("user");
String passwordValue = servletRequest.getParameter("password"); System.out.println("【getParameter】userValue is :"+userValue);
System.out.println("【getParameter】passwordValue is :"+passwordValue); //获取提交的所有参数名组成的Enumeration
Enumeration<String> enNmes = servletRequest.getParameterNames(); //循环
while (enNmes.hasMoreElements()) { //获取参数名打印
String enName = (String) enNmes.nextElement();
System.out.println("【getParameterNames】enName is :"+enName); //获取参数值打印
String enValue = servletRequest.getParameter(enName);
System.out.println("【getParameterNames】enValue is: "+enValue); } //获取参数名和参数值的String[]组成的键值对
Map<String, String[]> map = servletRequest.getParameterMap(); Set<Entry<String,String[]>> entrySet = map.entrySet(); for (Entry<String, String[]> entry : entrySet) { System.out.println("【getParameterMap】getNamebyMap is: "+entry.getKey()); System.out.println("【getParameterMap】getValuebyMap is:"+Arrays.asList(entry.getValue()));
} //获取多个请求方式的方法
String[] interests = servletRequest.getParameterValues("interesting"); for (String interest : interests) { System.out.println("【getParameterValues】interest is :"+interest);
} } }

4.web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWeb_06</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.dao.chu.LoginServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping> </web-app>

5.login.jsp



<%@ 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>
<title>登陆页</title>
</head>
<body> <!-- 相对路径写action="LoginServlet"; -->
<!-- 绝对路径写action="/javaWeb_06/LoginServlet"; --> <form action="LoginServlet" method="post">
用户名:<input type="text" name="user">
<br><br>
密码  <input type="password" name="password">
<br><br> <!-- 一组信息 -->
interesting:
<input type="checkbox" name="interesting" value="reading">Reading
<input type="checkbox" name="interesting" value="writing">Writing
<input type="checkbox" name="interesting" value="football">Football
<input type="checkbox" name="interesting" value="game">Game
<input type="checkbox" name="interesting" value="shopping">Shopping
<input type="checkbox" name="interesting" value="party">Party
<input type="checkbox" name="interesting" value="TV">TV <br><br> <input type="submit" value="提交">
</form> </body>
</html>

6.输入用户名:admim 密码:123,选择前三个复选框



7.查看打印信息







8.接下来根据打印信息和下面这张图进行总结





9.总结:

(1).在打印信息中,我们把ServletRequest的值和servletResponse的值都打印了出来,从关键词"apache.catalina"中可以看出:这两个接口的实现类都是由tomcat服务器给予实现的,并在服务器调用service时传入。

(2).请求信息经过tomcat服务器映射到我们的LoginServlet,在LoginServlet调用service方法时候,参数封装在了ServletRequest中,而ServletRequest有一些接收参数的方法。这样loginServlet可以写一些和数据库服务器连接的方法,就可以和数据库里面的值进行比对。

(3).当参数值为一个的时候,大多数我们根据需要应用getParameter方法或getParameterNames,而多参数值的时候,根据需要应用getParameterValuesgetParameterMap方法。



附:



API下载地址:点击打开链接



本次项目代码:点击打开链接



参考视频:点击打开链接

javaWEB总结(6):ServletRequest的更多相关文章

  1. javaWEB中的ServletRequest,ServletResponse的使用,及简化Servlet方法

    首先说一下ServletRequest,ServletResponse类的使用方法: public void service(ServletRequest request, ServletRespon ...

  2. JavaWeb学习之Path总结、ServletContext、ServletResponse、ServletRequest(3)

    1.Path总结 1.java项目 1 File file = new File(""); file.getAbsolutePath(); * 使用java命令,输出路径是,当前j ...

  3. JavaWeb——Servlet

    一.基本概念 Servlet是运行在Web服务器上的小程序,通过http协议和客户端进行交互. 这里的客户端一般为浏览器,发送http请求(request)给服务器(如Tomcat).服务器接收到请求 ...

  4. JavaWeb——Listener

    一.基本概念 JavaWeb里面的listener是通过观察者设计模式进行实现的.对于观察者模式,这里不做过多介绍,大概讲一下什么意思. 观察者模式又叫发布订阅模式或者监听器模式.在该模式中有两个角色 ...

  5. javaweb学习笔记之servlet01

    一.Servlet概述 A servlet is a small Java program that runs within a Web server. Servlets receive and re ...

  6. [Java面试三]JavaWeb基础知识总结.

    1.web服务器与HTTP协议 Web服务器 l WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. l Internet上供外界访问的Web资源分为: • 静 ...

  7. [javaweb]Java过滤器与包装设计模式的实用案例.

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  8. 传智播客JavaWeb听课总结

    一. JavaWeb基础 第一天: 1.Eclipse详解: (1).Bad versionnumber in .class file:编译器版本和运行(JRE)版本不符合.高的JRE版本兼容低版本的 ...

  9. JavaWeb学习总结-04 Servlet 学习和使用

    一 Servlet 1 Servlet概念 Servlet时运行在服务器端的Java程序. Servlet的框架核心是 javax.servlet.Servlet 接口. 所有自定义的Servlet都 ...

随机推荐

  1. ****The Toy of Flandre Scarlet

    The Toy of Flandre Scarlet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

  2. 4、Xcode8中的钥匙串保存数据取出时候为空的问题

    Xcode7以及之前的版本直接使用Keychain存储数据即可,但是从Xcode8开始,再用之前的方法会发现,读取不到存进去的数据了,或者说,存储不进去了,原因是苹果加强了隐私保护,这个东西需要打开开 ...

  3. MyBatis-防止Sql注入以及sql中#{}与${}取参数的区别

    #{}能够更安全的取出参数 ${}取出的参数不安全 尽量不要使用${}取参数 原因: A:select * from table where a = '10001' and b = ${paramet ...

  4. python修炼6

    文件操作 注:不能把open语句放在try块里,因为当打开文件出现异常时,文件对象file_object无法执行close()方法. 1.打开文件 (相当于桌面的快捷方式)f=open(文件名,模式默 ...

  5. Linux服务器建站基础-选择何种配置和安装环境项目

    我们准备在搬瓦工笔记中,边整理和分享利用Linux VPS建站过程的同时,也会分享关于用户在选择和使用VPS服务器以及网站管理运营中的一些心得和建议.经常有很多网友在很多主机论坛.QQ群众问道,有没有 ...

  6. 使用JavaScript把页面上的表格导出为Excel文件

    如果在页面上展示了一个数据表格,而用户想把这个表格导出为Excel文件,那么在要求不高的情况下,可以不通过服务器生成表格,而是直接利用JavaScript的Blob和Object URL特性将表格导出 ...

  7. VS2013使用EF与mysql数据库.

    一个VS2013的mvc+EF+mysql的项目,需要连接Mysql数据库 一,下载一个mysql-for-visualstudio-1.2.3.msi,在自己的电脑上安装,这个是解决在创建实体模型( ...

  8. <?php function say() { echo 'hello world'; } //在这里调用函数 say(); php 调用方法say()

    <?php function say() {     echo 'hello world'; } //在这里调用函数 say(); php 调用方法say()

  9. qml 中 使用 shader

    使用绘制工具如Photoshop .Flash已经可以创建许多效果非常绚丽的图像.动画等. Qt/QML 的努力其实是在这些工具发展的后面, 因此很多效果在Qt中无法实现. 不得不佩服Qt小组的才智, ...

  10. 应用 Valgrind 发现 Linux 程序的内存问题(转)

    Valgrind 概述 体系结构 Valgrind 是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个 ...