1

I have written a servlet program for login page ..am using tomcat server...after i run on the server am getting the above mentioned error... Below is my servlet code.

package demo;

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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */

public class LoginServlet extends HttpServlet {

    static private String dbUrl="jdbc:mysql://localhost:3306/employee";
    static private String dbUn="root";
    static private String dbPwd="root";
    static private Connection ConObj;
    static private Statement StmtObj;
    static private ResultSet RsObj; 


  public void service(HttpServletRequest request,HttpServletResponse response)throws IOException


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

      ConObj=DriverManager.getConnection(dbUrl, dbUn, dbPwd);
      StmtObj=ConObj.createStatement();
      response.setContentType("text/html");
      PrintWriter out=response.getWriter();
      out.write("<html><body>");
      out.write("<h2>");
      String ActLogName=request.getParameter("Logname");
      String ActPwd=request.getParameter("Pwd");
      String SqlQuery="select * from users where username='"+ActLogName+"' and password='"+ActPwd+"'";
      RsObj=StmtObj.executeQuery(SqlQuery);
      if(RsObj.next()==true)
      {
          String ExpLogName=RsObj.getString("username");
          String ExpPwd=RsObj.getString("password");
          if(ActLogName.equals(ExpLogName)&& ActPwd.equals(ExpPwd))
          {
              out.write("Login Success");
          }
      }
      else
      {
          out.write("Login Failed");
      }
      out.write("</h2>");
      out.write("</body></html>");
      }
      catch (ClassNotFoundException|SQLException exp) {

            exp.printStackTrace();
        }

      finally{
          try {
            RsObj.close();
            StmtObj.close();
            ConObj.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }

      }


  }
}

Below is my html code

<!DOCTYPE html>
<html>
<head>

<title>login page</title>
</head>
<body>
<form action="http://localhost:8080/FlipKart/loginpage">

LoginName :<input type="text" name="Logname"><br>
Password :<input type="password" name="Pwd"><br>
<input type="submit" value="Login">
<input type="button" value="cancel">

 </form>

</body>
</html>

Below is my web.xml code

<web-app>
<servlet>
<servlet-name>loginserv</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginserv</servlet-name>
<url-pattern>/loginpage</url-pattern>]
</servlet-mapping>
</web-app>

Can somebody help me to the resolve this issue.. Thanks in advance..:)

Kiranyls
  • 13
  • 1
  • 1
  • 3
  • What URL are you trying to hit? – Sotirios Delimanolis Mar 05 '14 at 18:09
  • You need to make sure that the application deployed on Tomcat is named as `FlipKart`. – Kashif Nazar Mar 05 '14 at 18:10
  • Another thing is that you should be using relative URLs in the form tag's action attribute if the page your accessing and the servlet in the action are hosted on the same location. – Kashif Nazar Mar 05 '14 at 18:12
  • http://localhost:8080/FlipKart/login.html...i was entering the username and password...as soon as i enter it will redirect it to http://localhost:8080/FlipKart/LoginServlet..and getting error.. – Kiranyls Mar 05 '14 at 18:13
  • This won't fix your problem, but you normally shouldn't override the service method. You should override doGet, doPost, doPut and doDelete as needed. – neildo Mar 05 '14 at 18:14
  • @neildo : am i getting error because of overriding service method ?? – Kiranyls Mar 05 '14 at 18:40

4 Answers4

3

You are overriding the wrong method, override doPost() instead and modify your form to include method="post" like this :

public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException
{
    //...
}

and this

<form action="http://localhost:8080/FlipKart/loginpage" method="post">

Also, if you don't want to always include the full path of the context manually, you can read this.

Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
1

Did you restart the server after making the package name change? Tomcat reads web.xml only on startup. Besides, make sure your classes are under webapps/WEB-INF/classes folder.

Boss Man
  • 587
  • 2
  • 12
1

It looks to me as though your application context needs to be changed. Perhaps it is being set to root. So it is not hitting /FlipKart

Look at this stackoverflow link for more information on setting the application context.

That is for tomcat 7. I know you can edit the META-INF/context.xml for previous versions. If you don't have that folder and file just create it.

EDIT Check your tomcat log to see where it is getting deployed or if something caused it to not get deployed at all. If you're running from an IDE, you may be able to see from the console.

Community
  • 1
  • 1
Mukus
  • 4,870
  • 2
  • 43
  • 56
0

Wrong reference to class in web.xml Use full name of Class with package name

<servlet>
<servlet-name>loginserv</servlet-name>
<servlet-class>demo.LoginServlet</servlet-class>
</servlet>
qwazer
  • 7,174
  • 7
  • 44
  • 69