I want to develop an application which has communication between a servlet file running on Tomcat.
Below is the code in my application, which is trying to connection to this servlet, send request and get response.
private URLConnection getServletConnection() {
try {
URL servletURL = new URL("http://localhost:8080/test/servlet");
URLConnection conn = servletURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
return conn;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
And below is the code where exception is thrown:
URLConnection conn = getServletConnection();
OutputStream outputStream = conn.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
ServletRequestMessage srm = new ServletRequestMessage(2, username, password);
oos.writeObject(srm);
oos.flush();
oos.close();
InputStream inputStream = conn.getInputStream();
The exception is:
java.io.FileNotFoundException: http://localhost:8080/test/servlet
Can anyone help me? Thanks.
Server Side
Here I used
subtag, see:if("sub".equals(paramName)){.web.xml
Client Side
I used
DefaultHttpClientandHttpPost. I sendsubtag . Here is a method that sends data to Servlet:** Comment, before you start check connectivity, remove user/password from server side. If all works as expected switch it back and use on client side: