Expand my Community achievements bar.

SOLVED

Need help on JSP and Sling servlet concepts.

Avatar

Level 2

Hi,

Using CRXde Lite, I'm trying to create a login page with jsp, and submutting the form to sling servlet where I can validate the user. Displaying some message if the user is a valid user.

Please see my sample code below:

this .jsp file is under /apps/test/components/testComponent/testLogin.jsp

 <form method = "post" action="/content/test/LoginServlet" nctype="multipart/form-data">

        <h3>Please enter your username         

            <input type="text" name="name"/> </h3><br>        
        
        <h3>Please enter your password
            <input type="password" name="password"/> </h3> <br>

        <input type="submit" name="Submit" value="Submit">        
        </form>

 What should be the value/path for 'action' in the 'form'? I wrote a LoginServlet.java under /content/test folder.

 

And my LoginSerlvet.java template looks like:

@Component(immediate = true, metatype = false, label = "LoginServlet")
   @Service
   @Properties(value = {
                    @org.apache.felix.scr.annotations.Property(name = "sling.servlet.methods", value = { "POST" }),
                    @org.apache.felix.scr.annotations.Property(name = "sling.servlet.resourceTypes", value = { "/content/test/LoginServlet" })

   })
/*@WebServlet(urlPatterns = "/LoginServlet")*/
public class LoginServlet extends SlingAllMethodsServlet  {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */

    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
        // TODO Auto-generated constructor stub
        String id = request.getParameter("name");
        String pass = request.getParameter("password");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = null;
        request.setAttribute(id, "name");
        if(id.equals("name") && pass.equals("pass")){
            out.println("<b>Successfully logged in</b><br>");

        }
        else{
            out.println("<b>Invalid Login Info.</b><br>");
            }
        out.close();
    }

}

So, when the user click on Submit button it should go to the LoginServelt to validate the user. Can anyone help me on how to send the call to LoginServlet when the user click on Submit? What should be the folder structure, where to place the components, templates and .java files? For this example, where should I place .jsp and sling servlet file?

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

See this community article - it talks about how to use AJAX from a front end to invoke a Sling Servlet (and the path prop):

@SlingServlet(paths="/bin/mySearchServlet"

http://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

View solution in original post

4 Replies

Avatar

Level 9

For a Servlet registered as an OSGi service to be used by the Sling Servlet Resolver, either or both of the sling.servlet.paths or the sling.servlet.resourceTypes service reference properties must be set. If neither is set, the Servlet service is ignored.

Please review the following example

http://edivad.wordpress.com/2011/07/18/cq-sling-servlets-resourcetype/

Avatar

Level 2

Thanks for the quick response. I will give it a try.

Avatar

Correct answer by
Level 10

See this community article - it talks about how to use AJAX from a front end to invoke a Sling Servlet (and the path prop):

@SlingServlet(paths="/bin/mySearchServlet"

http://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html