1

I have created a post yesterday regarding this, but someone decided to close it. I have tried solutions from the post, it didn't work. If answer from the post could solve my issues, I won't be asking the same question again. Tried Put servlet class in a package, Set servlet URL in url-pattern, Use domain-relative URL to reference servlet from HTML. The thing is my registration.jsp is almost the same as my login.jsp same for the servlets as well, I dont get why it doesnt work for registration when it works for the login servlet. I also have clean and restarted my server multiples times. Didnt install tomcat into the computer just import it thru zip files into eclipse.

HTTP Status 404 return by Servlet Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"

Login Servlet

package net.login.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;

import net.login.dao.UserDAO;
import net.login.model.Users;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet{

    public @interface WebServlet {

    }

    private static final long serialVersionUID = 1L;

    public UserLoginServlet() {
        super();
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
         
        UserDAO userDao = new UserDAO();
         
        try {
            Users user = userDao.checkLogin(username, password);
            String destPage = "login.jsp";
             
            if (user != null) {
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                destPage = "home.jsp";
            } else {
                String message = "Invalid username/password";
                request.setAttribute("message", message);
            }
             
            RequestDispatcher dispatcher = request.getRequestDispatcher(destPage);
            dispatcher.forward(request, response);
             
        } catch (SQLException | ClassNotFoundException ex) {
            throw new ServletException(ex);
        }
    }

}

register.jsp

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registration</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.0/dist/jquery.validate.min.js"></script>
<link href=”bootstrap/css/bootstrap.min.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript” src=”bootstrap/js/bootstrap.min.js”></script>
</head>
<body>
    <div style='text-align=center' class="container">
    <h1>New User Registration</h1>
        <form action='register' method='POST' role='form'>
        <div class='registerform'>
            <label for='username'> Username: </label>
            <input type="text" placeholder='Username' class='form-control' name='username'>
        </div>
        <div class='registerform'>  
            <label for='password'> Password: </label>
            <input type="password" class='form-control' placeholder='Password' name='password'>
        </div>
            <br><br>
            <button type="submit" class="btn btn-default"> Register </button>
        </form>
    </div>
    
</body>
</html>

UserRegisterServlet

package net.login.controller;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.login.dao.UserDAO;
import net.login.model.Users;

@WebServlet("/register")
public class UserRegisterServlet extends HttpServlet{
    
    
    
    public @interface WebServlet{
        
    }
    
    private static final long serialVersionUID = 1L;
    
    public UserRegisterServlet() {
        super();
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //      String pathInfo = request.getPathInfo(); 
//      String[] pathParts = pathInfo.split("/");
//      String part1 = pathParts[1];
//      String part2 = pathParts[2]; 
//      
//      System.out.println(part1 + part2); //doesnt work for now since cant find /register
        
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        UserDAO userDAO = new UserDAO();
        
        try {
            userDAO.createUsers(username, password);
            String destPage = "register.jsp";
            
            System.out.println("Success");
            RequestDispatcher dispatcher = request.getRequestDispatcher(destPage);
            dispatcher.forward(request, response);
            
            
        }catch(SQLException | ClassNotFoundException ex) {
            throw new ServletException(ex);
        }
        
    }
    
}
Anon1234
  • 23
  • 5
  • Just curious, would changing `
    ` work for you :) Notice the slash
    – JCompetence Jan 26 '22 at 09:31
  • Where exactly did you learn that you should be putting `public @interface WebServlet {}` in the servlet class? – BalusC Jan 26 '22 at 10:53
  • didnt work btw @SMA, i tried just register or `${pageContext.request.contextPath}/register` or '/register' <- this just make it point towards the rootfolder which is not what i want because there isnt any content there. – Anon1234 Jan 27 '22 at 01:26
  • Honestly not much, I just learned from tutorials and that is what they did so I just copied them @BalusC – Anon1234 Jan 27 '22 at 01:27

0 Answers0