servlet报错这是servlet的类Java code@Overrideprotected void doGet(HttpServletRequest req, HttpServle
servlet报错
这是servlet的类
Java code@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doGet(req, resp); /** * *@TradeOperator */ TradeOperator to = new TradeOperator(); ArrayList<Trade> list = to.getTradequery(); for (Trade trade : list) {// req.setAttribute("list", list); req.setAttribute("TRADE_NAME", trade.getName()); } req.getRequestDispatcher("test.jsp").forward(req, resp); }
jsp页面
Java code<body> ${Request.getAttribute("TRADE_NAME") } </body>
帮我看看,现在为什么报错
严重: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /test.jsp(11,4) The function getAttribute must be used with a prefix when a default namespace is not specified
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
at org.apache.jasper.compiler.Validator$ValidateVisitor$1FVVisitor.visit(Validator.java:1506)
at org.apache.jasper.compiler.ELNode$Function.accept(ELNode.java:129)
at org.apache.jasper.compiler.ELNode$Nodes.visit(ELNode.java:200)
at org.apache.jasper.compiler.ELNode$Visitor.visit(ELNode.java:242)
at org.apache.jasper.compiler.ELNode$Root.accept(ELNode.java:56)
at org.apache.jasper.compiler.ELNode$Nodes.visit(ELNode.java:200)
at org.apache.jasper.compiler.Validator$ValidateVisitor.validateFunctions(Validator.java:1531)
at org.apache.jasper.compiler.Validator$ValidateVisitor.prepareExpression(Validator.java:1536)
at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:720)
at org.apache.jasper.compiler.Node$ELExpression.accept(Node.java:958)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1763)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
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)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
[解决办法]
for (Trade trade : list) {
// req.setAttribute("list", list);
req.setAttribute("TRADE_NAME", trade.getName());
}
换成一下 :
如果多个 Trade的话 ,也只是显示最后一个 还不如这样子写
if (list.size() > 0){
req.setAttribute("TRADE_NAME", list.get(list.size()-1).getName());//显示最后一个
// req.setAttribute("TRADE_NAME", list.get(0).getName());//显示第一个
}else{
req.setAttribute("TRADE_NAME", "");
}
[解决办法]
Java codeArrayList<Trade> list = to.getTradequery(); for (Trade trade : list) {// req.setAttribute("list", list); req.setAttribute("TRADE_NAME", trade.getName()); } req.getRequestDispatcher("test.jsp").forward(req, resp);显示全部内容 不应该像上面所写 ,ArrayList<Trade> list = to.getTradequery();req.setAttribute("list", list);req.getRequestDispatcher("test.jsp").forward(req, resp);test.jsp需要import Trade类到jsp中<body> <% List<Trade> list = (ArrayList<Trade>)request.getAttribute("list") ; if (list.size() > 0){ Trade trade = null; for (int i = 0; i < list.size(); i++){ trade = list.get(i) ; %> <%= trade.toString()%><br /> <% } } else{ %> <br /><h1> 没有数据</h1> <% } %> for() </body>
[解决办法]
第一,你把doget换成dopost.
第二,你用forword进行跳转。不要用req.getRequestDispatcher("test.jsp").forward(req, resp);
[解决办法]
我觉得你有两个问题,首先你servlet页面
for (Trade trade : list) {
// req.setAttribute("list", list);
req.setAttribute("TRADE_NAME", trade.getName());
}
本身就有逻辑问题:
你因该把整个list存进req,根本没必要在Servlet中用循环,你不应该使用循环遍历list集合,每次都存刚遍历出来的trade的那一个name,到最后只有一个tradename。
jsp页面你可能根本就没加使用EL表达式的标签,所以才报了这样的错,而且即使要用EL表达式,这里也应该是遍历list集合的for each标签在前,el表达式位于整个for each标签中才可以。
我的异常网推荐解决方案:Servlet.service() for servlet default threw exception,http://www.myexception.cn/eclipse/181756.html