0

EDIT : 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 /servlet ) 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, 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 Userjavabean 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.

T4l0n
  • 595
  • 1
  • 8
  • 25
  • This has nothing to do with packages. This has everything to do with URL pattern and how you reference it in form action. – BalusC Feb 13 '16 at 14:26
  • then why does it work as soon as i remove packages and set servlet class to "Main", pattern to "/Main" and jsp action to "Main" ? – T4l0n Feb 13 '16 at 17:27
  • Because you also changed the URL pattern. Just don't change it, else you need to fix HTML form action as per the answer in the duplicate. – BalusC Feb 13 '16 at 21:52

0 Answers0