[英]NullPointerException with Servlet
I am calling a Servlet
using its URL address. 我正在使用其URL地址调用Servlet
。 This is the URL I am typing 这是我输入的URL
http://localhost:7001/ryan/olympics?action=selectCatalog&id=1
This is the Servlet's URL for sure; 这肯定是Servlet的URL; if I change the address I get 如果我改变我得到的地址
page not found
This is the code for the Servlet. 这是Servlet的代码。
public class Servlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
private static final long serialVersionUID = 1L;
public Servlet() {
super();
}
public void init(ServletConfig config) throws ServletException {
System.out.println("*** initializing controller servlet.");
super.init(config);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("selectCatalog")) {
String categoryId = request.getParameter("id");
ProductModelDAO dao4 = new ProductModelDAOImpl("jpac");
if (categoryId != null && !categoryId.trim().equals("")) {
CategoryDAO dao1 = new CategoryDAOImpl("jpac");
try {
Category category = dao1.getCategoryName(categoryId);
request.setAttribute("category", category);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
@SuppressWarnings("unchecked")
List<Product> products = dao4
.getProductsByCategory(categoryId);
request.setAttribute("products", products);
} catch (Exception e) {
e.printStackTrace();
}
url = "SelectCatalog.jsp";
RequestDispatcher requestDispatcher =
getServletContext().getRequestDispatcher(url);
requestDispatcher.forward(request, response);
I get the NullPointerException
pointing to the RequestDispatcher
's line. 我得到NullPointerException
指向RequestDispatcher
的行。 Any help? 有帮助吗?
尝试将"SelectCatalog.jsp"
更改为"/SelectCatalog.jsp"
,因为据我所知,您希望使用absolute path
。
If you want to use a relative path you have to use: 如果要使用必须使用的相对路径:
request.getRequestDispatcher(url);
in place of: 取代:
getServletContext().getRequestDispatcher(url);
request.getParameter("action"); 用request.getParameter( “动作”);
code is written in doPost method. 代码是用doPost方法编写的。 Are you invoking this servlet from doPost method of calling servlet? 您是否从调用servlet的doPost方法调用此servlet? URL parameters will not be used by doPost method. doPost方法不会使用URL参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.