表单
<form action = "" method = "">
action
action:目标地址,把请求交给谁处理;action可以不设置,也可以是空字符串(提交给自己处理)
method
method: get | post,默认是get
get和post的区别
- get:参数比较小用get,要取服务器数据
- post:向服务器提交数据
- get会把参数显示在地址栏,post不会
- 中文参数尽量不要使用get方式
- 两种请求的请求体大小,get是有限制的
输入手段
文本框,密码框,文本区域,下拉列表,但选框,复选框,隐藏框…,提交按钮,重置按钮
要求:往服务器传值的地方必须指定name属性,作为参数名传到服务器
<input type="text" name="code" class="....">
basePath
在jsp代码的上方能看到一个String变量basePath,被赋值为
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
对于我的web项目来说,basePath其实就等于http://localhost:8088/jsp/,所以当你想要访问其他网页,写路径的时候,可以直接利用basePath
提交信息给input_action.jsp页面
提交信息,分三步:
- 设置form的action属性为被提交的网站名称
- 填写输入部分的name,后面获取信息也要通过name
- 被提交网页通过request.getPramamter(String arg0)方法获取信息
<!-- input.jsp -->
<%@ 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%>"><!-- 指定当前页面相对路径得根目录在哪,目前的basePath = "http://localhost:8088/jsp/" -->
<title>My JSP 'input.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">
<jsp:include page = "../res.jsp"/>
</head>
<body class = "container">
<div>
<h3 class = "page-header">request.getPramamter()的用法</h3>
<form action = "<%=basePath%>c02/input_action.jsp" method = "post" class = "form-horizontal">
<div class = "form-group">
<label class = "col-md-3 control-label">学号:</label>
<div class = "col-md-5">
<input type = "text" name = "id" class = "form-control"><!-- 指定name -->
</div>
</div>
<div class = "form-group">
<label class = "col-md-3 control-label">姓名:</label>
<div class = "col-md-5">
<input type = "text" name = "name" class = "form-control"><!-- 指定name -->
</div>
</div>
<div class = "form-group">
<div class = "col-md-offset-3 col-md-5">
<button type = "submit" class = "btn btn-primary">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>
这里我设置了action提交的时候传给input_action.jsp这个网页,传输方式为post,两个输入框分别命名为id和name
<!-- input_action.jsp -->
<%@ 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 'input_action.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">
</head>
<body>
<%
// 设置编码格式,避免中文乱码
request.setCharacterEncoding("UTF-8");
// 获取请求参数,参数名为id,对应学号
String id = request.getParameter("id");
// 获取请求参数,参数名为name,对应姓名
String name = request.getParameter("name");
%>
<ul>
<li>学号:<%= id %></li>
<li>姓名:<%= name %></li>
</ul>
</body>
</html>
为了避免输入框输入中文,导致传到input_action.jsp网页的信息是乱码,这里要利用**request.setCharacterEncoding(“UTF-8”)方法将其设置为中文编码格式。然后利用request.getParameter(String arg0)**方法接受请求的参数,最后打印出来
中文乱码问题解决办法
- 将请求参数以ISO-8859-1编码形式转换成字节数组,再将字节数组转换成字符串
- request.setCharacterEncoding(“UTF-8”);
访问web手段
1.地址栏
http://localhost:8088/jsp/c02/input_action.jsp?name=x&code=1234
只要在URL后面跟上一个"?",然后继续跟上**pname1=pvalue1&pname2=pvalue2&**即可
2.超链接
<a href="<%= basePath %>c02/input_action.jsp?code=00000&name=张三">张三访问</a>
题目:提交三条边,输出面积
<%@page import="java.util.regex.Pattern"%>
<%@ 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 'input_action.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">
</head>
<body>
<%
String a = request.getParameter("a");
String b = request.getParameter("b");
String c = request.getParameter("c");
if(a != "" && b != "" && c != "") {
Pattern pattern = Pattern.compile("[1-9]");
if(pattern.matcher(a).matches() && pattern.matcher(b).matches() && pattern.matcher(c).matches()) {
double bian_a = Double.parseDouble(a);
double bian_b = Double.parseDouble(b);
double bian_c = Double.parseDouble(c);
if(bian_a + bian_b <= bian_c || bian_a + bian_c <= bian_b || bian_b + bian_c <= bian_a) {
out.println("Sorry三条边不能构成三角形,请重新输入");
%>
<a href = "<%= basePath %>c02/triangle.jsp">返回</a>
<%
} else {
double p = (bian_a + bian_b + bian_c) / 2.0;
out.println("边长为" + bian_a + "," + bian_b + "," + bian_c + "的面积为:" + Math.sqrt(p * Math.abs(bian_a - p) * Math.abs(bian_b - p) * Math.abs(bian_c - p)));
}
} else {
out.println("边不能含有非数字信息,请重新输入");
%>
<a href = "<%= basePath %>c02/triangle.jsp">返回</a>
<%
}
} else {
out.println("缺少边的信息,请重新输入");
%>
<a href = "<%= basePath %>c02/triangle.jsp">返回</a>
<%
}
%>
</body>
</html>
input.jsp就不附上了,原来的代码改一下name,在新增一个输入框就行了