hi folks,

Here i face problem in my webpage [servlet + mysql] Connection are working fine. But when running time it get download the page instead of display the content in the browser.

I try myself so many time, cant sove it.. plz guys help me to overcome this problem.

My Code :

package dbPackage;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DataConnectionServlet  extends HttpServlet {

	private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest inRequest,
      HttpServletResponse outResponse) throws ServletException,
      IOException {

    PrintWriter out = null;
    Connection connection = null;
    Statement statement;
    ResultSet rs;

    try {
      Class.forName("com.mysql.jdbc.Driver");

      connection = DriverManager
          .getConnection("jdbc:mysql://localhost/siva","root","root");
      statement = connection.createStatement();

      outResponse.setContentType("test/html");
      out = outResponse.getWriter();

      rs = statement.executeQuery("SELECT * FROM log");

      out.println("<HTML><HEAD><TITLE>Products</TITLE></HEAD>");
      out.println("<BODY>");
      out.println("<UL>");

      while (rs.next()) {
        out.println("<LI>" + rs.getString("id") + " "
            + rs.getString("uname") + " " + rs.getString("pwd"));
      }

      out.println("</UL>");
      out.println("</BODY></HTML>");
    } catch (ClassNotFoundException e) {
     
    } catch (SQLException e) {
      out.println("SQLException: " + e.getMessage());
    }
  }  
}

Run the query (SELECT * FROM log) from a different method. Create a custom object, run the query, put the results inside the object and have that method return a list with the data.
Call that method form the servlet.

The servlet will have only that call. Once you get the list, put it into the request and send it to the page you want it displayed. And I believe that, that is the problem. In the servlet, you don't specify where you want to display the data. You should declare the jsp where the out.print will print.

Also never put html code in a servlet. Send the data to a jsp page and use html tags for displaying.

In general delete everything you have written. Try to find a book about JSPs and learn HTML. Use objects and separate methods.

You must be able to know very good java before going to JSPs. Meaning that should know how to use objects and most important you should close the Connection and everything else in a finally block, which you don't.

public void doGet(HttpServletRequest inRequest,
HttpServletResponse outResponse) throws ServletException,
IOException {

  try {
     ArrayList<CustomObject> list = .... call a method from another class

     // put the list in the request
     // send it to a jsp
  } catch (Exception e) {
     // put the error in the request
     // send it to a jsp
  }

}

Run the query (SELECT * FROM log) from a different method. Create a custom object, run the query, put the results inside the object and have that method return a list with the data.
Call that method form the servlet.

The servlet will have only that call. Once you get the list, put it into the request and send it to the page you want it displayed. And I believe that, that is the problem. In the servlet, you don't specify where you want to display the data. You should declare the jsp where the out.print will print.

Also never put html code in a servlet. Send the data to a jsp page and use html tags for displaying.

In general delete everything you have written. Try to find a book about JSPs and learn HTML. Use objects and separate methods.

You must be able to know very good java before going to JSPs. Meaning that should know how to use objects and most important you should close the Connection and everything else in a finally block, which you don't.

public void doGet(HttpServletRequest inRequest,
HttpServletResponse outResponse) throws ServletException,
IOException {

  try {
     ArrayList<CustomObject> list = .... call a method from another class

     // put the list in the request
     // send it to a jsp
  } catch (Exception e) {
     // put the error in the request
     // send it to a jsp
  }

}

Thankyou, Actually i am newbie to servlet. I know little things, can u explain with example. And small request, I like to contact with u, if expert person guide me means i ll learn quickly. PM me ur mail id plz

From what I see you know some java. How to run queries. Just put that code to a separate method in another class. You already have written the code. You don't need to write anything else. Just take that code put it in another method and have it return the list with the data. You should know how to create custom objects with attributes. And how to use lists (ArrayList, Vector).


Also there are plenty examples and books to help you. Simply copying some code I might post here without having the background to understand it will not help you learn.

And I cannot become an online tutor. You can easily read any book and then post your questions here.

From what I see you know some java. How to run queries. Just put that code to a separate method in another class. You already have written the code. You don't need to write anything else. Just take that code put it in another method and have it return the list with the data. You should know how to create custom objects with attributes. And how to use lists (ArrayList, Vector).


Also there are plenty examples and books to help you. Simply copying some code I might post here without having the background to understand it will not help you learn.

And I cannot become an online tutor. You can easily read any book and then post your questions here.

yeah, i ll try and see.. thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.