Servlet无法正常工作

时间:2015-07-09 06:33:23

标签: java

我正在尝试从mysql中获取数据并使用servlet在浏览器中显示它。下面是我的servlet代码。但是当我尝试执行给定的代码时,浏览器会打开,但不会显示任何内容。我尝试更改数据库,表但没有任何对我有用。 Netbeans也不会给出任何错误

import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Login extends HttpServlet {
       @Override
       public void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException
       {


              response.setContentType("text/html");
              PrintWriter out = response.getWriter();        


              Connection con;
              try{

                     Class.forName("com.mysql.jdbc.Driver");
                     con=DriverManager.getConnection("jdbc:mysql://localhost:3306/servlet","root","");               
                     PreparedStatement ps=con.prepareStatement("select * from user");
                     if (con!=null)
                     {

                         out.println("connected");
                  }
                     else{

                         out.println("disconnected");
                     }

                     ResultSet rs=ps.executeQuery();                
                     /* Printing column names */

                     while(rs.next())
                        {
                            out.println(rs.getString(1));
                            out.println(rs.getString(2));
                        }


              }catch (ClassNotFoundException | SQLException e2)
                {

                }

              finally{out.close();
                }
       }

} 

1 个答案:

答案 0 :(得分:0)

在方法结束时有以下行,即。在out.close();

之前
out.flush();