EDIT : Was marked as duplicate (Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available") I don't have any issue on setting up the servlet the first time, everything works and when i submit it loads the results properly. The duplicate shows how to set up xml and jsp and i have them set in the same way. If i don't fill them once i have the normal retry but the second time regardless if it's correct or not the url becomes .../one/one/main
, resulting in 404.
If i set the url pattern of xml to the class name without the package ( in the example <url-pattern>/servlet</url-pattern>
) the servlet doesn't even start at the first correct try. Regardless of the url pattern and referencing from form, on url pattern i have /one/Main
, on form one/Main
. In the duplicate any of the example of form i tried result in a 404 after the submit.
I have tried to remove the xml servlet configuration and using @WebServlet
from the servlet with this ( @WebServlet(description = "Main", urlPatterns = { "/one/Main" })
), I have the same issue, first time works, second try it's one/one/Main in the url.
In this simple servlet whenever i use a package it changes the url to ../one/one/Main
when i don't fill both input text fields the second time.
public void doPost(HttpServletRequest request
, HttpServletResponse response) throws ServletException, IOException{
String action = request.getParameter("action");
String url = "/index.jsp";
if(action==null)
action="join";
if(action.equals("join"))
url="/index.jsp";
else if(action.equals("ok")){
String fName = request.getParameter("firstName");
String sName = request.getParameter("secondName");
if(fName==null||sName==null||fName.equals("")||sName.equals("")){
url = "/index.jsp";
request.getSession().setAttribute("message"
, "Please fill all the fields");
}
else{
url = "/thanks.jsp";
request.getSession().setAttribute("user"
, new User(fName, sName));
}
}
getServletContext().getRequestDispatcher(url)
.forward(request, response);
}
jsp form part
<form action="one/Main" method="post">..</form>
xml servlet mapping
<servlet>
<servlet-name>Main</servlet-name>
<servlet-class>one.Main</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/one/Main</url-pattern>
</servlet-mapping>
I have tried to copy the classes outside the one
package and reconfigure the form tag and xml accordingly ( set servlet class to Main
, pattern to /Main
and jsp action to Main
), at that point it works perfectly, i can miss the filling of the fields how many times i want, it will keep reloading the proper page with the message, but as soon as the servlet and User
javabean are inside the one package when i run and miss the inputs twice, the url changes to double package /Main
and i have a 404.