0

I just learned a great deal from this post doGet and doPost in Servlets and Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available". But I have gotten a little confused. My servlet loads then redirects to certain page by doGet(), but then I cannot use that same servlet to doPost() and direct to another page. I see a 404 error - Not Found.

@WebServlet(name = "LoginServlet", displayName="Login Servlet", urlPatterns={"/LoginServlet"}, loadOnStartup=1)
public class LoginServlet extends HttpServlet {

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

        request.getRequestDispatcher("/WEB-INF/view/Home.jsp").forward(request, response);  

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

       String userName = request.getParameter("userName");
       String password = request.getParameter("password");

       UserAccount userAccount = DataDAO.findUser(userName, password);

       if(userAccount == null) {
           String error = "Invalid User Name or Password";
           request.setAttribute("errorMessage", error);
           RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/WEB_INF/views/Home.jsp");
           dispatcher.forward(request,response);

           return;

       } else{
           request.getSession().setAttribute("user",userAccount);
           //response.sendRedirect("/WEB_INF/views/index.jsp");
           request.getRequestDispatcher("/WEB_INF/views/index.jsp").forward(request,response);
       }

    }

}

EDIT: I tried adding this annotation and removed the mappings from web.xml, the servlet was still not found... This is the form I have been trying to submit:

<form action="${pageContext.request.contextPath}/LoginServlet" method="post" class="form">
                <p>username: <input type="text" name="username" class="username" /></p>
                <p>password: <input type="pass" name="password" class="password" /></p>
                <p><input type="submit" value="submit" id="loginbutton" class="buttons" /></p>
            </form>

web.xml, based on reading this: @WebServlet annotation with Tomcat 7:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
        <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>flashcardshark.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/Home.jsp</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <res-ref-name>jdbc/FlashCardShark</res-ref-name>
        <res-type>javax.sql.ConnectionPoolDataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>

I then changed the java build path to load the compiled sources before compiling sources based on these readings: HTTP 404 not found(Servlet not found)

Equivalent of "Order and export" of Eclipse in Netbeans

...based on what I read there I started cleaning and building before deploying to the server, still no dice.

using NetBeans 8.1, GlassFish 4.1.1, Java EE 7 Web and, JDK 8

Here is my File tree: enter image description here

When I start I run the project I come to here(notice the url) : enter image description here

Then I enter some info and click 'submit' and I get this: enter image description here

server log:

Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   Loading application [FlashCardSharkWeb] at [/FlashCardSharkWeb]
Info:   FlashCardSharkWeb was successfully deployed in 201 milliseconds.
Severe:   PWC6117: File "null" not found

Also reading this now: https://stackoverflow.com/tags/servlets/info

I also tried to accessing it with get requests but I saw the same error. Please Help.

EDIT: After reading the wiki I moved all JSP to WEB-INF, changed the annotation to @WebServlet("/Home") and changed the form to:

<form action="Home" method="post" class="form">
                <p>username: <input type="text" name="username" class="username" /></p>
                <p>password: <input type="pass" name="password" class="password" /></p>
                <p><input type="submit" value="submit" id="loginbutton" class="buttons" /></p>
            </form>

... no dice, http://localhost:8080/FlashCardSharkWeb/Home not found.

EDIT: after re-reading Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available", I double checked that NetBeans was putting compiled servlet in the WEB-INF folder...: enter image description here

MadMax
  • 605
  • 1
  • 6
  • 19
  • The form action is pointing to Home.jsp - try changing the action to /LoginServlet. – Andrew S Mar 14 '18 at 17:09
  • Also, from this comment _TODO: make this smart enough to know if it was the uname or pw that was wrong_: don't tell the user which field is incorrect. This is a security flaw which provides hackers with too much info. – Andrew S Mar 14 '18 at 17:13
  • Ah hah ! I've always been annoyed at that, I've always said why can't it say which one is wrong. Thanks for the insight. – MadMax Mar 14 '18 at 18:50
  • No dice, still get a 404. Does it have something to do with the package the servlets in ? – MadMax Mar 14 '18 at 18:55
  • Any log error details? What is the path where doGet() works? – Andrew S Mar 14 '18 at 19:03
  • The servlet loads when first deployed and the server is started, I added Home.jsp – MadMax Mar 14 '18 at 19:13
  • added that to web.xml – MadMax Mar 14 '18 at 19:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166840/discussion-between-madmax-and-andrew-s). – MadMax Mar 14 '18 at 19:14

1 Answers1

0

The problem was with doPost() redirect code it should have been this:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

       response.setContentType("text/html;charset=UTF-8");

       String userName = request.getParameter("username");
       String password = request.getParameter("password");

       System.out.println(userName + " " +password);
       UserAccount userAccount = DataDAO.findUser(userName, password);

       if(userAccount == null) {
           System.out.println("no such username or password");
           String error = "Invalid User Name or Password";
           request.setAttribute("errorMessage", error);
           RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Home.jsp");
           dispatcher.forward(request,response);
           //response.sendRedirect("/Home.jsp");

           return;

       } else{
           System.out.println("bing return the user");
           HttpSession session = request.getSession();
           session.setAttribute("user",userAccount);
           //response.sendRedirect("/WEB_INF/index.jsp");
           //request.getRequestDispatcher("/WEB_INF/index.jsp").forward(request,response);
           RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/WEB-INF/index.jsp");
           dispatch.forward(request,response);
       }

    }
MadMax
  • 605
  • 1
  • 6
  • 19