js| Web page
1. You can use:
FFFFFF border=0> response.sendredirect ("http://www.foo.com/path/error.html");
2. You can manually modify the Location property of the HTTP header as follows:
<%
Response.setstatus (httpservletresponse.sc_moved_permanently);
String NEWLOCN = "/newpath/index.html";
Response.setheader ("Location", NEWLOCN);
%>
3. You can also use forward:
<jsp:forward page= "/newpage.jsp"/>
Note that this method can only be used before any output has been sent to the client.
5.6 Similar to global.asa practices
There is no Global.asa counterpart in the JSP. But you can have a workaround to run it. For example, if you need to store or access a application scope variable, you can always create a javabean and include it in the page where you need those variables.
<jsp:usebean id= "Globals" scope= "Application" class= "Com.xxx.GlobalBean"/>
However, there are some products that have such a correspondence:
Allaire Company's Product JRun 3.0 will provide GLOBAL.JSA. JRun 2.3.3 still gives support, but only for JSP 0.92. When JRun 3.0 is finally launched, it will support the GLOBAL.JSA for JSP 1.0 and 1.1.
You can get JRun 3.0 Beta 5 from Http://beta.allaire.com/jrun30.
In addition, Oracle's JSP supports GLOBALS.JSA.
5.7 JSP Displays the current time
<%@ page import= "java.util.*, java.text.*"%>
<HTML>
<HEAD>
<TITLE> JSP to display the current time </TITLE>
</HEAD>
<BODY>
The current time is:
<%
Date now = new Date ();
Out.println (Dateformat.gettimeinstance (). Format (now));
%>
</BODY>
</HTML>
5.8 Creating the directory Mkdir in the JSP (String path)
<%@ page import= "java.io.*"%>
<%!
String Mkdir (String path)
{
String Msg=null;
Java.io.File dir;
New File Object
Dir =new Java.io.File (path);
if (dir = = null) {
msg = "Error reason: <BR> Sorry, cannot create empty directory! ";
return msg;
}
if (Dir.isfile ()) {
msg = "Error Reason: <BR> There is already a file with the same name <B>" + dir.getabsolutepath () + "</B> exists. ";
return msg;
}
if (!dir.exists ())
{
Boolean result = Dir.mkdirs ();
if (result = = False) {
msg = "Error reason: <BR> directory <b>" + dir.getabsolutepath () + "</B> creation failed for unknown reason! ";
return msg;
}
If the directory is successfully created, there is no output.
msg = "Successfully created directory: <B>" + dir.getabsolutepath () + "</B>";
return msg;
}
else {
msg = "Error reason: <BR> directory <b>" + dir.getabsolutepath () + "</b> already exists. ";
}
return msg;
}
%>
<%
String filepath = "/usr/home/hoyi/html/dir";
String opmsg = Mkdir (filepath);
%>
5.9 Convert return to
function
public static string Returntobr (String sStr)
{
if (SSTR = = NULL//Sstr.equals (""))
{
return sStr;
}
String sTmp = new string ();
int i = 0;
while (I <= sstr.length ()-1)
{
if (Sstr.charat (i) = = ' \ n ')
{
STMP = Stmp.concat ("<br>");
}
Else
{
STMP = Stmp.concat (sstr.substring (i,i+1));
}
i++;
}
return STMP;
}