Java遇見HTML的6篇文章技術(shù)較老只是在熟悉java基礎(chǔ)知識(shí)和了解mvc模型思想
session內(nèi)置對(duì)象
session表示客戶端與服務(wù)端的一次會(huì)話。
image.png
image.png
image.png
image.png
image.png
session生命周期
創(chuàng)建、活動(dòng)、銷毀
image.png
image.png
application內(nèi)置對(duì)象
image.png
image.png
<%
application.setAttribute("username","zhoujielun");
application.setAttribute("age",20);
application.setAttribute("city","北京");
Enumeration at=application.getAttributeNames();
while (at.hasMoreElements())
{
out.println(at.nextElement()+"  ");
}
%>
page內(nèi)置對(duì)象、pageContext對(duì)象和config對(duì)象
image.png
image.png
image.png
exception對(duì)象
與java中的exception類似。
JavaBeans
JavaBeans就是創(chuàng)建一個(gè)java類,該類是public公有的類,有private私有的屬性,有一個(gè)無參的構(gòu)造方法,有公有的get和set方法。
創(chuàng)建一個(gè)實(shí)體類:
image.png
在jsp中創(chuàng)建實(shí)體類對(duì)象,調(diào)用set方法賦值,get方法獲取值:
image.png
javabean動(dòng)作元素
image.png
<jsp:userBean>動(dòng)作:
image.png
<jsp:setProperty>動(dòng)作:
在login.jsp中寫一個(gè)表單,表單中定義的字段name與student類中的字段名稱對(duì)應(yīng),表單指向dologin.jsp:
image.png
image.png
mypass的值是在login.jsp表單指向dologin.jsp的路徑參數(shù)中:
image.png
<jsp:getProperty>動(dòng)作:
直接獲取用戶信息:
<jsp:useBean id="getstudentbean" class="com.zhidaoauto.model.Student" scope="page"/>
學(xué)生姓名:<jsp:getProperty name="getstudentbean" property="studentname"/>
javabean不僅僅是對(duì)象實(shí)體類,也可以是業(yè)務(wù)邏輯類。
比如把校驗(yàn)登錄信息的業(yè)務(wù)邏輯放到一個(gè)類中,在jsp中引用這個(gè)類去校驗(yàn)
dao業(yè)務(wù)邏輯類:
image.png
package com.zhidaoauto.dao;
import com.zhidaoauto.model.Student;
/*
專門寫業(yè)務(wù)邏輯層
*/
public class StudentDao {
public Boolean studentLogin(Student student){
if (student.getStudentname().equals("qzx")){
return true;
}else {
return false;
}
}
}
dologin.jsp中使用usebean引用Student類和StudentDao類
image.png
jsp狀態(tài)管理
image.png
image.png
一個(gè)小例子講解cookie的使用,先進(jìn)入login.jsp中輸入用戶名和密碼跳轉(zhuǎn)到dologin.jsp,通過超鏈接再跳轉(zhuǎn)到userinfo.jsp中查看通過cookie存儲(chǔ)的用戶名和密碼
login.jsp頁面:
<%@ page import="java.net.URLDecoder" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/4/23
Time: 14:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<h1>用戶登錄</h1>
<hr>
<%--先進(jìn)入login.jsp中輸入用戶名和密碼跳轉(zhuǎn)到dologin.jsp,通過超鏈接再跳轉(zhuǎn)到userinfo.jsp中查看通過cookie存儲(chǔ)的用戶名和密碼--%>
<%
request.setCharacterEncoding("utf-8");
//讓輸入框中回顯上一次輸入的信息,其實(shí)就是把保存的cookie值展示出來
Cookie[] c=request.getCookies();
String username="";
String password="";
if (c!=null && c.length>0){
for (Cookie cookie:c){
if (cookie.getName().equals("username")){
username= URLDecoder.decode(cookie.getValue(),"utf-8");
}else if(cookie.getName().equals("password")) {
password=URLDecoder.decode(cookie.getValue(),"utf-8");
}
}
}
%>
<form action="dologin.jsp" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username" value="<%=username%>"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password" value="<%=password%>"></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="ischeck" checked="checked">是否保存用戶信息</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登錄"></td>
</tr>
</table>
</form>
</body>
</html>
dologin.jsp頁面:
<%@ page import="java.net.URLEncoder" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/4/23
Time: 14:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>dologin</title>
</head>
<body>
<h1>登錄成功</h1>
<hr>
<%
//設(shè)置字符集編碼是utf-8,這樣能接收輸入的中文信息
request.setCharacterEncoding("utf-8");
//先判斷用戶是否勾選了保存用戶名和密碼,ischeck是login.jsp頁面中復(fù)選框的name值
String[] ischeck=request.getParameterValues("ischeck");
//ischeck不為空并且長度大于0,代表勾選了復(fù)選框
if (ischeck !=null && ischeck.length>0){
//獲取用戶名和密碼保存到cookie中,并設(shè)置cookie的最大時(shí)效是10天
//URLEncoder.encode(參數(shù)值,“utf-8”) 設(shè)置字符集為utf-8,后面獲取cookie值是要解碼URLDecoder.decode(參數(shù)值,“utf-8”)
Cookie cookie=new Cookie("username",URLEncoder.encode(request.getParameter("username"),"utf-8"));
Cookie cookie1=new Cookie("password",URLEncoder.encode(request.getParameter("password"),"utf-8"));
//設(shè)置cookie最大時(shí)效是10天,以秒為單位
cookie.setMaxAge(864000);
cookie1.setMaxAge(864000);
//把cookie存到response中
response.addCookie(cookie);
response.addCookie(cookie1);
}else {
//不保存到cookie中,設(shè)置cookie時(shí)長為0,清空cookie
Cookie[] c=request.getCookies();
if (c!=null && c.length>0){
for (Cookie cookie:c){
if (cookie.getName().equals("username") || cookie.getName().equals("password")){
cookie.setMaxAge(0);
//同時(shí)把清空的cookie存到response中
response.addCookie(cookie);
}
}
}
}
%>
<a href="userinfo.jsp">查看用戶信息</a>
</body>
</html>
userinfo.jsp頁面:
<%@ page import="java.net.URLDecoder" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/4/23
Time: 14:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用戶信息</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String username="";
String password="";
//從request中獲取cookie中的用戶名和密碼
Cookie[] c=request.getCookies();
if (c != null && c.length>0){
for (Cookie cookie:c){
if (cookie.getName().equals("username")){
username= URLDecoder.decode(cookie.getValue(),"utf-8");
}else if (cookie.getName().equals("password")){
password=URLDecoder.decode(cookie.getValue(),"utf-8");
}
}
}
%>
用戶名:<%=username%><br>
密碼:<%=password%>
</body>
</html>
image.png
image.png
image.png