简体   繁体   English

Servlet无法正常工作

[英]Servlet not working properly

I am trying to fetch data from the mysql and display it in the browser using servlet. 我正在尝试从mysql获取数据,并使用servlet在浏览器中显示它。 Below is my servlet code. 下面是我的servlet代码。 But when I tried to execute the given code the browser opens up but doesnot display anything. 但是,当我尝试执行给定的代码时,浏览器将打开,但不显示任何内容。 I tried changing database, table but nothing worked for me. 我尝试更改数据库,表,但没有任何帮助。 Netbeans also doesnot give any error 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();
                }
       }

} 

Have following line at the end of the method ie. 在方法的末尾有以下一行。 before out.close(); 在out.close()之前;

out.flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM