jsp无法使用响应重定向页面

时间:2014-03-25 06:21:35

标签: java jsp redirect

我无法使用response.sendRedirect(url)重定向到该页面;

我可以重定向到公共jsp页面中的链接,但是我无法在jsp页面中定义的函数中发送它。所以在我的情况下,哪个重定向函数,如何解决?

仅供参考,错误是"无法解决响应"。

以下是我的源代码的一部分:

<%@ page language="java" contentType="text/html; charset=utf-8" 
                         pageEncoding="utf-8" %>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="java.util.*,java.io.*"%>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>   


<%!
public void sendSMS(String nickName, String setAname, String currAname, 
                                                      String toPhone){
String acctInfo="xxx";
String acctPwd="xxx";
String contents="Message from Miss U:( \n Please be reminded that your friend:"+nickName+" is OUT of your setting area: "+setAname+". The current area is "+currAname+".";

String smsURL="http://api.accessyou.com/sms/sendsms-utf8.php?msg="+contents+"&phone="+toPhone+"&pwd="+acctPwd+"&accountno="+acctInfo;
response.sendRedirect(smsURL);
}
%>

3 个答案:

答案 0 :(得分:1)

当您调用辅助函数时,您必须传入response对象。原因是JSP被转换为Java代码,然后编译它以便它可以在JVM上运行。 <%!...%>之间的事物将被转换为与<%...%>中的代码具有不同上下文的辅助方法。

你的jsp会被翻译成这样的东西:

public class MyJsp {

    public void doPost(HttpServletRrequest request, HttpServletResponse response) {
      /* tons of code compiled off JSP */
      sendSMS(...)
    }

    private void sendSMS(String nickName, String setAnmae...) {

        String acctInfo="xxx";

        response.sendRedirect(...); // error, there is no response object in the local context
    }

}

这样的事情会起作用

%@ page language="java" contentType="text/html; charset=utf-8" 
                         pageEncoding="utf-8" %>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="java.util.*,java.io.*"%>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>   

<%
 sendSMS(arg1, arg2, arg3, ar4, response)
%>

<%!
public void sendSMS(String nickName, 
                    String setAname, 
                    String currAname,                                                         
                    String toPhone,
                    ServletResponse response){
    String acctInfo="xxx";
    String acctPwd="xxx";
    String contents="Message from Miss U:( \n Please be reminded that your friend:"+nickName+" is OUT of your setting area: "+setAname+". The current area is "+currAname+".";

    String smsURL="http://api.accessyou.com/sms/sendsms-utf8.php?msg="+contents+"&phone="+toPhone+"&pwd="+acctPwd+"&accountno="+acctInfo;
    response.sendRedirect(smsURL);
}
%>

请注意sendSMS您无法直接访问requestresponse对象。

答案 1 :(得分:0)

您可以按照以下方式执行此操作

 <%@ page import="java.io.*,java.util.*" %>
    <html>
     <head>
      <title>Page Redirection</title>
     </head>
    <body>
     <center>
      <h1>Page Redirection</h1>
     </center>
    <%
     // New location to be redirected
     String site = new String("http://www.xyz.com");
     response.setStatus(response.SC_MOVED_TEMPORARILY);
     response.setHeader("Location", site); 
    %>
   </body>
   </html>

答案 2 :(得分:0)

示例代码:

<%! public void sendSMS(HttpServletResponse response){
try{
response.sendRedirect("th.jsp");}
catch(Exception e){
    e.printStackTrace();
}
} %>
<%sendSMS(response); %>