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请求参数
阅读全文
0 0
- Servlet--获取http协议的请求体参数
- Servlet--获取Http协议请求头信息
- servlet获取请求参数
- servlet获取http请求的内容
- 获取Http协议的请求行,请求头
- 可以传中文参数的自定义http协议请求方式
- Servlet程序获取http协议提交的信息
- java 发送http协议的POST请求获取数据
- NodeJS的Express框架获取Http请求参数
- HTTP协议请求和响应各参数
- javaweb-通过servlet获取请求参数
- Servlet中URL请求参数获取
- Servlet中URL请求参数获取
- 获取http协议请求行、请求头、请求转发
- Servlet-基于的HTTP协议
- HTTP的请求协议概述
- http协议的post请求
- Http协议请求的解析
- java笔记(四)之循环练习
- 实现组合算法
- 并发实战——“JUC集合”之ArrayBlockingQueue
- CTO都需要会哪些技能?
- 找出01字符串中0和1连续出现的最大次数
- Servlet--获取http协议的请求体参数
- css选择器总结和三大特性
- python绘制caffe中网络模型
- HDU 4006 The kth great number
- 去除元素间距的解决方法
- 全图最小割(Stoer-Wagner算法)
- ThinkCMF应用开发流程
- Filter过滤器:在进入主页时,先通过过滤器从而为index提供数据
- poj 3258 River Hopscotch (二分与贪心)