aspose file tools*
The moose likes Servlets and the fly likes Firewall Tunelling using Servlet Technology Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Firewall Tunelling using Servlet Technology" Watch "Firewall Tunelling using Servlet Technology" New topic
Author

Firewall Tunelling using Servlet Technology

kunal Jaggi
Greenhorn

Joined: Sep 16, 2003
Posts: 17
Hi!,
I want to connect my Java applications to Java servers through HTTP. To do this I have created servlets on the server side and wrap all client messages in HTTP requests.

On the client side, my code should use the URLConnection to send data to the server:



//connect
URL url = new URL("http://localhost:8080/servlet/myservlet");

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/octet-stream");

//Open output stream and send some data.
OutputStream out = conn.getOutputStream();
//I am not going to send anything on "out," but you can fill this in.

out.flush();
out.close();

//Open input stream and read the data back.
InputStream in = conn.getInputStream();

//Here you would read the data back.

in.close()

Everything is working fine. But, my servlet does dome dynamic content generation on client input, how do i send data as REQUEST PARAMETER to the servlet?

Regards,
Kunal Jaggi
Sun Certified Java Programmer
Maybach Smith
Greenhorn

Joined: Dec 22, 2004
Posts: 15
http://www.j-nine.com/pubs/applet2servlet/Applet2Servlet.html


Maybach
kunal Jaggi
Greenhorn

Joined: Sep 16, 2003
Posts: 17
Hi Smith,
Thanks 4 the post, but it still doesn't solve my query.
I want to send request parameter 2 a servlet.
I tried doing this:
BufferedOutputStream out=new BufferedOutputStream(conn.getOutputStream());
out.write("MKT___001".getBytes());
out.flush();
out.close();

But it throws 405 exception.
Regards,
Kunal Jaggi
SCJP
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

kunalnimc,

I just looked at the tutorial that Maybach sent to you.

The fourth section is "Applet-Servlet Communication with HTTP GET and POST"
and the fifth is "Communicating w/ Object Serialization".

I didn't read it line for line but a quick skim tells me that it should have everything you need to get your program working. Try reading it again and see if it answers your question.
[ January 22, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
kunal Jaggi
Greenhorn

Joined: Sep 16, 2003
Posts: 17
Hi!,
I have thought of one alternative to the problem.

Code Snippet:
URL url = new URL("http://localhost:8080/servlet/SalarySurveyServlet?emp_id=MKT_001");

Although this works but, i don't want to append the request parameter to the URL as a HTTP GET request.

Regards,
Kunal Jaggi
SCJP
kunal Jaggi
Greenhorn

Joined: Sep 16, 2003
Posts: 17
Hi! Bibeault,
Thanks 4 the post.
I have gone through the article.
What I suggested in my previous post is the GET method of sending output to the servlet.

Following is the code snippet from the article to incorporate the POST method:
// connect to the servlet
String location = "http://www.foo.com/servlet/TestServlet";
URL testServlet = new URL( servletLocation );
URLConnection servletConnection = testServlet.openConnection();

// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

// Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);

// Specify the content type that we will send binary data
servletConnection.setRequestProperty
("Content-Type", "<insert favorite mime type>");

// get input and output streams on servlet
. . .

// send your data to the servlet
. . .

But, I'm struck at how actually I would send request parameters as request/value pair?

Regards,
Kunal Jaggi
SCJP
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter17.pdf

Section 17.6 of this online book has the complete source to an applet that sends post data.
[ January 22, 2005: Message edited by: Ben Souther ]
Kunal Goel
Greenhorn

Joined: Sep 08, 2002
Posts: 16
I�m trying to POST data to a Servlet from a Java application and read back the response generated by the servlet. The servlet does a quick database query and returns the response.

Following is my servlet code:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

import java.sql.*;

public class HelloServlet extends HttpServlet {
private Connection con;
private Statement stmt;
private ResultSet rs;

public void init(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:tunnel");
}catch(Exception e){
System.err.println("Error-->"+e);
}
}
public void destroy(){
if(con!=null)
try{
con.close();
}catch(SQLException e){System.err.println("Error-->"+e);}
}
public void init(ServletConfig c) throws ServletException {
super.init (c);
}
public void doGet( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException {
doPost(request, response);
}
public void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<body bgcolor=\"white\">");
out.println("<h1>hello world servlet!</h1>");
String str=request.getParameter("param1");
out.println("The request parameter is --> "+str);
try{
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * FROM Employee");
while(rs.next()){
out.println(rs.getString(1));
}
}catch(SQLException e){
System.err.println("Error-->"+e);
}
out.println("</body>");
out.println("</html>");
}
}




Following is the Java application code:

import java.net.*;
import java.io.*;

public class HttpTunnel{
public static void main(String args[])throws Exception{
//connect
URL url = new URL("http://localhost/servlet/HelloServlet");

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);



//Open output stream and send some data.
//OutputStream out = conn.getOutputStream();
//I am not going to send anything on "out," but you can fill this in.

ByteArrayOutputStream byteStream =new ByteArrayOutputStream(512);
PrintWriter out = new PrintWriter(byteStream, true);
String val1 = URLEncoder.encode("Sun Micro");
String data = "param1=" + val1;// +"¶m2=" + val2; // Note �&�
out.print(data); // Note print, not println
out.flush(); // Necessary since no println used

conn.setRequestProperty("Content-Length", String.valueOf(byteStream.size()));
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

byteStream.writeTo(conn.getOutputStream());//send the real data

//out.flush();
//out.close();

//Open input stream and read the data back.
InputStream in = conn.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String str=br.readLine().trim();
while(str!=null){
System.out.println(str);
str=br.readLine().trim();
}
//Here you would read the data back.

br.close();

}
}


And this is the output that I get:
<html>
<body>
<body bgcolor="white">
<h1>hello world servlet!</h1>
The request parameter is --> Sun Micro
Scott
Michel
</body>
</html>
Exception in thread "main" java.lang.NullPointerException
at HttpTunnel.main(HttpTunnel.java:40)

Can somebody point out that what is causing the exceprion?

Regards,
Kunal Jaggi
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

While looking for the project code, I found the article that I followed to get things working:

http://www-128.ibm.com/developerworks/java/library/j-tunnel/
Kunal Goel
Greenhorn

Joined: Sep 08, 2002
Posts: 16
HI! Ben,
Thanks for the post.
I'm still struck. I have gone through the Code Snippet at the IBM Developer Works site. The only difference between my application and the code snippet on the IBM site is that I'm writing a byte of stream and the article discusses abt. writing a serializable object.

Anybody plz. help.

Regards,
Kunal Jaggi
SCJP
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Hint: What is a serialized object?
Kunal Goel
Greenhorn

Joined: Sep 08, 2002
Posts: 16
Hi Ben,
Thanks 4 ur post.
As far as my knowledge goes a Serializable object can be made persistent. It should implement the Serializable interface, which is a marker interface in the java.io package.

Could u plz. go through my code snippets and let me know that where exactly is the problem.

Regards,
Kunal Jaggi
SCJP
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



I can't be sure which is line 40 because you didn't post the line numbers.
I'm guessing my numbering is right.

You're calling a string method (trim) before testing for null.

Try this:


Or Better:


BTW: If you're going to ask others to debug your code for you (which you really shouldn't (you're cheating yourself out of learning an important skill)), you should try to present your code in a matter that makes it as easy as possible for them to read it.
There is a code button in this forum which, when used, preserves indenting.
Disable smileys so we can see what you actually typed instead of stupid cartoon faces.

Try to find a text editor that will allow you to paste the line numbers as well as the code. This is particularly helpful if you asking someone to figure out a error listed in a stack trace.

I tend to drop into this site, when I'm waiting for a script to run, or when I have a meeting in 15 minutes and don't want to get too deep into my own code, at the end of a lunch break before getting started on something, etc..
I assume this is the same for most people with jobs. The longer it takes for me to find "line 40" in page with 2 or 3 full classes of code + a stacktrace, the less likely I am to bother.
[ February 05, 2005: Message edited by: Ben Souther ]
Kunal Goel
Greenhorn

Joined: Sep 08, 2002
Posts: 16
Hi Ben,
Thanks for that. In feature I'll take care of all that.
There a lot 2 learn here.

Regards,
Kunal Jaggi
SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Firewall Tunelling using Servlet Technology