How does Jsp realize webpage redirection?

Source: Internet
Author: User
Tags date now

1. You can use:

Response. sendRedirect ("http://www.foo.com/path/error.html ");

2. You can manually modify the Location attribute 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: This method can only be used before any output is sent to the client.

   5.6 practices similar to global. asa

There is no global. asa counterpart in JSP. However, there can be a workaround to run. For example, if you need to store or access the application scope variable, you can always create a Javabean and include it where the variables are needed on the page.

<Jsp: useBean id = "globals" scope = "application" class = "com. xxx. GlobalBean"/>

However, some products have the following features:

Allaire's product JRun 3.0 will provide global. jsa. JRun 2.3.3 is still supported, but only JSP 0.92 is supported. When JRun 3.0 is finally launched, it will support 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 JSP supports globals. jsa.

   5.7 jsp display 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 create the directory Mkdir (String path) in JSP)

<% @ Page import = "Java. io. *" %>
<%!
String Mkdir (String path)
{
String msg = null;
Java. io. File dir;

// Create a file object
Dir = new Java. io. File (path );
If (dir = null ){
Msg = "error cause: <BR> sorry, you cannot create an empty directory! ";
Return msg;
}
If (dir. isFile ()){
Msg = "error cause: <BR> an existing file with the same name <B>" + dir. getAbsolutePath () + "</B> exists. ";
Return msg;
}
If (! Dir. exists ())
{
Boolean result = dir. mkdirs ();
If (result = false ){
Msg = "error cause: <BR> directory <B>" + dir. getAbsolutePath () + "</B> creation failed. The cause is unknown! ";
Return msg;
}
// If the directory is successfully created, no output is displayed.
// Msg = "successfully created Directory: <B>" + dir. getAbsolutePath () + "</B> ";
Return msg;
}
Else {
Msg = "error cause: <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 <br> 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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.