我再编写,一个Servlet的时候,出现一个问题是:无法讲 javax.servlet.Requestspatcher 中的 forward(javax.servlet.ServletRequest,javax,servlet,ServletResponse)应用于(javax.servlet.HtttpServletRequest,javax,servlet,HttpServletResponse)代码如下:package mypack;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.*;
public class DispatcherServlet extends HttpServlet{ private String target="/hello.jsp";
public void init(ServletConfig config)
throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServlet response)
throws ServletException,IOException
{
String username=request.getParameter("username");
String password=request.getParameter("password");
request.setAttribute("USER",username);
request.setAttribute("PASSWORD",password);
RedirectionServletContext context=getServletContext();
RequestDispatcher dispatcher=context.getRequestDispatcher(target);
dispatcher.forward(request,response);
}
public void destroy()
{
}
}顺便大家讲一下 RedirectionServletContext context=getServletContext();
RequestDispatcher dispatcher=context.getRequestDispatcher(target);
dispatcher.forward(request,response);的作用是什么,谢谢
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.*;
public class DispatcherServlet extends HttpServlet{ private String target="/hello.jsp";
public void init(ServletConfig config)
throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServlet response)
throws ServletException,IOException
{
String username=request.getParameter("username");
String password=request.getParameter("password");
request.setAttribute("USER",username);
request.setAttribute("PASSWORD",password);
RedirectionServletContext context=getServletContext();
RequestDispatcher dispatcher=context.getRequestDispatcher(target);
dispatcher.forward(request,response);
}
public void destroy()
{
}
}顺便大家讲一下 RedirectionServletContext context=getServletContext();
RequestDispatcher dispatcher=context.getRequestDispatcher(target);
dispatcher.forward(request,response);的作用是什么,谢谢
应该是ServletContext 吧
其实你要从servlet跳转到jsp客户端,我觉得应该这样写
RequestDispatcher dispatcher=request.getRequestDispatcher(target);
dispatcher.forward(request,response);