[javaweb]Javaweb中request获取前端数据
HttpServletrRequest
HttpServletRequest代表 客户端的请求,用户通过http协议访问服务器,http请求中的所有信息会被封装到HttpServletRequest中,通过它的方法就可以拿到所有的信息。
request获取前端传的数据
package com.javaweb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author panglili
* @create 2022-07-19-19:10
*/
public class Request extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
String[] hobbies = req.getParameterValues("hobby");
System.out.println("===========");
System.out.println(username);
System.out.println(pwd);
for(String s:hobbies){
System.out.println(s);
}
System.out.println("===========");
resp.sendRedirect("/success.jsp");
}
}
前端
<%--
Created by IntelliJ IDEA.
User: 塔塔
Date: 2022/7/19
Time: 16:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
$END$
<!--表单框架 -->
<form action="${pageContext.request.contextPath}/req" method="post">
<p>名字:<input type="text" name="username" value="默认初始值" maxlength="8"></p>
<p>密码:<input type="password" name="pwd"> </p>
<p>
性别:
<input type="radio" value="nan" name="sex">男
<input type="radio" value="nv" name="sex">女
</p>
<p>
爱好:
<input type="checkbox" value="sleep" name="hobby">睡觉
<input type="checkbox" value="sport" name="hobby">运动
<input type="checkbox" value="study" name="hobby">学习
</p>
<p>
下拉框
<select name="列表名称">
<option value="选项的值">印度</option>
<option value="选项的值">中国</option>
<option value="选项的值">朝鲜</option>
<option value="选项的值">美国</option>
<option value="选项的值">瑞士</option>
</select>
</p>
<p>
下拉框
<textarea name="textarea" cols="30" rows="50">文本neir</textarea>
</p>
<p>
文件域
<input type="file" name="files">
<input type="submit" value="上传" name="upload" >
</p>
<p>
<input type="submit">
<input type="reset">
</p>
</form>
</body>
</html>