我將在JSP頁面中獲得的變量值傳遞給servlet頁面。我使用會話。但它給出以下NullPointerException
。將變量值從JSP傳遞到Servlet
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
。爲什麼會發生這種情況?
哪個值的問題? 'text'或'extractedText'? – brso05
請避免在JSP中使用普通的java。 JSTL在它內部替換Java方面做得很好。 –
您正在設置一個名爲'text'的會話屬性,並*獲得一個名爲'extractedText'的會話屬性。這就是爲什麼它是'空'。 –