java接收前端参数

目录

  • 前端请求类型
  • 前端传递数据的提交方式
  • 后端接收数据
  • 使用注解

前端请求类型:

1、在前端开发中,有把八种请求,分别为:GET,POST,PUT,DELETE,TRACE,HEAD,OPTIONS,CONNECT,但是最为常用的只有GET请求和POST请求。

  • GET:会将传递的参数以地址栏的形式进行传递,如:http://baidu.com?name=xxx&param=xxx
  • POST:而post请求相反,不会将数据以地址栏的方式传递,这样传递的数据量更大。

前端传递数据的提交方式

1、在前端访问后端接口时,都会设置一个属性Content-Type的值,这个表示设置数据的提交方式,

  • Content-Type:application/x-www-form-urlencoded:默认的提交方式,传递的参数以key-value的形式进行传递。
  • Content-Type:multipart/form-data:用于文件上传
  • Content-Type:application/json:以JSON的格式进行传递

后端接收数据的方式

1、原始的web开发就是jsp+servlet的方式进行,后端要想取得前端的参数,可以通过request对象提供的方法进行。

  • request.getParameter(String paramName);
  • request.getParameterValues()
  • request.getParameterMap()

以form表单提交为例,form表单提交默认使用的是application/x-www-form-urlencoded的提交方式。
范例:使用request.getParameter()接收参数,这里是前端将要提交的数据写到form表单中,输入框标签的input中必须要有name的属性,以key-value的形式将参数传递到后台,后台通过key获取该参数的值。get请求和post请求都可以。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>【获取参数】</title>
<script type="text/javascript" src="<%=path%>/jquery/jquery-2.0.3.min.js"></script>
</head>
<body>
	<form action="http://localhost:8080/servlet-jsp/get_param_demo" method="get">
		<input type="text" name="name"/>
		<input type="text" name="phone"/>
		<button type="submit">提交</button>
	</form>
</body>
</html>

在这里插入图片描述
在这里插入图片描述
后台接收参数

package cn.txp.learn.param;
import java.io.IOException; 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class GetParamDemo extends HttpServlet{
   
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		this.getParam(req, resp);
	}
	public String getParam(HttpServletRequest req, HttpServletResponse resp) {
   
		String name = req.getParameter("name");
		String phone = req.getParameter("phone");
		System.out.println(name);
		System.out.println(phone);
		return null;
	}
}

正常接收参数
在这里插入图片描述

范例:request.getParameterValues(),如果说此时form表单中,有多个input框的name属性相同,可以是同此方法获取多个值。如下,有四个name相同。

<form action="http://localhost:8080/servlet-jsp/get_param_demo" method="post">
		<input type="text" name="name"/>
		<input type="text" name="name"/>
		<input type="text" name="name"/>
		<input type="text" name="name"/>
		<input type="text" name="phone"/>
		<button type="submit">提交</button>
</form>

后台通过此方法获取值,是一个String的数组。

package cn.txp.learn.param;
import java.io.IOException; 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class GetParamDemo extends HttpServlet{
   
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		
  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值