You are viewing a plain text version of this content. The canonical link for it is here.
Posted to regexp-dev@jakarta.apache.org by Rajesh Purohit <ra...@yahoo.com> on 2000/12/21 09:53:20 UTC
1 PROBLEM executing servlet using tomcat
hello there,
> i m using jakarta-tocat.. on my windows NT platform with personal peer web
> server version 4.0..
> with service pack 6.0..
>
> The problem is ... whenever i try to execute a servlet that returns a HTML
> script back to the browser while storing some data into the database...
The
> data is stored into the database but the html codes to show the messages
on
> the client side doen't work..
> But the sample codes for servlets with the tomcat appln runs very well..
>
> i'm attaching the java source code with this mail..
code is modified.. there may be some syntex error now.. but while executing
it was not as this...
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
//import java.util.*;
import java.net.*;
import java.sql.*;
public class addr_serv extends HttpServlet
{
//Initialize global variables
Connection con;
Statement stmt;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:sample");
stmt = con.createStatement();
}
catch(ClassNotFoundException abc)
{
System.out.println("class not found");
}
catch(SQLException sqle)
{
System.out.println("due to connection error"+sqle);
}
}//end init()
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
if (request.getParameter("code").equals("display"))
{
display(request, response);
}
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("<html>");
out.println("<head><title>ItemDetail_servlet</title></head>");
out.println("<body>");
out.println("</body></html>");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
try
{
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
ObjectInputStream ois = new
ObjectInputStream(request.getInputStream());
String str = "insert into addr values(?,?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(str);
// these string variables firstName, lastName comes from the html form.....
pst.setString(1, firstName);
pst.setString(2, lastName);
pst.setString(3, email);
pst.setDouble(4, phone1);
pst.setString(5, "ngp");
pst.setDouble(6, 440009);
pst.executeUpdate();
/*
// DATA IS STORED IN THE DATABASE BUT
// THE HTML TAGS.. WHICH I WANT TO SEND BACK TO CLIENT.. OR BROWSER
// IS NOT PRINTED ... BUT THE System.out.println(); works very well it
writes data back to
// the console where tomcat is running
// ALSO THE EXAMPLES GIVEN WITH JAKARTA-TOCAT PACKAGE RUNS VERY FINE
*/
item_obj.set_msg("ok");
ObjectOutputStream oos = new
ObjectOutputStream(response.getOutputStream());
oos.writeObject(item_obj);
oos.close();
oos.flush();
out.close();
}
catch(Exception ae)
{
System.out.println(ae + ae.getMessage());
}
}
}
REGARDS,
RAJESH PUROHIT
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com