JSP的LOGIN程序代码
<!-- LOGIN MANAGER -->
<JAVA TYPE="CLASS">
PUBLIC STATIC BOOLEAN CHECKUSERPERMISSION(HTTPSESSION MYSESSION, HTTPSERVLETREQUEST REQUEST,
HTTPSERVLETRESPONSE RESPONSE)
{
IF (MYSESSION.GETVALUE("HASPERMISSION") == NULL || !((BOOLEAN) MYSESSION.GETVALUE
("HASPERMISSION" )).BOOLEANVALUE())
{
STRING REQUESTEDURL = HTTPUTILS.GETREQUESTURL(REQUEST).TOSTRING();
STRING QUERYSTRING = REQUEST.GETQUERYSTRING();
IF (QUERYSTRING != NULL)
{
REQUESTEDURL = REQUESTEDURL + "" + QUERYSTRING;
}
REQUESTEDURL = RESPONSE.ENCODEURL(REQUESTEDURL);
MYSESSION.PUTVALUE("REQUESTEDURL", REQUESTEDURL);
RETURN FALSE;
}
ELSE
{
RETURN TRUE;
}
}
</JAVA>
<%
// JSP文件
STRING GOODUSER = "BILL";
STRING GOODPASS = "BOBO";
HTTPSESSION MYSESSION = REQUEST.GETSESSION(TRUE);
STRING ERRORMESSAGE = "PLEASE LOGIN TO ACCESS THE PAGE YOU REQUESTED";
BOOLEAN LOGINATTEMPT = FALSE;
STRING MYMETHOD = REQUEST.GETMETHOD();
IF (REQUEST.GETPARAMETERVALUES("CLICK") != NULL && REQUEST.GETPARAMETERVALUES ("CLICK")
[0].TRIM().EQUALS("LOG IN"))
{
LOGINATTEMPT = TRUE;
}
IF (LOGINATTEMPT)
{
STRING USERNAME = REQUEST.GETPARAMETERVALUES("USER")[0].TRIM();
STRING PASSWORD = REQUEST.GETPARAMETERVALUES("PASS")[0].TRIM();
//OUT.PRINTLN("USERNAME = |" + USERNAME + "| & PASSWORD = |" + PASSWORD + "|<BR>");
IF (GOODUSER.EQUALS(USERNAME) && GOODPASS.EQUALS(PASSWORD))
{
RESPONSE.SENDREDIRECT((STRING)MYSESSION.GETVALUE("REQUESTEDURL"));
MYSESSION.PUTVALUE("HASPERMISSION", NEW BOOLEAN(TRUE));
ERRORMESSAGE = "UNABLE TO REDIRECT: " + (STRING) MYSESSION.GETVALUE("REQUESTEDURL");
}
ELSE
{
ERRORMESSAGE = "YOU DID NOT GET THE USERNAME OR PASSWORD RIGHT";
}
}
ELSE
{
ERRORMESSAGE = "HAVEN’T TRIED LOGGING IN YET.";
IF (MYSESSION.GETVALUE("REQUESTEDURL") == NULL)
{
MYSESSION.PUTVALUE("REQUESTEDURL", "/INDEX.JSP");
}
//OUT.PRINTLN("SET USERREFERRER TO " + MYSESSION.GETVALUE("REDIRECTTO") + "<BR>");
}
%>
<CENTER>
<FONT COLOR=RED><%=ERRORMESSAGE%></FONT>
<TABLE ALIGN=CENTER>
<FORM ACTION="ADMINLOGIN2.JSP" METHOD=POST NAME="LOGIN">
<TR>
<TD>USERNAME:</TD>
<TD><INPUT TYPE=TEXT NAME=USER VALUE=""></TD>
</TR>
<TR>
<TD>PASSWORD:</TD>
<TD><INPUT TYPE=PASSWORD NAME=PASS VALUE=""></TD>
</TR>
<TR>
<TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=CLICK VALUE="LOG IN"> </TD>
</TR>
</FORM>
</TABLE>
</CENTER>
ORIGINAL URL: <%= (STRING)MYSESSION.GETVALUE("REQUESTEDURL") %>
<SCRIPT LANGUAGE="JAVASCRIPT">
// <!--
IF (DOCUMENT.FORMS.LOGIN != NULL)
DOCUMENT.FORMS.LOGIN.USER.FOCUS();
// -->
</SCRIPT>
5.11 CHECKBOX在JSP中的使用方法
<%@ PAGE LANGUAGE="JAVA" CONTENTTYPE="TEXT/HTML" %>
<%@ PAGE IMPORT="COM.ORA.JSP.UTIL.*" %>
<HTML>
<BODY BGCOLOR="WHITE">
<FORM ACTION="CHECKBOX.JSP">
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="APPLE">APPLE<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="BANANA">BANANA<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="ORANGE">ORANGE<BR>
<INPUT TYPE="SUBMIT" VALUE="ENTER">
</FORM>
<%
STRING[] PICKED = REQUEST.GETPARAMETERVALUES("FRUITS");
IF (PICKED != NULL && PICKED.LENGTH != 0) {
%>
YOU PICKED THE FOLLOWING FRUITS:
<FORM>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="APPLE"
<%= ARRAYSUPPORT.CONTAINS(PICKED, "APPLE")"CHECKED" : "" %> >APPLE<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="BANANA"
<%= ARRAYSUPPORT.CONTAINS(PICKED,"BANANA")"CHECKED":"" %> >BANANA<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="ORANGE"
<%= ARRAYSUPPORT.CONTAINS(PICKED,"ORANGE")"CHECKED" : "" %> >ORANGE<BR>
</FORM>
<% } %>
</BODY>
</HTML>
5.12 REQUEST对象
如何获得一个运行时刻的JSP/SERVLET文件的绝对URL地址
STRING URL = REQUEST.GETREQUESTURL();
IF (REQUEST.GETQUERYSTRING() != NULL)
{
URL += ’’ + REQUEST.GETQUERYSTRING();
}
URL THEURL = NEW URL ( REQUEST.GETSCHEME() , REQUEST.GETSERVERNAME() ,
REQUEST.GETSERVERPORT(),URL);
OUT.PRINT(URL.TOSTRING());
如何知道客户端通过哪个URL访问本页面
STRING CALLINGPAGE = REQUEST.GETHEADER("REFERER");
OUT.PRINT(CALLINGPAGE);
如果FORM中出现好几个SUBMIT按钮,JSP/SERVLET如何判断哪个按钮进行了提交在FORM中可以这样定义:
<INPUT TYPE=SUBMIT NAME="NAME" VALUE="JOHN"><BR>
<INPUT TYPE=SUBMIT NAME="NAME" VALUE="JOE"><BR>
在JSP/SERVLET中使用REQUEST.GETPARAMETER("NAME"),根据返回值就可进行判断。
5.13 INCLUDE指令
这个指令让你可以在JSP编译成SERVLET的时候可以包含进一些文件。这个指令是这样的:
<%@ INCLUDE FILE="RELATIVE URL" %>
这个指定的URL通常是指向它的JSP页面的相关解释。包含的文件内容被当作JSP文本来分析,因此可以包含静态 HTML、SCRIPTING ELEMENTS、DIRECTIVES以及ACTIONS。
例如,很多站点的每个页面上都包含有小的导航条。这个 INCLUDE 是做这个的很好方法,省得开发者经常拷贝HTML到不同的文件中。例如:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<TITLE>SERVLET TUTORIAL: JAVASERVER PAGES (JSP) 1.0</TITLE>
<META NAME="AUTHOR" CONTENT="WEBMASTER@SOMESITE.COM">
<META NAME="KEYWORDS" CONTENT="...">
<META NAME="DESCRIPTION" CONTENT="...">
<LINK REL=STYLESHEET HREF="SITE-STYLES.CSS" TYPE="TEXT/CSS">
</HEAD>
<BODY>
<%@ INCLUDE FILE="/NAVBAR.HTML" %>
<!-- PART SPECIFIC TO THIS PAGE ... -->
</BODY>
</HTML>
既然文件是在页面被编译的时候插入的,如果导航条改变了,你需要去重新编译它所指向的所有JSP页面。注意,问题很容易在这里出现。一些读者在改变导航条后,重新运行包含导航条的JSP文件时,发现导航条不变。原因有两个:一是前面提到的INCLUDE指令在JSP编译成SERVLET的时候包含进导航条文件;第二就是JSP文件是被编译成SERVLET之后再运行的,如果服务器发现JSP文件没有被修改,则直接去调用已经编译好的SERVLET。如此一来,当调用JSP文件时,由于直接调用了编译好的SERVLET,显示的结果当然就是以前的导航条了。只要稍微修改一下JSP文件,该问题就可以自行解决。
如果导航条不是经常改变的,而且想要整个过程尽可能高效,那么在这样的环境下这是好的妥协。 如果这个 INCLUDED 文件经常改变,那么建议读者使用用 JSP:INCLUDE 行为(ACTION)来代替。
<!-- LOGIN MANAGER -->
<JAVA TYPE="CLASS">
PUBLIC STATIC BOOLEAN CHECKUSERPERMISSION(HTTPSESSION MYSESSION, HTTPSERVLETREQUEST REQUEST,
HTTPSERVLETRESPONSE RESPONSE)
{
IF (MYSESSION.GETVALUE("HASPERMISSION") == NULL || !((BOOLEAN) MYSESSION.GETVALUE
("HASPERMISSION" )).BOOLEANVALUE())
{
STRING REQUESTEDURL = HTTPUTILS.GETREQUESTURL(REQUEST).TOSTRING();
STRING QUERYSTRING = REQUEST.GETQUERYSTRING();
IF (QUERYSTRING != NULL)
{
REQUESTEDURL = REQUESTEDURL + "" + QUERYSTRING;
}
REQUESTEDURL = RESPONSE.ENCODEURL(REQUESTEDURL);
MYSESSION.PUTVALUE("REQUESTEDURL", REQUESTEDURL);
RETURN FALSE;
}
ELSE
{
RETURN TRUE;
}
}
</JAVA>
<%
// JSP文件
STRING GOODUSER = "BILL";
STRING GOODPASS = "BOBO";
HTTPSESSION MYSESSION = REQUEST.GETSESSION(TRUE);
STRING ERRORMESSAGE = "PLEASE LOGIN TO ACCESS THE PAGE YOU REQUESTED";
BOOLEAN LOGINATTEMPT = FALSE;
STRING MYMETHOD = REQUEST.GETMETHOD();
IF (REQUEST.GETPARAMETERVALUES("CLICK") != NULL && REQUEST.GETPARAMETERVALUES ("CLICK")
[0].TRIM().EQUALS("LOG IN"))
{
LOGINATTEMPT = TRUE;
}
IF (LOGINATTEMPT)
{
STRING USERNAME = REQUEST.GETPARAMETERVALUES("USER")[0].TRIM();
STRING PASSWORD = REQUEST.GETPARAMETERVALUES("PASS")[0].TRIM();
//OUT.PRINTLN("USERNAME = |" + USERNAME + "| & PASSWORD = |" + PASSWORD + "|<BR>");
IF (GOODUSER.EQUALS(USERNAME) && GOODPASS.EQUALS(PASSWORD))
{
RESPONSE.SENDREDIRECT((STRING)MYSESSION.GETVALUE("REQUESTEDURL"));
MYSESSION.PUTVALUE("HASPERMISSION", NEW BOOLEAN(TRUE));
ERRORMESSAGE = "UNABLE TO REDIRECT: " + (STRING) MYSESSION.GETVALUE("REQUESTEDURL");
}
ELSE
{
ERRORMESSAGE = "YOU DID NOT GET THE USERNAME OR PASSWORD RIGHT";
}
}
ELSE
{
ERRORMESSAGE = "HAVEN’T TRIED LOGGING IN YET.";
IF (MYSESSION.GETVALUE("REQUESTEDURL") == NULL)
{
MYSESSION.PUTVALUE("REQUESTEDURL", "/INDEX.JSP");
}
//OUT.PRINTLN("SET USERREFERRER TO " + MYSESSION.GETVALUE("REDIRECTTO") + "<BR>");
}
%>
<CENTER>
<FONT COLOR=RED><%=ERRORMESSAGE%></FONT>
<TABLE ALIGN=CENTER>
<FORM ACTION="ADMINLOGIN2.JSP" METHOD=POST NAME="LOGIN">
<TR>
<TD>USERNAME:</TD>
<TD><INPUT TYPE=TEXT NAME=USER VALUE=""></TD>
</TR>
<TR>
<TD>PASSWORD:</TD>
<TD><INPUT TYPE=PASSWORD NAME=PASS VALUE=""></TD>
</TR>
<TR>
<TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=CLICK VALUE="LOG IN"> </TD>
</TR>
</FORM>
</TABLE>
</CENTER>
ORIGINAL URL: <%= (STRING)MYSESSION.GETVALUE("REQUESTEDURL") %>
<SCRIPT LANGUAGE="JAVASCRIPT">
// <!--
IF (DOCUMENT.FORMS.LOGIN != NULL)
DOCUMENT.FORMS.LOGIN.USER.FOCUS();
// -->
</SCRIPT>
5.11 CHECKBOX在JSP中的使用方法
<%@ PAGE LANGUAGE="JAVA" CONTENTTYPE="TEXT/HTML" %>
<%@ PAGE IMPORT="COM.ORA.JSP.UTIL.*" %>
<HTML>
<BODY BGCOLOR="WHITE">
<FORM ACTION="CHECKBOX.JSP">
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="APPLE">APPLE<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="BANANA">BANANA<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="ORANGE">ORANGE<BR>
<INPUT TYPE="SUBMIT" VALUE="ENTER">
</FORM>
<%
STRING[] PICKED = REQUEST.GETPARAMETERVALUES("FRUITS");
IF (PICKED != NULL && PICKED.LENGTH != 0) {
%>
YOU PICKED THE FOLLOWING FRUITS:
<FORM>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="APPLE"
<%= ARRAYSUPPORT.CONTAINS(PICKED, "APPLE")"CHECKED" : "" %> >APPLE<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="BANANA"
<%= ARRAYSUPPORT.CONTAINS(PICKED,"BANANA")"CHECKED":"" %> >BANANA<BR>
<INPUT TYPE="CHECKBOX" NAME="FRUITS" VALUE="ORANGE"
<%= ARRAYSUPPORT.CONTAINS(PICKED,"ORANGE")"CHECKED" : "" %> >ORANGE<BR>
</FORM>
<% } %>
</BODY>
</HTML>
5.12 REQUEST对象
如何获得一个运行时刻的JSP/SERVLET文件的绝对URL地址
STRING URL = REQUEST.GETREQUESTURL();
IF (REQUEST.GETQUERYSTRING() != NULL)
{
URL += ’’ + REQUEST.GETQUERYSTRING();
}
URL THEURL = NEW URL ( REQUEST.GETSCHEME() , REQUEST.GETSERVERNAME() ,
REQUEST.GETSERVERPORT(),URL);
OUT.PRINT(URL.TOSTRING());
如何知道客户端通过哪个URL访问本页面
STRING CALLINGPAGE = REQUEST.GETHEADER("REFERER");
OUT.PRINT(CALLINGPAGE);
如果FORM中出现好几个SUBMIT按钮,JSP/SERVLET如何判断哪个按钮进行了提交在FORM中可以这样定义:
<INPUT TYPE=SUBMIT NAME="NAME" VALUE="JOHN"><BR>
<INPUT TYPE=SUBMIT NAME="NAME" VALUE="JOE"><BR>
在JSP/SERVLET中使用REQUEST.GETPARAMETER("NAME"),根据返回值就可进行判断。
5.13 INCLUDE指令
这个指令让你可以在JSP编译成SERVLET的时候可以包含进一些文件。这个指令是这样的:
<%@ INCLUDE FILE="RELATIVE URL" %>
这个指定的URL通常是指向它的JSP页面的相关解释。包含的文件内容被当作JSP文本来分析,因此可以包含静态 HTML、SCRIPTING ELEMENTS、DIRECTIVES以及ACTIONS。
例如,很多站点的每个页面上都包含有小的导航条。这个 INCLUDE 是做这个的很好方法,省得开发者经常拷贝HTML到不同的文件中。例如:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<TITLE>SERVLET TUTORIAL: JAVASERVER PAGES (JSP) 1.0</TITLE>
<META NAME="AUTHOR" CONTENT="WEBMASTER@SOMESITE.COM">
<META NAME="KEYWORDS" CONTENT="...">
<META NAME="DESCRIPTION" CONTENT="...">
<LINK REL=STYLESHEET HREF="SITE-STYLES.CSS" TYPE="TEXT/CSS">
</HEAD>
<BODY>
<%@ INCLUDE FILE="/NAVBAR.HTML" %>
<!-- PART SPECIFIC TO THIS PAGE ... -->
</BODY>
</HTML>
既然文件是在页面被编译的时候插入的,如果导航条改变了,你需要去重新编译它所指向的所有JSP页面。注意,问题很容易在这里出现。一些读者在改变导航条后,重新运行包含导航条的JSP文件时,发现导航条不变。原因有两个:一是前面提到的INCLUDE指令在JSP编译成SERVLET的时候包含进导航条文件;第二就是JSP文件是被编译成SERVLET之后再运行的,如果服务器发现JSP文件没有被修改,则直接去调用已经编译好的SERVLET。如此一来,当调用JSP文件时,由于直接调用了编译好的SERVLET,显示的结果当然就是以前的导航条了。只要稍微修改一下JSP文件,该问题就可以自行解决。
如果导航条不是经常改变的,而且想要整个过程尽可能高效,那么在这样的环境下这是好的妥协。 如果这个 INCLUDED 文件经常改变,那么建议读者使用用 JSP:INCLUDE 行为(ACTION)来代替。