思路:用request中的方法保存URL,存在session中,在loginAction中forward到一个loginSuccess.jsp,在这个页面中取得保存在session中的URL,用重定向标记重定向到原来的页面。这样,返回原来的页面并刷新。
另外,我发现用javascript的延迟跳转(<meta http-equiv="Refresh" content="0; URL=loginActionindexAction.do">)没有用jsp的重定向快(<logic:redirect page="/indexAction.do" />),不知是为什么?
(转贴)Jsp+javascript打造二级级联下拉菜单
数据库需求分析:
class(一级栏目信息):classId(自动编号),className(栏目名称)
Nclass(二级栏目信息):NclassId(自动编号),NclassName(栏目名称),parentId(一级栏目id,与class表中的classId关联)
<%@ page contentType="text/html; charset=GB2312" language="java" errorPage="../error.jsp" %>
<%@ include file="../conn.jsp"%>
<%@ include file="../ds.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%request.setCharacterEncoding("gb2312"); %>
<HTML><HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>级联菜单</TITLE>
<LINK rel="stylesheet" type="text/css" href="style.css">
</HEAD>
<!--从数据库中得到二级栏目信息-->
<%String sql="select * from Nclass order by NclassId asc";
ResultSet rs=stmt.executeQuery(sql);
%>
<!--将二级栏目信息保存到数组subcat中-->
<script type="text/javascript">
var onecount;
onecount=0;
subcat = new Array();
<%
int count = 0;
while(rs.next()){
%>
subcat[<%=count%>] = new Array("<%=rs.getString("NclassName")%>","<%=rs.getString("NclassId")%>","<%=rs.getString("parentId")%>");
<%
count++;
}
rs.close();
%>
onecount=<%=count%>;
<!--决定select显示的函数-->
function changelocation(locationid)
{
document.myform.NclassId.length = 0;
var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][2] == locationid)
{
document.myform.NclassId.options[document.myform.NclassId.length] = new Option(subcat[i][0], subcat[i][1]);
}
}
}
</script>
<FORM method="POST" name="myform" action="adminsave.jsp?action=add">
<TABLE>
<TR>
<TD>一级分类</TD>
<TD>
<SELECT name="classId" onChange="changelocation(document.myform.classId.options[document.myform.classId.selectedIndex].value)" size="1">
<OPTION selected value>==请选一级分类==</OPTION>
<sql:query var="query" dataSource="${bookdev}">
SELECT * FROM class
</sql:query>
<c:forEach var="row" items="${query.rows}">
<option value="${row.classId}">${row.className}</option>
</c:forEach>
</select>
</TD>
<TD>选择二级分类</TD>
<TD>
<SELECT name="NclassId">
<OPTION selected value>==请选二级分类==</OPTION>
</SELECT>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
posted on 2005-03-21 15:15 似水流年 阅读(915) 评论(5) 编辑 收藏 收藏至365Key 所属分类: JSP/Servlet
String类中concat(String str)方法的使用问题
这里的参数str是不能为null的,否则会出现:java.lang.NullPointerException错误,
如: String currentURL = request.getServletPath().concat("?").concat(request.getQueryString());
System.out.println("currentURL is: " + currentURL);
如果:request.getQueryString()==null,就会报错。
查看jdk1.4中String.java的源码如下:
public String concat(String str) {
int otherLen = str.length();//这里首先检索新字符串的长度,所以str不能为null.
if (otherLen == 0) {
return this;
}
char buf[] = new char[count + otherLen];
getChars(0, count, buf, 0);
str.getChars(0, otherLen, buf, count);
return new String(0, count + otherLen, buf);
}
所以:如果不知道一个字符串是否会为null,而且不再乎字符串中会跟一个null,就不应该用这种方法,可以这样用:
String currentURL = request.getServletPath().concat("?")+ request.getQueryString();
System.out.println("currentURL is: " + currentURL);
结果为:currentURL is: /bulletinAction.do?null
jdk1.4APIDOC中这个方法的文档:
concat
public String concat(String str)
Concatenates the specified string to the end of this string.
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
Examples:
"cares".concat("s") returns "caress"
"to".concat("get").concat("her") returns "together"
Parameters:
str - the String that is concatenated to the end of this String.
Returns:
a string that represents the concatenation of this object's characters followed by the string argument's characters.
(转贴)JSP/Servlet: session 的使用(1、2)
From Gossip@caterpillar
JSP/Servlet: session (1)
session隱含物件在轉換為Servlet之後,對應於 javax.servlet.http.HttpSession型態之物件, HttpSession負責管理客戶端在瀏覽網站期間的進程(session)資訊,也就是每個操作流程間的狀態維護,您無需知道進程追蹤的細節,就可以讓使用者在數個頁面之間瀏覽而又能保留相關的進程資訊,例如在幾個頁面之間挑選商品並放入購物車,在最後進行結帳工作。
之前在 Cookie 中介紹過進程追蹤的三種作法,其適用與不適用的場合各不相同。您可以將何時使用何種技術這個工作交給session(HttpSession)物件來負責,相關的實作細節並不太需要您來注意(當然您願意的話,建議您還是瞭解一下會更好)。
舉個例來說,session(HttpSession)會預設使用Cookie來追蹤進程,如果不行(例如使用者關閉Cookie的使用),則您可以試著使用URL rewriting,或許伺服器會保留一些資訊在伺服器的記憶體或資料庫甚至檔案中,而只傳送session id給客戶端,之後瀏覽器在每個請求中包括這個id,以讓伺服器取出對應的資訊,具體的作法依各家容器的實作而不同。
總而言之,您可以使用session(HttpSession)來協助您作進程追蹤,而不用擔心資訊在伺服端是如何保留的,而session id又是如何產生的。
下面簡單的示範session的使用方式,實作一個簡單的登入網頁,您可以大致瞭解session的使用方式,首先您必須先製作一個簡單的HTML表單,它將用來輸入使用者名稱與密碼:
form.htm
<html> <head><title>登入頁面</title></head> <body> 輸入密碼登入:<br> <form action="login.jsp" method="POST"> 名稱:<input type="text" name="user"> <br> 密碼:<input type="password" name="password"> <br> <input type="submit" value="登入"> </form> </body> </html>
接下來撰寫登入驗證頁面login.jsp:
login.jsp
<%@page contentType="text.html;charset=Big5"%> <% String user = request.getParameter("user"); String password = request.getParameter("password"); String memberURL = "http://localhost:8080/myjsp/member.jsp"; String loginFormURL = "http://localhost:8080/myjsp/form.html"; if(user == null || password == null) { response.setHeader("Refresh", "0; " + loginFormURL); } else if(user.equals("justin") && password.equals("1234")) { session.setAttribute("user", user); response.setHeader("Refresh", "3; " + memberURL); out.println(user + "歡迎登入!3秒後進入會員頁面!"); } else { response.setHeader("Refresh", "3; " + loginFormURL); out.println( "使用者或密碼錯誤,請重新登入(3秒後回到登入表單)"); } %>
上面這個頁面在驗證使用者名稱與密碼無誤之後,使用setAttribute()方法,將使用者名稱存入session (HttpSession)物件中,setAttribute()可以存入任何的Java物件,在這邊存入的屬性名稱為"user",存入的物件為 String物件,內容為登入者的名稱,也就是"justin",然後將客戶端重導至會員頁面,如果驗證的結果不正確,就將客戶端重導至登入表單;接下來撰寫會員頁面member.jsp:
member.jsp
<%@page contentType="text.html;charset=Big5"%> <% String loginFormURL = "http://localhost:8080/myjsp/form.html"; String user = (String) session.getAttribute("user"); if(user == null) { response.setHeader("Refresh", "3; " + loginFormURL); out.println( "喔喔!您還沒登入!不能偷看喔!3秒後進入登入頁面"); } else { out.println("嗨!" + user + "!"); out.println("會員好康在這邊。。。。"); // 立即登出 session.invalidate(); } %>
getAttribute()可以指定屬性名稱取得存在session(HttpSession)中的物件,取回的型態是 Object,為了能夠使用,您必須將之轉換為相對應的型態,在這邊則是String型態,如果之前login.jsp中驗證通過的話,在這個 member.jsp中就可以取得儲存在session中的屬性物件,如果取回的是null,表示之前沒有設定過user屬性,也就是在 login.jsp中沒有通過驗證,我們必須將之送回登入頁面以重新進行登入;invalidate()可以讓目前的session物件失效,我們在 member.jsp中執行這個方法,以實作登出的功能(其實也是為了測試的方便而讓使用者立即登出)。
上面這個例子中,完全沒有牽涉到進程追蹤的實作細節(cookie或是URL rewriting等),一切都是交由session物件來負責,而由於session可以儲存物件資訊,使得進程追蹤所關聯的資訊更為豐富,但卻不用增加您實作進程追蹤的困難,不過上面這個例子是在瀏覽器的Cookie功能有打開的情況下才能正常運作,如果Cookie沒有開啟,則我們需要作一些額外的動作,這在下一個主題中討論。
要注意的是,session並不是執行緒安全的,由於session會在客戶端對話期間存在,所以當有多個執行緒會存取session時,必須注意到執行緒安全問題。
From Gossip@caterpillar
JSP/Servlet: session (2)
session(HttpSession)物件會在使用者第一次存取Web伺服器時產生,伺服器會產生一個獨特的 session id來表示這個客戶端,瀏覽器在之後的每次請求中都包括這個session id(可能是使用Cookie或是URL rewriting,這個細節不用您來擔心),伺服器根據這個session id來對應該客戶端的session物件,您可以使用getId()方法來取得session id,例如:
<%
out.println("Session ID:" + session.getId());
%>
顯示的session id型式如下:
Session ID:2F892EDF2669858811B8D121119AE90B
session id預設是使用Cookie來儲存於客戶端,並在每一次瀏覽器發送請求時夾帶這個訊息給伺服器,伺服器根據session id來對應出HttpSession物件,假如Cookie沒有開啟,則瀏覽器將無法儲存session id,也就無法將session id的訊息傳送給伺服器,也就無法進行進程追蹤,即使資料物件確實儲存於HttpSession中,您也無法取出,下面這個程式在瀏覽器Cookie功能關閉的情況下,只會一直顯示session not found, reset!訊息:
sessionDemo.jsp
<%@page contentType="text/html;charset=Big5"%> <html> <head><title>session demo</title></head> <body> <H1> <% if(session.getAttribute("info") == null) { session.setAttribute("info", "session information"); out.println("session not found, reset!"); } else { out.println("session found: " + session.getAttribute("info")); } %> </H1> </body> </html>
如果Cookie功能關閉,則session id無法儲存,也就無法在下一次請求時一併送至伺服器,為了讓進程追蹤得以進行,您必須使用URL rewriting來傳送session id,所幸的是有一個簡單的方法可以幫您進行這個動作,使用response的encodeURL()可以自動將session id編進URL中,例如:
sessionDemo.jsp
<%@page contentType="text/html;charset=Big5"%> <html> <head><title>session demo</title></head> <body> <H1> <% if(session.getAttribute("info") == null) { session.setAttribute("info", "session information"); out.println("session not found, reset!"); } else { out.println("session found: " + session.getAttribute("info")); } out.println("<br><a href='" + response.encodeURL("sessionDemo.jsp") + "'>" + "進程追蹤" + "</a>"); %> </H1> </body> </html>
如果您的瀏覽器Cookie功能關閉,您必須使用response的encodeURL()自動將session id編進URL中,如果Cookie功能可以運作,則encodeURL()會原封不動的傳回指定的URL,否則會在指定的URL後加上sessiond id,例如上面的JSP網頁在Cookie功能關閉的情況下,會傳回以下的內容:
<html>
<head><title>session demo</title></head>
<body>
<H1>
session not found, reset!
<br><a href='sessiondemo.jsp;jsessionid=
7A2A0BFA32D0022D8BB80A5E690A9D10'>進程追蹤</a>
</H1>
</body>
</html>
簡單的說,按下經過URL rewriting的連結,瀏覽器就可以將session id傳送至伺服器,然而您的網址列上就會出現session id的訊息:
http://localhost:8080/myjsp/sessionDemo.jsp;jsessionid=7A2A0BFA32D0022D8BB80A5E690A9D10
這是一個有危險性的訊息,任何人只要在session的存活期限獲得這個訊息,就可以進行進程追蹤,所以基本上還是建議使用者開啟Cookie功能,以免 session id曝露在網址列上。
在上一個主題中的登入網頁如果在Cookie功能關閉的情況下也將無法運作,您必須這樣改寫login.jsp:
login.jsp
<%@page contentType="text.html;charset=Big5"%> <% String user = request.getParameter("user"); String password = request.getParameter("password"); String memberURL = "http://localhost:8080/myjsp/member.jsp"; String loginFormURL = "http://localhost:8080/myjsp/form.html"; if(user == null || password == null) { response.setHeader("Refresh", "0; " + loginFormURL); } else if(user.equals("justin") && password.equals("1234")) { session.setAttribute("user", user); memberURL = response.encodeURL(memberURL); response.setHeader("Refresh", "3; " + memberURL); out.println(user + "歡迎登入!3秒後進入會員頁面!"); } else { response.setHeader("Refresh", "3; " + loginFormURL); out.println( "使用者或密碼錯誤,請重新登入(3秒後回到登入表單)"); } %>
或者是您可以直接使用response的sendRedirect()方法,由於sendRedirect()要求完整的位址訊息,也就是包括http: //開始的位址訊息,您可以使用response的encodeRedirectURL()傳回這個位址,同樣的,如果 Cookie有開啟,則只是原封不動傳回原來指定的URL,您也可以改寫login.jsp程式如下:
login.jsp
<%@page contentType="text.html;charset=Big5"%> <% String user = request.getParameter("user"); String password = request.getParameter("password"); String memberURL = "http://localhost:8080/myjsp/member.jsp"; String loginFormURL = "http://localhost:8080/myjsp/form.html"; if(user == null || password == null) { response.setHeader("Refresh", "0; " + loginFormURL); } else if(user.equals("justin") && password.equals("1234")) { session.setAttribute("user", user); memberURL = response.encodeRedirectURL(memberURL); response.sendRedirect(memberURL); } else { response.setHeader("Refresh", "3; " + loginFormURL); out.println( "使用者或密碼錯誤,請重新登入(3秒後回到登入表單)"); } %>
session具有其存活期限,關閉瀏覽器、伺服器關閉可能讓session失效,當客戶端停止活動一段時間(Tomcat預設是30分鐘), session會自動失效,您可以使用getMaxInactiveInterval()取得session的等待期限,取得的值以秒為單位,或是以 setMaxInactiveInterval()設定等待期限,設定的值也是以秒為單位:
<%
out.println("default session life: " +
session.getMaxInactiveInterval());
session.setMaxInactiveInterval(600);
out.println("now session life: " +
session.getMaxInactiveInterval());
%>
您可以在web.xml中設定預設的session等待期限,使用<session-config>與<session- timeout>來進行設定,注意設定的單位為分鐘數,例如下面的設定將session等待期限預設為10分鐘:
web.xml
... <session-config> <session-timeout> 10 <!--分鐘--> </session-timeout> </session-config> ...
(转贴)如何防止IE缓存jsp文件
如何防止IE缓存jsp文件
[ 繁?中文 ] | 文章类别:JSP技巧 | 文章等级: | 发表日期:2001-1-10 星期三
[ 计数器 | 精彩博客 | 魔法表情 | 博客申请 | 源码下载 | IP查询 | 投票调查 | Html2Js ]
--------------------------------------------------------------------------------
转自:动态网制作指南 www.knowsky.com1, 使用java提供的方法,在jsp或者servlet中都可以
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
%>
2, 使用HTML标记,如下面:
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</HEAD>
(转贴)URL解码(Decode)/编码(Encode)
还不懂这个,放在这里供以后备查:
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
<title>URL解码(Decode)/编码(Encode)</title>
</head>
<body>
<center><font color=green size=+2>URL解码(Decode)/编码(Encode)</font><br>
需要解码的字符串:<TEXTAREA ID="String1" ROWS="10" COLS="30"></TEXTAREA> 解码后的字符串:<TEXTAREA ID="String2" ROWS="10" COLS="30"></TEXTAREA><br>
需要编码的字符串:<TEXTAREA ID="String3" ROWS="10" COLS="30"></TEXTAREA> 编码后的字符串:<TEXTAREA ID="String4" ROWS="10" COLS="30"></TEXTAREA><br>
<INPUT TYPE="button" ID="Decode" value="解码(Decode)" onClick="javascript:String2.value=decodeURI(String1.value);">
<INPUT TYPE="button" ID="Encode" value="编码(Encode)" onClick="javascript:String4.value=encodeURI(String3.value);">
</center>
</body>
</html>
LP5800->common conversation->social->clothes
Last week,I focused on learning from listening and reading the common coversation in Lp5800.I listened it threes mornings in a week.There were four morning I didn't learn it.I should learn english very day persistly.
Since last week,I have been reading and listening the sentences in Lp5800.
There are too many strange words for me,they are below:
shade of foundation,skin tone
light (dark) complexion
She wears heavy make up.
lipstick
in fashion
Do you follow what is going on in fashion?(follow?)
trendy style
sweater(毛衣)
It is getting cold.
wardrobe,outfit(?)
redo my wardrobe(redo?)
gorgious
It goes perfectly...
pearl necklace
extensive collection
hair style,new hairdo,It is easy to manage.frizz,shoulder length style
pony tail you used to have
curling iron
perfume
the fragrance you are wearing.
floral scent
perfume with a light scent,cologue
chunked...(?)
low platform
casual wear
fashionabe
heel
shoe-repair shop
HttpServletRequest的一些方法
配置tomcat5.0的端口为:80,应用的context path="",应用结果如下:
System.out.println("request url is: " + request.getRequestURI()); (Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.)
System.out.println("PathInfo is: " + request.getPathInfo());
System.out.println("getQueryString is: " + request.getQueryString());
System.out.println("getPathTranslated()is: " + request.getPathTranslated());
System.out.println("getServletPath() is: " + request.getServletPath()); (Returns the part of this request's URL that calls the servlet. This includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string.在Struts中的有关配置:web.xml中:
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
)
分别得到下面的结果:
request url is: /disGoalContentAction.do
PathInfo is: null
getQueryString is: goalID=619&naviStr=a10
getPathTranslated()is: null
getServletPath() is: /disGoalContentAction.do
配置tomcat5.0的端口为:8080,应用的context path="/learndiary",应用结果如下:
request url is: /learndiary/disGoalContentAction.do
PathInfo is: null
getQueryString is: goalID=313&naviStr=a10
getPathTranslated()is: null
getServletPath() is: /disGoalContentAction.do
从下面的网址可以得到一些有关request header的知识
http://web-sniffer.net/?url=http%3A%2F%2Fwww.learndiary.com%2FdisDiaryContentAction.do%3FsearchDiaryID%3D%26goalID%3D1219%26naviStr%3Da10a21105&submit=Submit&http=1.1&gzip=yes&type=GET&ua=Mozilla%2F4.0+%28compatible%3B+MSIE+6.0%3B+Windows+NT+5.0%3B+.NET+CLR+1.1.4322%29+Web-Sniffer%2F1.0.22
学习日记向您征集学习日记logo创意并请教1
朋友:
您好。一切都还好吗?我是学习日记的admin。
现在有两个事情打搅您:
1、学习日记向您征集学习日记logo创意:
学习日记创办到还没有一个代表我们形象的logo,请把你的想法和创意告诉我们,或者回复在学习日记网站的下列帖子的回复中:http://www.learndiary.com/disDiaryContentAction.do?goalID=1215(向广大网友征集学习日记logo创意,ps:学习日记的登录模式已经初步改变,不必登录即可查看帖子。)或在留言板中留言(游客身份可以留言)。如果可能的话,你能为我们制作一个你想像中的学习日记logo,我们将感激不尽(请发往邮箱:webmaster@learndiary.com)。
我们的口号是:学习到永远!!!我们致力于以JAVA技术构建一个普遍适用的开源网络学习交流平台。
2、请教1个问题:
1)怎样在游客登录系统成功后,回到并刷新他前的页面?
比如:我现在在不登录的情况直接输入url:http://www.learndiary.com/disDiaryContentAction.do?goalID=1215查看这个帖子,然后,我点击页面上部的“请登录”链接:http://www.learndiary.com/toLoginAction.do ,然后我在出现的登录窗口输入我的用户名和密码并成功登录后,请问:我怎么回到并刷新原页面:http://www.learndiary.com/disDiaryContentAction.do?goalID=1215 ?学习日记的最新源码见我们的开发者社区的CVS库中的old目录。
我们现在已经添加了导航系统,对数据存取层做了一点拆分,初步改变了登录模式。接下来,进一步改变登录模式。。。
在我们快速完成了一些有关用户使用的必要的改变后,根据情况,我们会启动新系统的全新设计。
今天孩子咳嗽发烧,把她送到医院门诊输液
这两天咳嗽加剧,昨晚和今天下午发烧,今天下午体温曾达到39。4度,我和她妈妈把她送到人民医院住院部找金医生看了。然后开了一些液体,因为小孩青霉素过敏,忘了给老师说,又麻烦老师重新开了药。
在门诊上输液,大概有3个小时。小孩的体温基本正常了。我们在晚上9:00过把她抱回了家。
找金老师看病是她妈妈的一个医院的朋友介绍的。没有挂号,人家对我们还挺客气的。
小孩的病还要认真对待,难关还没有过去。