<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/Javascript">
function fun(){
var uid = document.getElementById("uid").value;
if(uid == ""){
alert("請(qǐng)輸入數(shù)據(jù)");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<!-- 鏈接到page2 -->
<a href="page2.jsp">鏈接到page2</a><br>
<!-- 鏈接到page2,彈出新窗口 -->
<a href="page2.jsp" target="_blank">鏈接到page2</a><br>
<!-- 相對(duì)路徑鏈接到page2 -->
<a href="./page2.jsp">鏈接到page2</a><br>
<a href="../demo1/page2.jsp">鏈接到page2</a><br>
<!-- 絕對(duì)路徑鏈接到page2 -->
<a href="http://localhost:8080/web1/demo1/page2.jsp">鏈接到page2</a><br>
<a href="<%=request.getContextPath() %>/demo1/page2.jsp">鏈接到page2</a><br>
<!-- 鏈接到page2,并傳參 -->
<a href="page2.jsp?uid=admin">鏈接到page2</a><br>
<!-- 使用js鏈接到page2 -->
<a href='javascript:window.location="page2.jsp";'>鏈接到page2</a><br>
<!-- 使用js鏈接到page2,并傳參 -->
<a href='javascript:window.location="page2.jsp?uid=admin";'>鏈接到page2</a><br>
<!-- 使用js鏈接到page2,彈出新窗口,并傳參 -->
<a href='javascript:window.open("page2.jsp?uid=admin");'>鏈接到page2</a><br>
<!-- submit提交表單 -->
<form action="page2.jsp" method="post">
<input type="text" name="uid">
<input type="submit" value="提交">
</form>
<!-- 驗(yàn)證后提交表單 -->
<form action="page2.jsp" method="post">
<input type="text" name="uid" id="uid">
<input type="submit" value="驗(yàn)證后提交表單" onclick="return fun()">
</form>
<!-- url方式提交數(shù)據(jù) -->
<form action="page2.jsp?uid=admin" method="post">
<input type="text" name="uid">
<input type="submit" value="提交">
</form>
<!-- hidden方式提交數(shù)據(jù) -->
<form action="page2.jsp" method="post">
<input type="hidden" name="uid2" id="uid2" value="admin">
<input type="submit" value="提交">
<script type="text/javascript">
document.getElementById("uid2").value = "admin22222";
</script>
</form>
<!-- button+js提交表單 -->
<form id="form1" action="page2.jsp" method="post">
<input type="text" name="uid">
<input type="button" value="提交" onclick='document.getElementById("form1").submit();'>
</form>
<!-- <a>+js提交表單 -->
<form id="form2" action="page2.jsp" method="post">
<input type="text" name="uid">
<a href='javascript:document.getElementById("form2").submit();'>提交表單</a>
</form>
</body>
</html>
5.2 中文亂碼解決方法詳見文檔


String xing = request.getParameter("xing");
xing = new String(xing.getBytes("ISO-8859-1"),"utf-8");
這種方法很費(fèi)事,當(dāng)變量極少時(shí)可以考慮用這種方式。
添加 URlEncoding=”utf-8’ (此方法對(duì)get提交的方式有效)

Request.setCharacterEncoding(“utf-8”); //獲取參數(shù)前
<%
Cookie c = new Cookie("name","張曼");
response.addCookie(c);
%> 此時(shí)頁面可能會(huì)出現(xiàn)500錯(cuò)誤,但不決對(duì)。
需要在存放和獲取的時(shí)候進(jìn)行編碼:
<%
Cookie c = new Cookie("name",URLEncoder.encode("張曼"));
response.addCookie(c);
%>
<%
String name = "";
Cookie[] cook = request.getCookies();
if(cook != null)
{
for(Cookie c : cook){
if(c.getName().equals("name")){
name = URLDecoder.decode(c.getValue());
}
}
}
 out. %> 詳見下文的過濾器 是Web容器加載的一組類的實(shí)例,不想一般的java對(duì)象那樣用 new 去獲取實(shí)例,而是直接在JSP 頁面使用的對(duì)象。  表示客戶端的請(qǐng)求,包含了所有的請(qǐng)求信息。  String getParameter(String name) : 根據(jù)請(qǐng)求頁面表單組件名稱獲取請(qǐng)求頁面提交的數(shù)據(jù)  String[] getparameterValues: 獲取頁面請(qǐng)求中一個(gè)表單組建對(duì)應(yīng)多個(gè)值時(shí)的用戶的請(qǐng)求數(shù)據(jù)。(例如復(fù)選框)  getRemoteAddr() 返回發(fā)送請(qǐng)求的主機(jī)的網(wǎng)絡(luò)地址  getRequestURL() 請(qǐng)求的URL  處理JSP生成的響應(yīng),然后將響應(yīng)結(jié)果發(fā)送給客戶端。 void setContentType(String name): 設(shè)置作為響應(yīng)生成的內(nèi)容的類型和字符編碼 void sendRedirect(String): 請(qǐng)求的另一個(gè)URL 表示信息的輸出  Print() println() write()
表示用戶的會(huì)話狀況,用此機(jī)制可以輕易識(shí)別每一個(gè)用戶,能保存和跟蹤用戶的會(huì)話狀態(tài)。 (例如,購(gòu)物車)。
Void SetAttribute(String name,Object value);
Void getAttribute(String name);
application作用的范圍比session更大,不僅僅是在同一個(gè)窗口,而是作用在整個(gè)應(yīng)用程序,所有客戶端窗口都可以共享該對(duì)象,從服務(wù)器啟動(dòng)開始就存在,直到服務(wù)器關(guān)閉為止。他是 ServletContext的對(duì)象(全局)
方法同 session
pageContext使用戶可以訪問頁面作用域中定義的所有隱式對(duì)象。
方法同 session
Ø Servlet對(duì)象 page提供對(duì)網(wǎng)頁上定義的所有對(duì)象的訪問。Page對(duì)象表示頁面本身,它是 java.lang.Object類的一個(gè)實(shí)例。 (很少用到)
<%= ((javax.servlet.jsp.HttpJspPage)page).getServletInfo() %>
config存儲(chǔ)servlet的一些初始化信息(很少用到)
Ø 錯(cuò)誤對(duì)象只提供了一個(gè) exception的錯(cuò)誤對(duì)象,利用exception對(duì)象處理執(zhí)行過程中引發(fā)的異常。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注