Java web学习笔记,转发,重定向具体代码例子,传参方式,乱码处理,转型【诗书画唱】
JS代码中优先使用单引号。
传参方式:
"1、浏览器地址栏中直接传入
http://localhost:8888/j190802/demo.jsp?act=admin&pwd=123&sex=男"
2、表单提交
2种乱码处理方式(下面是具体代码例子):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//乱码处理方式一:
//request.setCharacterEncoding("utf-8");
String a = request.getParameter("act");
String p = request.getParameter("pwd");
//乱码处理方式二 :
String sex = request.getParameter("sex");
String s = null;
if(sex != null && sex.length() > 0) {
s = new String(sex.getBytes("iso8859-1"),"GBK");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title></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>
<label>账号:</label><%=a %>
<br>
<label>密码:</label><%=p %>
<br>
<label>性别:</label><%=s %>
</body>
</html>
——————————
用parseDouble转型的方法:
<%@page import="com.jy.Product"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
//获取表单中输入的商品名称和商品价格
request.setCharacterEncoding("utf-8");
String name = request.getParameter("pname");
//从表单中获取到的价格是一个字符串
String strPrice = request.getParameter("price");
//将价格字符串strPrice转换成Double类型
Double price = null;
if(strPrice != null && strPrice.length() > 0) {
price = Double.parseDouble(strPrice);
}
Product p = new Product();
p.setName(name);
p.setPrice(price);
System.out.println(p);
%>
————————
跳转页面,重定向,跳转到百度等http://www.XXX.com:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
//乱码处理
request.setCharacterEncoding("utf-8");
//获取账号密码
String act = request.getParameter("act");
String pwd = request.getParameter("pwd");
System.out.println("你输入的账号是:" + act);
System.out.println("你输入的密码是:" + pwd);
String flag = request.getParameter("flag");
System.out.println(flag);
//跳转页面
//转发跳转页面
request.getRequestDispatcher("manage.jsp")
.forward(request, response);
//重定向
//response.sendRedirect("manage.jsp");
//跳转到百度
//response.sendRedirect("http://www.baidu.com");
%>
____________
登录注册页面跳转:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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>

<title></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">
<style type="text/css">
* {
font-size: 50px;
}
</style>
<script type="text/javascript">
//注册按钮的功能:跳转到注册页面
function toReg(){
window.location.href = 'reg.jsp?msg=Hello';
}
</script>
</head>
<body>
<form action="doLogin.jsp?flag=false" method="post">
<table border="1">
<tr>
<td>账号:</td>
<td><input type="text" name="act" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登录" />
<input type="button" value="注册" onclick="toReg();" />
</td>
</tr>
</table>
</form>
</body>
</html>
————————
后台管理页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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>

<title></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>
<h1>后台管理页面</h1>
</body>
</html>
——————
下面是设置只读和日期框:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String msg = request.getParameter("msg");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title></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>
<h1><%=msg %></h1>
<input type="text" readonly
onclick="new Calendar().show(this);" />
</body>
</html>
_______
下面是用JSP打印九九乘法表的两种方法:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//用StringBuilder拼接的方法:
StringBuilder html = new StringBuilder();
for(int i = 1;i <= 9;i ++) {
for(int j = 1;j <= i;j ++) {
html.append("<span>" + i + "*" + j + "=" + i * j + "</span>");
html.append(" ");
}
//System.out.println();
html.append("<br>");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title></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>
<%
//用Java和HTML等的代码混合的方法:
for(int i = 1;i <= 9;i ++) {
for(int j = 1;j <= i;j ++) {
%>
<span><%=i %>*<%=j %>=<%=i * j %></span>
<%
}
%>
<br>
<%
}
%>
</body>
</html>