這裏寫目錄標題
1、基本概念
1.1、什麼是jsp
在html頁面中嵌入Java腳本代碼
<%@ page language="java" import="java.util.*,java.text.*"
contentType= "text/html; charset=utf-8" %>
<html>
<head>
<title>輸出當前日期</title>
</head>
<body>
你好,今天是
<% SimpleDateFormat formater =
new SimpleDateFormat("yyyy年 MM月dd日");
String strCurrentTime = formater.format(new Date()); %>
<%=strCurrentTime %>
</body>
</html>
jsp的page屬性
在JSP頁面中計算兩個數的和,將結果輸出顯示
總結
2.1、get、post
概念
GET和POST是HTTP請求的兩種基本方法,一般用在表單提交中
<form action="welcome.jsp" method="GET">
登錄名:<input type="text" id="loginName" name="loginName"><br>
密 碼:<input type="text" id="password" name="password"><br>
<input type="submit" id="submit">
</form>
<form action="welcome.jsp" method="POST">
登錄名:<input type="text" id="loginName" name="loginName"><br>
密 碼:<input type="text" id="password" name="password"><br>
<input type="submit" id="submit">
</form>
get和post的區別
doGet/doPost
2、內置對象
2.1、九大內置對象
2.2、解決頁面中出現的中文亂碼
idea默認utf-8,但是eclipse編碼需要改變編碼方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
<%@ page language="java"contentType="text/html";charset=utf-8"%>
//注意統一編碼
request.setCharacterEncoding("utf-8");
//響應編碼設置
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("utf-8");
如果get請求出現亂碼
//治標的方法:
new String(s.getBytes(“iso-8859-1”),”utf-8”);
//治本的辦法:配置tomcat\conf\server.xml文件
//修改爲URIEncoding=”utf-8”,useBodyEncodingForURI=”ture”
2.3、在請求中保存和獲取屬性
2.5、request
request對象常用方法
request對象主要用於處理客戶端請求
<form name="form1" method="post" action="reginfo.jsp">
<table border="0" align="center">
<tr> <td>用戶名</td><td> <input type="text" name="name"></td></tr>
<tr><td>密碼</td><td > <input type="password" name="pwd"> </td></tr>
<tr> <td>信息來源</td><td>
<input type="checkbox" name="channel" value="報刊">報刊
<input type="checkbox" name="channel" value="網絡">網絡
<input type="checkbox" name="channel" value="朋友推薦"> 朋友推薦
<input type="checkbox" name="channel" value="電視"> 電視
</td></tr>
<!--省略提交、取消按鈕 -->
</table>
</form>
<%
//讀取用戶名和密碼
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
……//HTML頁面
//讀取複選框選擇項
String[] channels = request.getParameterValues("channel");
if (channels != null) {
for (String channel : channels) {
out.println(channel);
}
}
%>
2.6、response
response對象用於響應客戶請求並向客戶端輸出信息
頁面重定向
void sendRedirect(String location)
客戶端將重新發送請求到指定的URL
以下代碼實現登陸驗證並跳轉到歡迎頁面
<%
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
if ("sa".equals(name) && "sa".equals(pwd))
response.sendRedirect("welcome.jsp");
%>
但是頁面跳轉,請求的信息不一定跟着一起跳轉,此時可以使用轉發代替重定向實現頁面跳轉
轉發的作用:在服務器端,將請求發送給服務器上的其他資源,以共同完成一次請求的處理
RequestDispatcher對象的forward()方法實現轉發
<%
RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
%>
2.7、session
一個會話就是在瀏覽器和服務器之間的一次通話,如果接通就會一直在會話,直到掛斷
會話可以在多次請求中保存和使用數據
獲取用戶請求的登錄信息進行驗證
<%
if ("admin".equals(name) && "admin".equals(pwd)) { // 如果是已註冊用戶
session.setAttribute("login", name);
// 設置session過期時間
session.setMaxInactiveInterval(10*60);
request.getRequestDispatcher("admin.jsp").forward(request, response);
} else {
response.sendRedirect("index.jsp");
}
%>
每個session都有唯一的id,這個id可以直接打印出來
Session.invalidate();//讓session失效,手動設置無效
Session.removeAttribute(“userName”);//移除session中指定的數據
Session.setMaxInactiveInterval(5);//設置session的生命週期
然後通過設置項目的web.xml或Tomcat目錄下的/conf/web.xml文件,單位是分鐘
<session-config>
<session-timeout>10</session-timeout>
</session-config>
整個session的過程如下
如何避免多餘代碼
可以將一些共性的內容寫入一個單獨的文件中,然後通過include指令引用該文件
創建登錄驗證文件 loginControl.jsp
<%
String login = (String) session.getAttribute("login");
if (login == null) {
response.sendRedirect("index.jsp");
return;
} %>
在後臺首頁面中使用include指令引用登錄驗證文件
<%@ include file="loginControl.jsp" %>
2.7、cookie
使用cookie自動填充用戶名
步驟
1、將用戶名保存到cookie
Cookie cookie=new Cookie(“userName”,usernName);
2、
Response.addCookie(cookie);
2.8、application
<%\
Int count=1;
Object count1=application.getAttribute(“count”);
If(count1!=null){
Count=count+Integer.ParseInt(count1.toString());
}
%>
<h3>
<%=count%>
</h3>