Advertisement

12.27.2007 at 06:18AM PST, ID: 23044729
[x]
Attachment Details

Java, client and server communication via URLConnection (HTTP Post)

Asked by cvaguilar in Java Programming Language, Apache Web Server

Tags: urlconnection, java

I am coding a JSP (process.jsp) containing scriplets to accept an request in xml format (via http post) from another server.  I am currently simulating the client side by using jsp also (sendrequest.jsp).  Please see my codes attached to this.

Problem is, the xml data that I am supposed to send to process.jsp is duplicated.  Thus, causing the failure of processing.  I suspected that it is due to getting the input stream (under sendrequest.jsp).  

I tried removing that part but another problem arises, the java methods that I call on the server side is not invoked anymore. :(

Also, I noticed that when I modify my jsp pages, it uses the cached one in tomcat (tomcat_home/work/localhost...)  I have to remove it first to ensure that the changes I made is recognized.  How do I ensure that tomcat will get the new jsp when there's a change?

Any ideas???Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
sendrequest.jsp (client)
 
<%@ page import="java.io.*,
                 java.util.*,
                 java.net.*,
                 java.net.HttpURLConnection.*"%>
 
 
<%
    String xmlData = "<?xml version=\"1.0\"?>"  +
	"<request application=\"test\">"  +
	"<target id=\"66819205613\"/>"  +
	"<service action=\"delete\" page=\"ABC|1|0\">"  +
	"ABC|1|0"  +
	"</service>"  +
	"<service action=\"add\" page=\"ABC|1|0\" label=\"Favorites\">"  +
	"ABC|1|0"  +
	"</service>"  +
	"</request>";
	
	// Create the URL and URL Connection
	java.net.URL programUrl = new java.net.URL("http://111.111.111.12:1980/Testing/process.jsp" );
	java.net.HttpURLConnection connection = (java.net.HttpURLConnection)programUrl.openConnection();
			
	((HttpURLConnection)connection).setRequestMethod("POST");
	connection.setDoOutput(true);
	//connection.setDoInput(true); //Only if you expect to read a response...
	connection.setUseCaches(false); //Highly recommended...
	connection.setRequestProperty("Content-Type", "text/xml");
	
	PrintWriter output = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));
	output.println(xmlData);
	//output.flush();
	output.close();	
 
	connection.connect();
	InputStream is = connection.getInputStream();
	InputStreamReader isr = new InputStreamReader(is);
	BufferedReader br = new BufferedReader(isr);
 
	String line = null;
 
	while ((line = br.readLine()) != null) {
   		out.println("TESTING");
	}
	
%>
 
 
process.jsp (server)
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@ page import="java.io.*, java.util.*"%>                 
                 
<%@ page import="com.test.interface.Beans"%>
<%
 
try{
	BufferedReader in = new BufferedReader(request.getReader());
	String inputLine;
	String xmlData = "";
 
	while ((inputLine = in.readLine()) != null) {     
		MyBean.get().processRequest(inputLine.concat(inputLine.trim())); 
	}
	
	
} catch (Exception e){
	PrintWriter responseWriter = response.getWriter();
	responseWriter.println("bad");
}
%>
[+][-]12.28.2007 at 03:09PM PST, ID: 20545964

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.28.2007 at 06:28PM PST, ID: 20546542

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.30.2007 at 09:00AM PST, ID: 20551310

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, Apache Web Server
Tags: urlconnection, java
Sign Up Now!
Solution Provided By: ysnky
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628