将列表从Servlet传递到Jsp,从而给出空指针异常 [英] Passing a list from Servlet to Jsp giving null pointer exception

查看:303
本文介绍了将列表从Servlet传递到Jsp,从而给出空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印问题列表(questionList).加载表单时,列表中的元素应打印为标签.

i am trying to print a list of questions(questionList). When the form loads the elements of the list should be printed as labels.

在我的java文件中,我返回一个问题列表.

In my java file i am returning a list of questions.

在我的servlet代码中:

In my servlet code:

ArrayList<String> questionList = qd.getFormLabels();
request.setAttribute("question", questionList);
System.out.println("Hello World " + questionList);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Home.Index.jsp");
if (rd != null){    
rd.forward(request, response);
return;

在我的jsp代码中,当我访问此列表时,它给了我一个空指针异常.

In my jsp code when i access this list it gives me a null pointer exception.

jsp的代码:

<%

ArrayList<String> LabelList = (ArrayList)request.getAttribute("question");
out.println(LabelList.size());

if(LabelList.isEmpty()==false)
    {
        for( int i = 0; i< LabelList.size(); i++)
        {%>
            <tr><td><%out.println(LabelList.get(i)); %></td></tr>

当我尝试打印尺寸时,它会给出null. 请告诉我我的流程是否正确以及servlet是否已正确触发

when i try to print the size it gives null. Please tell whether my flow is right and the servlet is triggered properly

推荐答案

只有当您不通过其URL调用servlet时,这种情况才会发生.基于这些注释,您似乎实际上是直接通过其URL调用JSP,并且似乎不了解servlet映射.

That can only happen if you are not invoking the servlet by its URL. Based on the comments you seem to be actually invoking the JSP directly by its URL and you seem not to be understanding servlet mappings.

好吧,为了让servlet在JSP中显示结果之前能够预处理HTTP请求,您需要将servlet映射到web.xml 中的URL模式上,并且将JSP隐藏起来在/WEB-INF文件夹中,这样就永远不能直接调用它,也不会偶然调用它,因此您始终可以确保仅通过调用servlet才能显示JSP.

Well, in order to get a servlet to preprocess a HTTP request before displaying the results in a JSP, you need to map the servlet on an URL pattern in web.xml and hide the JSP away in /WEB-INF folder so that it can never be called directly, also not by accident, and thus you can always ensure that the JSP can only displayed by invoking the servlet.

例如,在web.xml中:

<servlet>
    <servlet-name>home</servlet-name>
    <servlet-class>com.example.controller.HomeServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>home</servlet-name>
    <url-pattern>/home</url-pattern>
</servlet-mapping>

请注意/home<url-pattern>.这要注意,可以通过 http://localhost:8080/contextname/home 来调用servlet. "/contextname"实际上是您的Web应用的上下文名称.当您在浏览器地址栏中输入该URL或单击指向该URL的链接/书签时,将调用servlet的doGet()方法.您只需要更改这些RequestDispatcher

Please note the <url-pattern> of /home. This takes care that the servlet can be invoked by http://localhost:8080/contextname/home where "/contextname" is actually your webapp's context name. When you enter that URL in the browser address bar or click on a link/bookmark pointing to that URL, the servlet's doGet() method will be called. You only need to change those RequestDispatcher lines

RequestDispatcher rd = getServletContext().getRequestDispatcher("/Home.Index.jsp");
if (rd != null){    
rd.forward(request, response);

采用以下更简单,更规范的形式

to the following simpler and more canonical form

request.getRequestDispatcher("/WEB-INF/home.jsp").forward(request, response);

您还需要将JSP文件移动到该路径上的/WEB-INF文件夹中.为了清楚起见,我将其重命名为JSP文件名. Home.Index.jsp是一个很奇怪的名称.只要它位于/WEB-INF文件夹中,就可以随时将其重命名.

You also need to move the JSP file to /WEB-INF folder on exactly that path. I've for the sake of clarity renamed the JSP file name. Home.Index.jsp is a rather strange name. You can always rename it back if you want, as long as it's inside the /WEB-INF folder.

  • Our Servlets wiki page - Explains how servlets work and contains concrete Hello World examples

这篇关于将列表从Servlet传递到Jsp,从而给出空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆