Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting to a page using restful methods?

I've created a page which asks user to fill some form fields and when he submits, the form is sent to a Restful method which you can see below:

@POST
@Path("addUser")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addUser(@FormParam("username") String username,
        @FormParam("password") String password,
        @FormParam("id") String id,
        @FormParam("group_name") String groupName,
        @FormParam("authority_name") String authorityName,
        @FormParam("authority_id") String authorityId
        )
{
    //Something will be done here
}


How can I redirect the user at the end of this function to (let's say) index.jsp?

like image 456
Matin Kh Avatar asked Jun 20 '12 09:06

Matin Kh


3 Answers

Create a URI using javax.ws.rs.core.UriBuilder that maps the parameters and other data you want to preserve. Then use Response.temporaryRedirect to return a redirect to the client and pass it the URI you’ve built.

like image 55
sergiofbsilva Avatar answered Oct 06 '22 22:10

sergiofbsilva


change your code like this, the addUser() should return a Response Object

public Response addUser(@FormParam("username") String username,
        @FormParam("password") String password,
        @FormParam("id") String id,
        @FormParam("group_name") String groupName,
        @FormParam("authority_name") String authorityName,
        @FormParam("authority_id") String authorityId
        )
{
    //Something will be done here

    java.net.URI location = new java.net.URI("../index.jsp?msg=A_User_Added");
    return Response.temporaryRedirect(location).build()

}
like image 35
Joey Avatar answered Oct 07 '22 00:10

Joey


See below the usage of redirecting in web services:

public class LoginWebService {

    @POST
    @Path("/check")
    public Response checkDetails(@FormParam("name") String name,@FormParam("pass") String pass ) throws URISyntaxException  {

        URI uri = new URI("/login/success");
        URI uri2= new URI("http://localhost:9090/NewWebServiceproject/new/login/failure");

        if(name.equals("admin") && pass.equals("pass"))
    //@Path("http://localhost:8010/NewWebServiceproject/new/login/success");
            {
            return Response.temporaryRedirect(uri).build();
            //Response.seeOther(uri);
            //return Response.status(200).entity("user successfully login").build();
            }
        else
        {
            return Response.temporaryRedirect(uri2).build();
            //Response.seeOther(uri2);
            //return Response.status(200).entity("user logon failed").build();
            }
    }
    @POST
    @Path("/success")
    public Response successpage()
    {
    return Response.status(200).entity("user successfully login").build();
    }
    @POST
    @Path("/failure")
    public Response failurepage()
    {
    return Response.status(200).entity("user logon failed").build();
    }
}
like image 33
Divya Jaiswal Avatar answered Oct 06 '22 22:10

Divya Jaiswal