站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题Jsp如何实现网页的重定向

Jsp如何实现网页的重定向

摘要: Jsp如何实现网页的重定向


  1.可以使用:

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0><tr><td>  response.sendRedirect(http://www.foo.com/path/error.html); </td></tr></table>
  2.可以手工修改HTTP header的Location属性,如下:

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0><tr><td><%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = /newpath/index.html;
response.setHeader(Location,newLocn);
%> </td></tr></table>
  3.也可以使用forward:

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0><tr><td>  <jsp:forward page=/newpage.jsp /> </td></tr></table>
  请注意:只能在任何输出还没有发送到客户端之前使用这种方式。

  5.6 类似global.asa的做法

  在JSP中没有global.asa的对应物。但可以有一个workaround来运行。例如,如果你需要存储或存取application scope变量,你总是可以创建一个Javabean,并在页面中需要这些变量的地方将它包含进来。

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0><tr><td><jsp:useBean id=globals scope=application class=com.xxx.GlobalBean/> </td></tr></table>
  但是,也有一些产品具有这样的对应:

  Allaire公司的产品JRun 3.0将提供global.jsa。JRun 2.3.3仍然给予支持,但只对JSP 0.92。当JRun 3.0最终推出时它将支持用于JSP 1.0和1.1的global.jsa。

  你可以从http://beta.allaire.com/jrun30得到JRun 3.0 beta 5

  另外,Oracle的JSP支持globals.jsa。

  5.7 jsp显示当前时间

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0><tr><td><%@ 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> </td></tr></table>
  5.8在JSP中创建目录 Mkdir(String path)

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0><tr><td><%@ page import=Java.io.* %>
<%!
String Mkdir(String path)
{
String msg=null;
Java.io.File dir;

// 新建文件对象
dir =new Java.io.File(path);
if (dir == null) {
msg = 错误原因:<BR>对不起,不能创建空目录!;
return msg;
}
    if (dir.isFile()) {
    msg = 错误原因:<BR>已有同名文件<B> + dir.getAbsolutePath() + </B>存在。;
    return msg;
    }
    if (!dir.exists())
{
    boolean result = dir.mkdirs();
        if (result == false) {
    msg = 错误原因:<BR>目录<b> + dir.getAbsolutePath() + </B>创建失败,原因不明!;
        return msg;
    }
   // 如果成功创建目录,则无输出。
    // msg =成功创建目录: <B> + dir.getAbsolutePath() + </B>;
    return msg;
    }
  else {
      msg = 错误原因:<BR>目录<b> + dir.getAbsolutePath() + </b>已存在。;
    }
  return msg;
}
%>
<%
String filepath = /usr/home/hoyi/html/dir;
String opmsg = Mkdir(filepath);
%> </td></tr></table>
  5.9将return 转为<br>函数

<table cellSpacing=0 cellPadding=0 width=600 bgColor=#ffffff border=0><tr><td>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;
}
</td></tr></table>



↑返回目录
前一篇: 通过JSP的预编译消除性能瓶颈
后一篇: jsp连接数据库大全