代码:
public class transferOptionServlet extends HttpServlet
{
public void init(ServletConfig config) throws ServletException {
super.init(config);
context = config.getServletContext();
...
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
...
context.getRequestDispatcher( "TransferServlet ").forward(request,response);//报错
}
}
报错信息:(日志文件里面得报错信息)
<[ServletContext(id=26801639,name=/Winds,context-path=/Winds)] Servlet failed with Exception
java.lang.NullPointerException
at transferOptionServlet.doGet(transferOptionServlet.java:122)
at transferOptionServlet.doPost(transferOptionServlet.java:63)
那位高手能解答一下.非常感谢了!!!在线等
------解决方案--------------------
IOException捕获否?包导入否?路径对否?
------解决方案--------------------
这个是取不到TransferServlet的值,也就是说TransferServlet传进去的时候是空的
------解决方案--------------------
如果想用请求转发,是否该这样使用?
request.getRequestDispatcher( "servlet1 ").forward(request,response);
------解决方案--------------------
TransferServlet里面的错误
------解决方案--------------------
你加了DOPOST()方法了吗 DOGET和DOPOST2个要一起出现的 不然你换成Service
------解决方案--------------------
你要把请求转发道servlet,看看你的url映射:
<servlet-mapping>
<servlet-name> TransferServlet </servlet-name>
<url-pattern> /servlet/transferServlet </url-pattern>
</servlet-mapping>
之所以报NullPointerException,那说明你这个dispatch转发器指向的引用是空的,为什么:
context.getRequestDispatcher( "TransferServlet ").forward(request,response);//报错
context.getRequestDispatcher( "path ")的参数格式不对。应该是 "/ "开头。
你要把请求转发给 <servlet-name> TransferServlet </servlet-name>
你只见诶写 context.getRequestDispatcher( "/TransferServlet ").当然也不对,因为它的url映射不是这样。所以报找不到相关资源
正确的写法:
context.getRequestDispatcher( "/servlet/transferServlet ");
------解决方案--------------------
代码:
public class transferOptionServlet extends HttpServlet
{
private String toPage= "index.jsp ";
public void init(ServletConfig config) throws ServletException {
super.init(config);
context = config.getServletContext();
toPage = config.getInitParameter( "toPage ");
}
然后配再配置个init-value参数就可以了
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
...
request.getRequestDispatcher(toPage).forward(request,response);//报错
}
}