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