我想通过servlet将数据从javascript发送到managedbean,但问题是第一次代码工作正常,但是当我返回到包含javascript代码的原始jsp页面并再次单击submit按钮移动到servlet时,显示的值在控制台中但发生错误并重定向到jsp页面失败。
包含javascript的JSP页面:
<%@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);
}
MapBean:
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$9.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.access$100(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]]}]
答案 0 :(得分:0)
在引用this时,FacesContext.getCurrentInstance()返回null。
您可以使用以下代码。
//ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//context.redirect(context.getRequestContextPath() + "/gefencingJason.jsp?str=" + strret);
response.sendRedirect(request.getContextPath() + "/gefencingJason.jsp?str=" + strret);
或者你可以参考@BalusC的this文章。
答案 1 :(得分:0)
永远记住:在IDE中检查项目的目标运行时(服务器)。
对于eclipse IDE :
转到项目的属性并查找"Targeted Runtimes"
选项。检查所需的运行时环境。