1

I know previous questions have been asked, can't find the right solution...

I'm trying to create a login page. I've already have a working registration page, but my login page shows this error: https://i.stack.imgur.com/m8G0i.jpg

The .jsp works fine- My login page appears, shows a textbox for username and password, and the button to login. But when I type in the credentials (both correct and wrong), I get the error shown in the imgur above.

Here is my code:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Registration</display-name>

    <servlet>
    <servlet-name>reg</servlet-name>
    <servlet-class>jdbc.Registration</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>reg</servlet-name>
    <url-pattern>/regServlet</url-pattern>
    </servlet-mapping>


    <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>jdbc.Login</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
    </servlet-mapping>

Login.java (Servlet)

@WebServlet("/Login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
        super();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
            String name = request.getParameter("user");
            String password = request.getParameter("password");
            String dbName = null;
            String dbPassword = null;
            String sql = "select * from users where username=? and password=?";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root","");
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            PrintWriter out = response.getWriter();
            while(rs.next()) {
                dbName = rs.getString("username");
                dbPassword = rs.getString("password");

            }
            if (name.equals(dbName)&&password.equals(dbPassword)) {

                out.println("You have successfully Logged in!");
            }
            else {
                //response.sendRedirect("login.jsp");
                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                rd.include(request, response);

            }

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();
        }
    }

  }

Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<div align="center">
<form action="Login" method="post">
Username: <input type="text" name="user" required="required">
Password: <input type="password" name="password" required="required">
<input type="submit" value="LOGIN">
</form>
</div>
</body>

MySQL database

https://i.stack.imgur.com/YslxO.jpg

Database name is "employee" Table name is "users"

Zack King
  • 31
  • 7
  • So, do you want your application to be a servlet 3.x webapp, using annotations for mapping servlets, or do you want it to be a servlet 2.5 webapp using XML for mapping servlets? Pick one, and stick to it. – JB Nizet Feb 25 '18 at 08:50
  • I'm sorry, Im new to this still. Can u clarify a bit more? This worked for my registration.java page, I was able to add user credentials to MySQL, but its not working for the login.java page – Zack King Feb 25 '18 at 08:55
  • You annotated your servlet with `@WebServlet("/Login")`. This is supported since version 3.0 of the spec, and is used to tell the container that this class is a servlet that should be mapped to /Login. But you *also* have a web.xml file with `version="2.5"`, declaring the class as a servlet, and mapping it to a different URL: /loginServlet. Why do you have those two contradictory things in your webapp? – JB Nizet Feb 25 '18 at 09:01
  • Oh. I didn't know actually, I was watching tutorials when doing this. I removed it from my Login.java file, but my problem still exists- Is there anything else I can do? – Zack King Feb 25 '18 at 09:05
  • So you chose to use the old, 2.5 way, of mapping your servlet, instead of the newer, 3.x way? Your servlet is thus mapped to /loginServlet. But your form posts to "Login". – JB Nizet Feb 25 '18 at 09:07
  • Unrelated to your question: Saving clear text password is nothing you should get used to. It's ok to do this for learning the technology, but you should get rid of manual login handling asap. – Olaf Kock Feb 25 '18 at 10:26
  • @JBNizet I tried changing my version of web.xml from 2.5 to 3, but that gave more errors, so I chose to use 2.5. So now, do I need to change my form action = "Login" to "LoginServlet"? – Zack King Feb 25 '18 at 17:07
  • @OlafKock, I will change that later, I just wanted my login page to work, then I'll go back and perfect the small details – Zack King Feb 25 '18 at 17:08
  • I had errors when trying to change it to 3.x way, but I fixed it now. Now my web.xm is version 3.1, and I removed the @WebServlet notation from my login.java page. However, I still have the same error. – Zack King Feb 25 '18 at 17:12
  • No. You need to understand that you're doing two contradictory things with annotations and XML, that you dont need XML at all if you're using annotations, that errors should be read, understood and fixed rather that circumvented, and that LoginServlet and loginServlet are two different things. – JB Nizet Feb 25 '18 at 17:13
  • I changed it to 3.1, and removed the annotations from my Login.java page- Should that be enough? It should now map to /loginServlet right? – Zack King Feb 25 '18 at 17:19

0 Answers0