Reputation: 77
I have 2 web applications, GPSClient and RuralCloud, deployed on my tomcat server in which I have 2 servlets SendData and RecieveData respectively. I want to send object from SendData to RecieveData. I am using requestDispature but its not working for me.
This is code for servlet one which is thwoing NullPointerException
@WebServlet("/SendData")
public class SendData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SendData() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
String userID = request.getParameter("userID");
String latitude = request.getParameter("latitude");
String longitude = request.getParameter("longitude");
GPSData d = new GPSData(userID, Double.parseDouble(latitude),
Double.parseDouble(longitude));
request.setAttribute("data", d);
ServletContext context = getServletContext().getContext("/RuralCloud");
RequestDispatcher rd = context.getRequestDispatcher("/RecieveData");
rd.forward(request, response);
}
}
This is error log
java.lang.NullPointerException
at com.gps.client.SendData.doPost(SendData.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Is there any way to solve the problem. Thanx in advance.
Upvotes: -1
Views: 1460
Reputation: 48057
You'll have to keep in mind that two webapplications use totally separated classloaders and you can't just access each other's objects in the various application contexts - assume they have nothing in common.
This brings you to communicating between the two applications through http - indeed I'd very much prefer that technique. Yes, there's more that you can do on the server level, but their explanation would be far beyond the scope of a single answer here.
Related to your question, the stacktrace points to a NullpointerException at com.gps.client.SendData.doPost(SendData.java:60)
, but you don't give us a clue what that line is. It's probably one of the last two lines in that method and it won't help if you add this information now (as the approach is not correct), but typically you want to add those pointers to your question to get more (and quicker) help. The NPE
's location might also point you to the assumption that was wrong...
So, in general, go through any other mechanism (e.g. http, as @Szarpul suggests). I hope to have pointed to the reasons why your approach doesn't really work well.
Upvotes: 1