1

I'm creating Restful web services for reps to login from UI and connect to Mysql Database through JDBC. using Dynamic web services -web module version 2.5, Apache Tomcat server 8.0 on windows 10. I wrote the Query in Java class and tring to connect to Database. when running the tomcat server.. its going to ui and when submit button clicked, next action is to connect to DB but its throwing:

Java.null.point.exception on UI and in Console its
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION **
java.io.EOFException

public class loginservlet6_1 extends HttpServlet {

    private static final long serialVersionUID = 1L;
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;

    public void init(ServletConfig config)
    {
        try
        {
            System.out.println("Database connectionestablishment");
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:1002/root","root","root");
    //(jdbc:mysql://localhost:portnum/dbname , username,password)
            System.out.println("Database conn established");
        }
        catch(Exception e)
            {
                System.out.println(e);
            }
    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try
        {
            st = con.createStatement();
                    rs = st.executeQuery("select UserName,Password from reps where UserName='"+username+"' and Password='"+password+"'");
            while(rs.next())
            {
                System.out.println("Username : "+rs.getString(1)+"  Password : "+rs.getString(2));

                if(username.equals(rs.getString(1)) && password.equals(rs.getString(2))){  
                JOptionPane.showMessageDialog(null,"Login Successful");
                response.sendRedirect("/login.html");
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Login Not Successful");
                    /*RequestDispatcher rd = request.getRequestDispatcher("/RegisterServlet");
                    rd.forward(request, response);
                    */
                    response.sendRedirect("/TestDemo6/login.html");
                }

            }


        }
        catch(Exception e){
            out.println(e);
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);

    }
    public void destroy()
    {
        try {
            rs.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
        try {
            st.close();
        } catch (SQLException e) {
                    e.printStackTrace();
                }
        try {con.close();
        } catch (SQLException e) {
                e.printStackTrace();
        }
    }
}
Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
krishna
  • 11
  • 1

0 Answers0