-1

I need to include the content of a jsp file in my servlet. I tried this.

out_str = "<jsp:include page=\"jsp/agentMenu.jsp\"/>";

Here out_str is a String type variable.

But this doesn't work.

Edited: I am using a post request and get the response. According to that result displaying content will be different.

private void initCall(String urlParameters) {
    String result = "";

    try {
        //result = sendPostRequest(urlParameters);
        result = "0#04#Succesfully Login";
        System.out.println(result);    //for debugging
        String[] out_array = splitResult(result);

        if (Integer.parseInt(out_array[0]) == 0) {    //success
            System.out.println("SUCCESS");
            switch (Integer.parseInt(out_array[1])) {
                case 1:
                    out_str = "<html>\n"
                            + "<body align=\"center\">\n"
                            + "<center><h2>RobiCash</h2>\n"
                            + "<p>Invalid PIN<p></center>\n"
                            + "<a href=\"jsp/login.jsp\">Exit</a>"
                            + "</body>\n"
                            + "</html>";

                    break;
                case 2:
                    out_str = "<html>\n"
                            + "<body align=\"center\">\n"
                            + "<center><h2>RobiCash</h2>\n"
                            + "<p>Invalid Mobile Number<p></center>\n"
                            + "<a href=\"jsp/login.jsp\">Exit</a>"
                            + "</body>\n"
                            + "</html>";
                    break;
                case 3:
                    out_str = "<html>\n"
                            + "<body align=\"center\">\n"
                            + "<center><h2>RobiCash</h2>\n"
                            + "<p>System Error<p></center>\n"
                            + "<a href=\"jsp/login.jsp\">Exit</a>"
                            + "</body>\n"
                            + "</html>";
                    break;
                case 4:  //Agent
                    System.out.println("AGENT");
                    //out_str.concat("<%@ include file=\"jsp/agentMenu.jsp\" %>");
                    out_str =  <jsp:include page="jsp/agentMenu.jsp" />;


                    /*out_str = "<html>\n"
                            + "<body align=\"center\">\n"
                            + "<h2>RobiCash</h2>\n"
                            + "<h3>Agent Main Menu</h3>\n"
                            + "<table align=\"center\">\n"
                            + "<tr><td>1:</td><td><a href=\"agent_2.htm\">Top-Up/Recharge (P2)</a></td></tr> \n"
                            + "<tr><td>2:</td><td><a href=\"jsp/billPayment.jsp\">Bill Payment</a></td></tr>\n"
                            + "<tr><td>3:</td><td><a href=\"agent_2.htm\">Bill Check</a></td></tr>\n"
                            + "<tr><td>4:</td><td><a href=\"agent_3.htm\">Cash IN (P2)</a></td></tr>\n"
                            + "<tr><td>5:</td><td><a href=\"agent_4.htm\">Cash OUT (P2)</a></td></tr>\n"
                            + "<tr><td>6:</td><td><a href=\"agent_5.htm\">Customer Registration</a></td></tr>\n"
                            + "<tr><td>6:</td><td><a href=\"agent_6.htm\">My Balance</a></td></tr>\n"
                            + "<tr><td>7:</td><td><a href=\"agent_7.htm\">More</a></td></tr>\n"
                            + "<tr><td>8:</td><td><a href=\"jsp/login.jsp\">Exit</a></td></tr>\n"
                            + "</table>\n"
                            + "</body>\n"
                            + "</html>\n";*/
                    break;
                case 5:       //DSR
                    break;
                case 6:      //Distributor
                    break;
                case 7:      //Master Distributor
                    break;
                default:
                    break;
            }
        }  else {      //fail

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    response = ServletActionContext.getResponse();
    response.setContentType("text/html");
    PrintWriter printWriter = null;
    try {
        printWriter = response.getWriter();
    } catch (IOException e) {
        e.printStackTrace();
    }

    StringBuffer _bf = new StringBuffer();

    _bf.append(out_str);
    printWriter.write(_bf.toString());
    printWriter.flush();
    printWriter.close();
}

Any suggestions would be appreciated.

Thank you in advance

Rose18
  • 2,892
  • 8
  • 47
  • 98
  • Putting html in a Controller is a bad pratice. I'd suggest you to change the jsp name depending on the case and including it with RequestDispatcher. – Sezin Karli Sep 15 '14 at 06:14
  • I am very new java ee. I would be much obliged if u would be so king enough to give me a some advice to how to do it – Rose18 Sep 15 '14 at 06:20
  • using JSP is a bad practice to begin with, that and your inability to search for existing answers is your fundamental problem. –  Sep 15 '14 at 06:29

4 Answers4

1
RequestDispatcher rd = request.getRequestDispatcher(“jsp/agentMenu.jsp”); 
rd.include(request, response);

You can use the code above.

Sezin Karli
  • 2,517
  • 19
  • 24
1

Problem is because of you are writing string into Servlet response. What you have write in response using PrintWriter.write() is treated as HTML string or simple string and write into your response or browser, it will be not execute as JSP by the browser.
For statement out_str = "<%@ include file=\"jsp/agentMenu.jsp\" %>" you have to interpret or execute your JSP page to make this into effect. So this treated as simple HTML string and could not leads to result JSP content which is what you want as output.

Do include any other content in current response you can use RequestDispatcher as show below.

RequestDispatcher rd = request.getRequestDispatcher("jsp/agentMenu.jsp");
rd.include(request, response);     

include above line into case:4 condition.

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
0
out_str = <jsp:include page="jsp/agentMenu.jsp" />; 

I think this should work. Otherwise, check the path again or use Request Dispatcher.

Zack Kaytranada
  • 351
  • 3
  • 11
0

Edited: I am using a post request and get the response. According to that result displaying content will be different.

Your content may dynamically vary , its no issues to use the request dispatcher. Things you need to follow as the beginner.

  1. Write your code-logics in the controller , it is also considered to be bad practice as you need to separate it with service classes .
  2. Set the required values in the request or session scopes
  3. use the RequestDispatcher or pageRedirect to access the views from the controller\
  4. Print the objects from the request using EL or JSTL

A simple example here and lot of references in the internet to start with.

Hope this helps !!

Santhosh
  • 8,181
  • 4
  • 29
  • 56