JS笔记|request.getParameter()|request.setCharacterEncoding()|10-12
Posted 向阳-Y.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS笔记|request.getParameter()|request.setCharacterEncoding()|10-12相关的知识,希望对你有一定的参考价值。
index.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 'index.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="display.jsp" method="post">
值1<input type="text" name="num1"/><br/>
值2<input type="text" name="num2"/>
<select name="opt">
<option value="+">加</option>
<option value="-">减</option>
<option value="*">乘</option>
<option value="/">除</option>
</select>
<input type="submit" value="等于"/>
</form>
</body>
</html>
display.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 'display.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8"); //设置接收的编码,method需要设置为post
String num1 = request.getParameter("num1");
String num2 = request.getParameter("num2");
String opt = request.getParameter("opt");
double i1=Integer.parseInt(num1);
double i2=Integer.parseInt(num2);
// ---------------------------
// 数值型转换为字符串
// String.valueof(int i);
// 100+" ";
// 字符串转数值型
// Integer.parseInt(String s);
// Double.parseDouble(String s);
// Float.parseFloat(String s);
// ---------------------------
double result=0;
if("+".equals(opt)){
result=i1+i2;
}else if("-".equals(opt)){
result=i1-i2;
}else if("*".equals(opt)){
result=i1*i2;
}else if("/".equals(opt)){
result=i1/i2;
}
// byte[] bytes = username.getBytes("ISO-8859-1"); //唯一差别就是method可以等于get
// username = new String(bytes, "UTF-8"); //这两句功能基本等同于setCharacter....
%>
运算结果是:<%=num1 %><%=opt %><%=num2 %>=<%=result %>
</body>
</html>
以上是关于JS笔记|request.getParameter()|request.setCharacterEncoding()|10-12的主要内容,如果未能解决你的问题,请参考以下文章
ajax post 请求 ,java端使用 request.getParameter 获取不到数据问题
request.setAttribute()与getParameter() 的区别
request.getParameter() request.getInputStream()和request.getReader()三者的区别
request.getParameter()request.getInputStream()和request.getReader()