Servlet--获取http协议的请求体参数

来源:互联网 发布:三维激光扫描数据 编辑:程序博客网 时间:2024/06/05 16:06

Servlet--获取http协议的请求体参数


一、获取用户提交的数据

获取用户提交参数的方式有两种:1、get方式   2、post方式

二、两种获取数据的方式

1、创建一个servlet类来获取用户提交的参数
package test06_3;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class UserServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置响应的类型及编码格式response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();//接受用户的参数,通过get方式。String name = request.getParameter("name");//接受用户的参数String sex = request.getParameter("sex");out.print("GET方式获取你的名称是:"+name+",性别是:"+sex);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8"); //设置接受数据是utf-8编码response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();//接受用户的参数,通过post方式。String name = request.getParameter("name");//接受用户的参数String sex = request.getParameter("sex");out.print("POST方式获取你的名称是:"+name+",性别是:"+sex);}}

2、web.xml配置文件

<?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>UserServlet</servlet-name>    <servlet-class>test06_3.UserServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>UserServlet</servlet-name>    <url-pattern>/userservlet</url-pattern>  </servlet-mapping></web-app>


3、配置index.jsp文件转发用户的get和post请求

<%@ 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 'index.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>    This is my JSP page. <br>    <a href="userservlet?name=wanghong&sex=nan">获取get请求参数</a><br/>    <!-- form表单提交方式获取post请求参数 -->    <form action="userservlet" method="post">    名字:<input type="text" name="name" value="王红"/><br/>    性别:<input type="text" name="sex" value="男"/><br/>    <input type="submit" value="提交"/>    </form>  </body></html>

三、测试获取用户提交参数

1、浏览器发送请求  http://127.0.0.1:8080/test06_3/     在首页点击【获取Get请求参数】和 【提交】

2、分别或的get参数和post参数如下

get请求参数


post请求参数



原创粉丝点击