无法在sendRedirect之后调用另一个方法

时间:2017-02-21 01:03:52

标签: java servlets

使用servlet调用sendRedirect后是否可以调用另一个方法。我还使用了return或response.setHeader(" REFRESH"," 5; url = / Receive.jsp");与之前的Srackoverflow答案一样。有没有办法实现这个......? 它会打开谷歌页面,但不会/接收页面。

基本上我想让手机没有验证,在进入手机号码后它会点击api链接并返回到带有otp文本框的新jsp页面。

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SignUp extends HttpServlet  {

    protected void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
           String  mobno=request.getParameter("mobile");

                    response.sendRedirect("https://google.com");
                    response.setHeader("REFRESH", "5;url=/Receice.jsp"); 
        }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String mobno = request.getParameter("mobile");

        if (mobno.equals("")) {
            PrintWriter out = response.getWriter();
            out.print("mob number can't be empty");
            out.flush();
            response.setContentType("text/html;charset=UTF-8");
            RequestDispatcher myDispatch = request
                    .getRequestDispatcher("/OtpVerification.html");
            myDispatch.include(request, response);
        } else {
            request.setAttribute("TextValue", mobno);
            doGet(request, response);
            request.getRequestDispatcher("/Receive.jsp");


        }
    }


}
    [here is the screen shot of the above code][1]

1 个答案:

答案 0 :(得分:0)

您可以设置一个拦截调用google.com

的过滤器

首先,在web.xml文件中配置它:

<filter>
<filter-name>SomeName123</filter-name>
<filter-class>packagename.FilterClassThatWillTakeCareOfTheSaidIssue</filter-class>
</filter>

<filter-mapping>
<filter-name>SomeName123</filter-name>   //name here should be exactly the same as above <filter-name> 
<url-pattern>/google.com</url-pattern> //play around with this google.com until you get it right
</filter-mapping>

设置完成后,现在创建一个实现javax.servlet.Filter的Filter类,并将其命名为FilterClassThatWillTakeCareOfTheSaidIssue或其他(名称必须与Web中<filter-class>命名空间中的名称相同。 xml),覆盖doFilter方法,然后在那里应用你的逻辑:

HttpServletResponse res = (HttpServletResponse) response;
res.setHeader("REFRESH", "5;url=/Receice.jsp"); //This is your logic
chain.doFilter(request, response);

那应该这样做。