what values can a JSP form "action" take?

29,544

As per the specs , it can take any valid URI

This attribute specifies a form processing agent. User agent behavior for a value other than an HTTP URI is undefined.

Can i give the servlet class in the form action ?

Yes if the servlet class name resolves to a valid URL mapped in the web.xml or else you will encounter a 404 .

Let us consider your JSP is at the root of the application, then

<FORM action="someServletName" method="post">

Now this will be resolved as protocol://servername:port/context/someServletName .Now somewhere you should have a mapping for /someServletName , either in web.xml or through annotation to a Servlet or JSP.

<servlet>
     <servlet-name>someServletName</servlet-name>
     <servlet-path>packageName.servletName</servlet-path>
</servlet>
<servlet-mapping>
     <servlet-name>someServletName</servlet-name>
     <url-pattern>/someServletName</url-pattern>
</servlet-mapping>
Share:
29,544
Siddhu
Author by

Siddhu

Updated on July 06, 2020

Comments

  • Siddhu
    Siddhu almost 4 years

    What can a JSP form action be? I have a Login.jsp page for the user to end the details. Can i give the servlet class in the form action?

    here is the the servlet code.

    package mybean;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public LoginServlet() {
        super();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
            System.out.println("In the Login Servlet");
            LoginBean user = new LoginBean();
            user.setUemail(request.getParameter("uemail"));
            user.setUpass(request.getParameter("upass"));
            user = LoginDAO.login(user);
            if(user.isValid())
            {
                HttpSession session = request.getSession(true);
                session.setAttribute("currentSessionUser",user);
                response.sendRedirect("LoginSuccess.jsp");
            }else
                response.sendRedirect("LoginFailed.jsp");
        } catch (Throwable exc)
        {
            System.out.println(exc);
        }
    }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    
    }
    
  • Siddhu
    Siddhu almost 11 years
    I have updated the web.xml file as you mentioned, but still am getting 404 error, saying the requested resource is not available. What would have been the problem?
  • AllTooSir
    AllTooSir almost 11 years
    Show us your JSP and web.xml .
  • Siddhu
    Siddhu almost 11 years
    I made few changes, now i didn't get the error. but the redirection of the links is not being done properly... I get a black page with the page url as the servlet name? What would have been the problem? please help.