1

I have a JSP page (client-side)

<form action="http://localhost:8080/REST-WS/rest/token" method="POST">
<label for="email">Email</label>
<input name="email" />
<br/>
<label for="password">Password</label>
<input name="password" />
<br/>
<input type="submit" value="Submit" />
</form>

It points to a function in REST Web Service (server-side)

@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Code verify(@FormParam("email") String email,
@FormParam("password") String password,
@Context HttpServletResponse servletResponse) throws IOException {
    Code code = generateRandomCode(email,password);
    return token;
}

The problem is I want to give response to the client side containing the random-generated code from the server side.

First, it will be redirected to another JSP page and then the client side can receive the random-generated code from server.

How do I do it?

ZZZ
  • 1,415
  • 5
  • 16
  • 29

1 Answers1

3

The problem is that you can't send arbitrary Java objects in a redirect. You can however add the data into query parameters. For example

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@FormParam("name") String name,
                     @FormParam("email") String email) {
    
    String message = "Hello " + name + ". Your email is " + email;
    URI uri = UriBuilder.fromPath("/index.jsp")
            .queryParam("message", message)
            .build();
    return Response.seeOther(uri).build();
}

Here, you are building a URI from the location of the jsp page, and adding a query parameter to the end of the URI. So the redirect will go to

http://localhost:8080/index.jsp?message=<the message>

From the index.jsp page you can get the parameter with request.getParameter("message"). For example

<h1><%= request.getParameter("message") %></h1>

Another option to work with JSP and Jersey is to implement MVC, which Jersey provides support for. You can check out this answer, though the examples use Maven (to get all the required jars). If you are interested and don't know how to use Maven, let me know and I'll see if I can help you get all the jars you need.


UPDATE

Ajax Example.

Easiest Javascript library to get started with (if you have no experience) is jQuery. I won't really give much explanation about the code, that's kinda out of scope. I would go through some tutorials (W3Schools has some good getting started guides), and there are answers all over SO that can answer your questions.

Here's a complete working html page. Just change var url = "/api/submit"; to whatever endpoint you are sending the request to.

<!DOCTYPE html>
<html>
    <head>
        <title>Ajax Example</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
        <script>
            $(document).ready(function(){
                var url = "/api/submit";
                
                $("#submitBtn").click(function(e) {
                    e.preventDefault();
                    
                    var formData = $("#nameForm").serialize();
                    $.ajax({
                        url: url,
                        data: formData,
                        dataType: "json",
                        type: "POST",
                        success: function(data) {
                            var message = data.message;
                            var date = data.date;
                            
                            var h1 = $("<h1>").text(message);
                            var h3 = $("<h3>").text(date);
                            
                            $("#content").empty()
                                    .append(h1).append(h3);
                        },
                        error: function(jqxhr, status, errorMsg) {
                            alert(status + ": " + errorMsg);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <div id="content">
            <form id="nameForm">
                First Name: <input type="text" name="fname"/><br/>
                Last Name : <input type="text" name="lname"/><br/>
                <button id="submitBtn">Submit</button>
            </form>
        </div>
    </body>
</html>

Here is the test resource class

@Path("submit")
public class FormResource {
    
    public static class Model {
        public String message;
        public String date;
    }
    
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Model post(@FormParam("fname") String fname,
                      @FormParam("lname") String lname) {

        String message = "Hello " + fname + " " + lname;
        Model model = new Model();
        model.message = message;
        model.date = new Date().toString();
        return model;
    }
}

You will need to make sure you have a JSON provider to handle the JSON Pojo serialization or it won't work (the Model won't be able to serizalize to JSON).

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • How to use XML or JSON instead of parameter? Is it possible? – ZZZ Nov 15 '15 at 06:41
  • Redirects don't have any body. If you want to make an ajax request instead, it can return JSON to the calling page. Or you can return a jsp page without a redirect. For that you should look into the MVC – Paul Samsotha Nov 15 '15 at 06:48
  • The problem is in the application I'm currently working on, server (REST) and client(JSP) have to be on different ports, so I don't think MVC work. But I'm intersted with your solution to make an ajax request. Can you give me an example? – ZZZ Nov 15 '15 at 07:12
  • I got this error `SEVERE: MessageBodyWriter not found for media type=application/json` – ZZZ Nov 15 '15 at 09:55
  • Exactly. I said you need a JSON provider. See [this answer](http://stackoverflow.com/a/32412830/2587435) – Paul Samsotha Nov 15 '15 at 10:04