JSP值传递

JSP课堂笔记

划重点  记笔记

JSP服务器端的跳转  实现用户信息的接收

先上用户选项页面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="d.jsp" method="post"><!--  创建表单元素  给定跳转的jsp页面-->
请输入账号:<input type="text" name="account" /><br>
请输入密码:<input type="password" name="pwd" /><br>
请选择你的性别:<input type="radio" value="男" name="sex" ></input>
<input type="radio" name="sex" value="女" ></input>
请选择你的城市:<select name="class"><!-- 创建下拉框元素 -->
<option value="1班" >1班</option>
<option value="2班" >2班</option>
<option value="3班" >3班</option>
</select><br>
请选择你喜欢吃的食物: 
<input type="checkbox" value="水煮肉片" name="food">水煮肉片</input>
<input type="checkbox" value="宫爆鸡丁" name="food">宫爆鸡丁</input>
<input type="checkbox" value="蚂蚁上树" name="food">蚂蚁上树</input>
<input type="checkbox" value="土豆丝" name="food">土豆丝</input><br>


<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</body>
</html>

下面是接受方的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
THIS IS d.jsp <br>

客户端传递过来的信息是:<br>
<%
request.setCharacterEncoding("utf-8");//设置传输来的编码为"utf-8"
String n=request.getParameter("account");//为接受的各个数据定义对象接收
String p=request.getParameter("pwd");
String sex=request.getParameter("sex");
String class1=request.getParameter("class");
String[] food=request.getParameterValues("food");
String s=" ";
for(String f:food){//food含有多个value   循环便利输出
    s=s+f+" ";
}

%>
<%=n %><br>//输出在界面上
<%=p %><br>
<%=sex %><br>
<%=class1 %>
<%=s %>

</body>
</html>

完工

posted @ 2018-11-27 13:59  LoopYasa  阅读(177)  评论(0编辑  收藏  举报