Java web學(xué)習(xí)筆記,轉(zhuǎn)發(fā),重定向具體代碼例子,傳參方式,亂碼處理,轉(zhuǎn)型【詩書畫唱】
JS代碼中優(yōu)先使用單引號。
傳參方式:
"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轉(zhuǎn)型的方法:
<%@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轉(zhuǎn)換成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);
%>
————————
?? 跳轉(zhuǎn)頁面,重定向,跳轉(zhuǎn)到百度等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);
? ? //跳轉(zhuǎn)頁面
? ? //轉(zhuǎn)發(fā)跳轉(zhuǎn)頁面
? ? request.getRequestDispatcher("manage.jsp")
? ? ? ? .forward(request, response);
? ? //重定向
? ? //response.sendRedirect("manage.jsp");
? ? //跳轉(zhuǎn)到百度
? ? //response.sendRedirect("http://www.baidu.com");
%>
____________
登錄注冊頁面跳轉(zhuǎn):
<%@ 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">
? ? ? ? ? ? //注冊按鈕的功能:跳轉(zhuǎn)到注冊頁面
? ? ? ? ? ? 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>
——————
下面是設(shè)置只讀和日期框:
<%@ 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>