表单展开目录
- <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,在新增一个输入框就行了