为什么 servlet jsp 的 Servlet.service() 抛出异常 java.lang.NullPointerException?
Why Servlet.service() for servlet jsp threw exception java.lang.NullPointerException?
我想通过 servlet 将数据从 javascript 发送到 managedbean 但问题是第一次代码工作正常但是当我返回到包含 javascript 代码的原始 jsp 页面时并再次单击提交按钮以移动到 servlet,值显示在控制台中,但发生错误并且重定向到 jsp 页面失败。
JSP 页面包含 javascript:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=drawing,geometry"></script>
<style>
html, body
{
padding: 0;
margin: 0;
height: 100%;
}
.map{height:40%;}
</style>
<script>
function initialize()
{
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
);
}
alert("my function in jason");
var jsonString = getURLParameter("str");
// var strr = '{mapBean.str1}';
alert(jsonString);
var goo = google.maps,
map_in = new goo.Map(document.getElementById('map_in'),
{zoom: 12,
center: new goo.LatLng(32.344, 51.048)
}),
shapes = [],
selected_shape = null,
drawman = new goo.drawing.DrawingManager({map: map_in}),
byId = function(s) {
return document.getElementById(s)
},
clearSelection = function() {
if (selected_shape) {
selected_shape.set((selected_shape.type
===
google.maps.drawing.OverlayType.MARKER
) ? 'draggable' : 'editable', false);
selected_shape = null;
}
},
goo.event.addListener(drawman, 'overlaycomplete', function(e) {
var shape = e.overlay;
shape.type = e.type;
goo.event.addListener(shape, 'click', function() {
setSelection(this);
});
setSelection(shape);
shapes.push(shape);
});
goo.event.addListener(map_in, 'click', clearSelection);
goo.event.addDomListener(byId('clear_shapes'), 'click', clearShapes);
goo.event.addDomListener(byId('save_raw'), 'click', function() {
var data = IO.IN(shapes, false);
byId('data').value = JSON.stringify(data);
if (this.shapes) {
for (var i = 0; i < this.shapes.length; ++i) {
this.shapes[i].setMap(null);
}
}
for (var i = 0; i < shapes.length; ++i) {
shapes[i].setMap(null);
}
shapes = [];
alert("clear");
});
goo.event.addDomListener(byId('restore'), 'click', function() {
if (this.shapes) {
for (var i = 0; i < this.shapes.length; ++i) {
this.shapes[i].setMap(null);
}
}
// alert(jsonString);
this.shapes = IO.OUT(JSON.parse(byId('data').value), map_in);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<br/>
<div class="map" id="map_in"></div>
<div style="text-align:center">
<form method="get" action="GeofencingServlet">
<input id="clear_shapes" value="clear shapes" type="button" />
<!--<input id="save_encoded" value="save encoded(IO.IN(shapes,true))" type="button" />-->
<input id="save_raw" value="save raw(IO.IN(shapes,false))" type="button" />
<input id="data" name="data" value="" style="width:100%" readonly/>
<input type="submit" id="restore" value="restore(IO.OUT(array,map))" type="button" />
<!--<input type="submit" name="save" value="Create Zone!" id="save" />-->
</form>
</div>
</body>
</html>
GeofencingServlet:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<p>Zone created successfully.</p>");
out.println("<a href=\"gefencingJason.jsp\">Click here!</a>to go back to Map");
out.println("</body>");
out.println("</html>");
// String str = request.getParameter("vertices");
String str = request.getParameter("data");
System.out.println("str in servlet" + str);
MapBean mapBean = new MapBean();
String strret = mapBean.geo(str);
System.out.println("servlettttt string returned by mapbean" + strret);
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
context.redirect(context.getRequestContextPath() + "/gefencingJason.jsp?str=" + strret);
}
地图豆:
public String geo(String str) throws IOException {
System.out.println("The data in map bean is str" + str);
str1 = "my string";
return str;
}
我有以下错误:
WARNING: StandardWrapperValve[GeofencingServlet]: Servlet.service() for servlet GeofencingServlet threw exception
java.lang.NullPointerException
at servlet.GeofencingServlet.doGet(GeofencingServlet.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
INFO: str in servlet[{"type":"RECTANGLE","id":null,"geometry":[[32.33265644167479,50.932273864746094],[32.352090586480486,50.985145568847656]]}]
INFO: The data in map bean is str[{"type":"RECTANGLE","id":null,"geometry":[[32.33265644167479,50.932273864746094],[32.352090586480486,50.985145568847656]]}]
INFO: servlettttt string returned by mapbean[{"type":"RECTANGLE","id":null,"geometry":[[32.33265644167479,50.932273864746094],[32.352090586480486,50.985145568847656]]}]
参考this, FacesContext.getCurrentInstance() returns null.
您可以使用以下代码。
//ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//context.redirect(context.getRequestContextPath() + "/gefencingJason.jsp?str=" + strret);
response.sendRedirect(request.getContextPath() + "/gefencingJason.jsp?str=" + strret);
或者您可以参考@BalusC 的this文章。
永远记住:在 IDE.
中检查项目的目标运行时(服务器)
对于日食IDE:
转到项目的属性并查找 "Targeted Runtimes"
选项。检查所需的 运行时环境 。
我想通过 servlet 将数据从 javascript 发送到 managedbean 但问题是第一次代码工作正常但是当我返回到包含 javascript 代码的原始 jsp 页面时并再次单击提交按钮以移动到 servlet,值显示在控制台中,但发生错误并且重定向到 jsp 页面失败。
JSP 页面包含 javascript:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=drawing,geometry"></script>
<style>
html, body
{
padding: 0;
margin: 0;
height: 100%;
}
.map{height:40%;}
</style>
<script>
function initialize()
{
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
);
}
alert("my function in jason");
var jsonString = getURLParameter("str");
// var strr = '{mapBean.str1}';
alert(jsonString);
var goo = google.maps,
map_in = new goo.Map(document.getElementById('map_in'),
{zoom: 12,
center: new goo.LatLng(32.344, 51.048)
}),
shapes = [],
selected_shape = null,
drawman = new goo.drawing.DrawingManager({map: map_in}),
byId = function(s) {
return document.getElementById(s)
},
clearSelection = function() {
if (selected_shape) {
selected_shape.set((selected_shape.type
===
google.maps.drawing.OverlayType.MARKER
) ? 'draggable' : 'editable', false);
selected_shape = null;
}
},
goo.event.addListener(drawman, 'overlaycomplete', function(e) {
var shape = e.overlay;
shape.type = e.type;
goo.event.addListener(shape, 'click', function() {
setSelection(this);
});
setSelection(shape);
shapes.push(shape);
});
goo.event.addListener(map_in, 'click', clearSelection);
goo.event.addDomListener(byId('clear_shapes'), 'click', clearShapes);
goo.event.addDomListener(byId('save_raw'), 'click', function() {
var data = IO.IN(shapes, false);
byId('data').value = JSON.stringify(data);
if (this.shapes) {
for (var i = 0; i < this.shapes.length; ++i) {
this.shapes[i].setMap(null);
}
}
for (var i = 0; i < shapes.length; ++i) {
shapes[i].setMap(null);
}
shapes = [];
alert("clear");
});
goo.event.addDomListener(byId('restore'), 'click', function() {
if (this.shapes) {
for (var i = 0; i < this.shapes.length; ++i) {
this.shapes[i].setMap(null);
}
}
// alert(jsonString);
this.shapes = IO.OUT(JSON.parse(byId('data').value), map_in);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<br/>
<div class="map" id="map_in"></div>
<div style="text-align:center">
<form method="get" action="GeofencingServlet">
<input id="clear_shapes" value="clear shapes" type="button" />
<!--<input id="save_encoded" value="save encoded(IO.IN(shapes,true))" type="button" />-->
<input id="save_raw" value="save raw(IO.IN(shapes,false))" type="button" />
<input id="data" name="data" value="" style="width:100%" readonly/>
<input type="submit" id="restore" value="restore(IO.OUT(array,map))" type="button" />
<!--<input type="submit" name="save" value="Create Zone!" id="save" />-->
</form>
</div>
</body>
</html>
GeofencingServlet:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<p>Zone created successfully.</p>");
out.println("<a href=\"gefencingJason.jsp\">Click here!</a>to go back to Map");
out.println("</body>");
out.println("</html>");
// String str = request.getParameter("vertices");
String str = request.getParameter("data");
System.out.println("str in servlet" + str);
MapBean mapBean = new MapBean();
String strret = mapBean.geo(str);
System.out.println("servlettttt string returned by mapbean" + strret);
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
context.redirect(context.getRequestContextPath() + "/gefencingJason.jsp?str=" + strret);
}
地图豆:
public String geo(String str) throws IOException {
System.out.println("The data in map bean is str" + str);
str1 = "my string";
return str;
}
我有以下错误:
WARNING: StandardWrapperValve[GeofencingServlet]: Servlet.service() for servlet GeofencingServlet threw exception
java.lang.NullPointerException
at servlet.GeofencingServlet.doGet(GeofencingServlet.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
INFO: str in servlet[{"type":"RECTANGLE","id":null,"geometry":[[32.33265644167479,50.932273864746094],[32.352090586480486,50.985145568847656]]}]
INFO: The data in map bean is str[{"type":"RECTANGLE","id":null,"geometry":[[32.33265644167479,50.932273864746094],[32.352090586480486,50.985145568847656]]}]
INFO: servlettttt string returned by mapbean[{"type":"RECTANGLE","id":null,"geometry":[[32.33265644167479,50.932273864746094],[32.352090586480486,50.985145568847656]]}]
参考this, FacesContext.getCurrentInstance() returns null.
您可以使用以下代码。
//ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//context.redirect(context.getRequestContextPath() + "/gefencingJason.jsp?str=" + strret);
response.sendRedirect(request.getContextPath() + "/gefencingJason.jsp?str=" + strret);
或者您可以参考@BalusC 的this文章。
永远记住:在 IDE.
中检查项目的目标运行时(服务器)对于日食IDE:
转到项目的属性并查找 "Targeted Runtimes"
选项。检查所需的 运行时环境 。