当前位置: 移动技术网 > IT编程>开发语言>Java > Jsp如何实现网页的重定向

Jsp如何实现网页的重定向

2017年12月12日  | 移动技术网IT编程  | 我要评论
  1.可以使用:

  response.sendredirect("http://www.foo.com/path/error.html");

  2.可以手工修改http header的location属性,如下:

<%
response.setstatus(httpservletresponse.sc_moved_permanently);
string newlocn = "/newpath/";
response.setheader("location",newlocn);
%>

  3.也可以使用forward:

  <jsp:forward page="/newpage.jsp" />

  请注意:只能在任何输出还没有发送到客户端之前使用这种方式。

  5.6 类似global.asa的做法

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

<jsp:usebean id="globals" scope="application" class="com.xxx.globalbean"/>

  但是,也有一些产品具有这样的对应:

  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显示当前时间

<%@ 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在jsp中创建目录 mkdir(string path)

<%@ 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);
%>

  5.9将return 转为<br>函数

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;
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网