1

My suspicion is that my wording for this is incorrect and that is why I cant find a suitable existing solution.

What I have is a JSP - "X" and within it another JSP - "Y".I want to be able to press a button on "X" and send information to change the contents of "Y".

Here is a simplification of my code.

mainJSP_X.jsp

<button type="button" onlick=javascript:sendInfo('test')> Button </button>

<%@ include file = "secondJSP_Y.jsp" %>

<script type="text/javascript">

sendInfo = function(input){
          $.ajax({
              type:'GET',
              url: 'secondJspServlet'
              data: 'Name='+input,
              ....
}

secondJspServlet.java

@WebServlet("/secondJspServlet/*")

protected void doGet(){
       String name = request.getParameter("Name");
       
       request.setAttribute("name", name);
       RequestDispatcher requestDispatcher = request.getRequestDispatcher("secondJSP_Y.jsp");
       requestDispatcher.forward(request, response);
}

secondJSP_Y.jsp

<%
Map<String, String[]> parameters = request.getParameterMap();
System.out.println(parameters.size());
%>

<p>Name:${name}</p>

So in this situation it should appear as "Name:test". The code above there is just me checking if theres anything request, there isnt.

In my testing, my parameters reach the servlet, but cannot reach the second JSP, I think the problem with my approach is that the secondJSP is not expecting a request to update itself.

What is the correct approach for this kind of situation. If possible I am searching for a solution that passes through the servlet as I need to do a lot more in there than this example shows.

Staykov
  • 19
  • 2
  • Hi, if you need to redirect to next page why using ajax ? why not use form ? – Swati Dec 10 '20 at 13:27
  • @Swati Hm, because its not a next page, its a page within the page that would be invisible(not-populated) before pressing the button. Also why form when there's nothing being populated for a form ? – Staykov Dec 10 '20 at 13:47
  • because here you are using ajax so you cannot redirect from `secondJspServlet` response will come back to ajax and then you can redirect . Also , [this](https://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax) post will help you to understand more . – Swati Dec 10 '20 at 14:13
  • I've seen that post but it does not contain what I need, the ajax is not imporant, can you suggest a solution with form or anything that will allow the second jsp to access request variables set from the servlet such as collections and/or java objects to be used with jstl ? – Staykov Dec 10 '20 at 14:32
  • With form : `
    ` when you will click on submit button form will submit to servlet with `test` as value . Try like this then let me know if you face any problem.
    – Swati Dec 10 '20 at 14:39
  • I tested it, but it tries to redirect me to the jsp instead of updating it. I am not sure maybe its better to not have a second jsp at all and just jsp1->button1->info to servlet->servlet processing->put data in request/response->jsp1 and then jsp1 updates data in a div. – Staykov Dec 10 '20 at 15:10
  • Then above link which i have added in previous comment follow that . – Swati Dec 10 '20 at 16:22

0 Answers0