将变量值从JSP传递给Servlet

时间:2016-03-28 16:53:14

标签: java jsp servlets

我将在JSP页面中获得的变量值传递给servlet页面。我用了会话。但它提供了以下NullPointerException

java.lang.NullPointerException
    com.google.api.client.http.GenericUrl.appendParam(GenericUrl.java:599)
    com.google.api.client.http.GenericUrl.addQueryParams(GenericUrl.java:582)
    com.google.api.client.http.UriTemplate.expand(UriTemplate.java:346)
    com.google.api.client.http.UriTemplate.expand(UriTemplate.java:259)
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequestUrl(AbstractGoogleClientRequest.java:266)
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:301)
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    com.translator.api.TranslatorApi.getTranslate(TranslatorApi.java:66)
    control.TextTranslation.doPost(TextTranslation.java:47)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

imageExtract.jsp

</div>
        <div class="row">
            <div  id = "display" class="col-lg-8 center-block ">
            <%
            String extracted = (String) request.getAttribute("extractedText");
            session.setAttribute("text", extracted);

            %>
                <textarea maxlength="200" class=" form-control " name="msg" rows="20" id="Extract-textarea" ><%=extracted %> </textarea>

            </div>
    </div>

TextTranslate.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    String language = "ES";
    HttpSession extractedText = request.getSession();
    String imageName = (String) extractedText.getAttribute("extractedText");
    System.out.println("this is so stupid");
    System.out.println("This is "+imageName);
    TranslatorApi translation = new TranslatorApi();

    String outputText =   translation.getTranslate(imageName, language);
    System.out.println("Second"+outputText);
    request.setAttribute("outputText", outputText);
    RequestDispatcher sndValue = request.getRequestDispatcher("/translation.jsp");
    sndValue.forward(request, response);

}

问题是会话值没有传递给servlet,后者给出NullpointerException。为什么会发生这种情况?

2 个答案:

答案 0 :(得分:2)

正在设置的会话变量的名称只是&#34; text&#34;,但您正在尝试访问&#34; extractedText&#34;代码中的属性。 &#34;文字&#34;属性根本没有被访问,只是设置。

上面的代码中似乎没有任何内容可以在会话中设置extractedText属性。因此,它只是通过imageName变量将null返回到您的Google API,然后当然失败。

答案 1 :(得分:0)

您只能获取已设置的属性值,您已设置outputText的属性,因此您需要获取该属性值。

request.setAttribute("outputText", outputText);

更改代码以更正getAttribute

    <div class="row">
        <div  id = "display" class="col-lg-8 center-block ">
        <%
        String extracted = (String) request.getAttribute("outputText");
        session.setAttribute("text", extracted);

        %>
            <textarea maxlength="200" class=" form-control " name="msg" rows="20" id="Extract-textarea" ><%=extracted %> </textarea>

        </div>
</div>

使用JSTL是标准,有助于清理您的HTML代码..

使用jstl

您的代码,这样您就可以看到使用jstl的简单程度。

    <div class="row">
        <div  id = "display" class="col-lg-8 center-block ">
          <textarea maxlength="200" class=" form-control " name="msg" rows="20" id="Extract-textarea" >
            <c:out value="${outputText}"> 
          </textarea>
        </div>
     </div>

Simple tutorial for using and setting up jstl