当前位置: 代码迷 >> Java Web开发 >> 问一个servlet 请求转发有关问题
  详细解决方案

问一个servlet 请求转发有关问题

热度:82   发布时间:2016-04-12 23:19:23.0
问一个servlet 请求转发问题
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Test extends HttpServlet {


public void  doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionIOException {
   
//System.out.println("a");
//return null;
test(request,response);
}

public String test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    response.setContentType("text/html; charset=gb2312"); 
ServletContext sc = getServletContext(); 
RequestDispatcher rd = null; 
rd=sc.getRequestDispatcher("/index.jsp");
rd.forward(request,response);
return null;
}

}


抛异常了

at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at Test.test(Test.java:27)
at Test.doGet(Test.java:19)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

------解决方案--------------------
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp"); 
rd.forward(request, response);
试试
  相关解决方案