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
When I start I run the project I come to here(notice the url) :
Then I enter some info and click 'submit' and I get this:
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...: