javaweb入门之表单提交与HttpSeverletRequest

1.下面是一个表单,把它以post方式提交到addchuli的severlet:

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>添加联系人</title>
</head>
<body>
	<center><header>添加联系人</header></center>
	<form action="/webproject_contact/addchuli" method="post">
	<table align="center" border="1" width="700px">
	<tr>
		<th>姓名</th>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<th>性别</th>
		<td>
			<input type="radio" value="man">男
			<input type="radio" value="feman">女
		</td>
	</tr>
	<tr>
		<th>年龄</th>
		<td><input type="text" name="age"></td>
	</tr>
	<tr>
		<th>电话</th>
		<td><input type="text" name="phone"></td>
	</tr>
	<tr>
		<th>邮箱</th>
		<td><input type="text" name="email"></td>
	</tr>
	<tr>
		<th>qq</th>
		<td><input type="text" name="qq"></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" value="保存">&nbsp;<input type="reset" value="重置"></td>
	</tr>
</table>
</form>
</body>
</html>

2.当我部署好后,浏览器客户端输入http://localhost:8080/webproject_contact/add.html,就会访问到部署在服务器上的资源在这里插入图片描述
在这里插入图片描述
3.下面是severlet代码

// 把表单中的name项打印到控制台
public class addchuli extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		System.out.println(name);	
	}
}

4.在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
tomcat服务器将我此次的请求的请求行,请求头,实体内容都封装成HttpSeverletRequest对象,服务器调用资源代码的doPost方法,并将封装好的HttpSeverletReqest对象传入,我们可以在方法内部通过传入的对象获取表单信息。
结果:在这里插入图片描述

发布了56 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/hpccph15/article/details/86774100