5 Replies Latest reply: Mar 5, 2013 11:34 PM by r035198x RSS

    How to call JSP from a java class?

    srhcan
      I have a java class Enrollment which has a method enroll() which returns a String.
      public class Enrollment {
        public String enroll() { ...
        }
      }
      I have another java class EnrollmentWF which has a method processEnrollment(); this method instantiates Enrollment class, call its enrollEmp method and get the return value as a string. After that I want it to call a JSP Test.jsp passing the string return value to the JSP. How can I do that?
      public class EnrollmentWF {
        public void processEnrollment() {
          Enrollment enrollment = new Enrollment();
          String returnValue = enrollment.enroll();
          // now need to call Test.jsp 
        }
      }
      Thanks

      Edited by: srhcan on Feb 28, 2013 8:00 AM
        • 1. Re: How to call JSP from a java class?
          Kayaman
          You can create a HTTP request and pass the return value as an URL parameter (or if the page uses POST, pass it that way).
          • 2. Re: How to call JSP from a java class?
            srhcan
            Kayaman wrote:
            You can create a HTTP request and pass the return value as an URL parameter (or if the page uses POST, pass it that way).
            can you give example of this (how to create HTTP request and passing the value)? Thanks
            • 3. Re: How to call JSP from a java class?
              Kayaman
              You can use the HttpURLConnection class, or get an easier to use library, such as the apache HttpClient.

              Tens of thousands of examples on both available on the internet.
              • 4. Re: How to call JSP from a java class?
                srhcan
                ok I tried a simple test and it is not working, can anyobdy tell me what I am doing wrong here?

                I created a web application project JEE_Test
                I created 2 jsp: First.jsp and Second.jsp
                I created 1 class: Test.java

                First.jsp instanstiates Test.java and calls its method redirect() which creates the HTTP request for Second.jsp.
                What I want is that I will call the First.jsp as http://localhost:8080/JEE_Test/First.jsp and I will get the Second.jsp

                This is my First.jsp:
                <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
                <%@page import="com.test.Test"%>
                <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                
                <html>
                
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                <title>First.jsp</title>
                </head>
                
                <body>
                
                <%
                Test test = new Test();
                System.out.println("First.jsp: test object created");
                
                test.redirect();
                %>
                
                FIRST
                
                </body>
                
                </html>
                This is my Second.jsp:
                <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
                <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                
                <html>
                
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                <title>Second.jsp</title>
                
                <script type="text/javascript">
                function executeAfterLoad() {
                     alert("Second.jsp: inside executeAfterLoad()");
                }
                </script>
                </head>
                
                <body onload="executeAfterLoad();">
                
                SECOND
                
                </body>
                
                </html>
                This is my Test.java:
                package com.test;
                
                import java.io.IOException;
                import java.io.OutputStreamWriter;
                import java.io.UnsupportedEncodingException;
                import java.net.HttpURLConnection;
                import java.net.MalformedURLException;
                import java.net.URL;
                import java.net.URLEncoder;
                
                public class Test {
                
                     public void redirect() {
                          
                          HttpURLConnection connection = null;
                          
                          try {
                               String message = URLEncoder.encode("ABC", "UTF-8");
                               System.out.println("Test.java: redirect(): message=" + message);
                               
                               URL url = new URL("http://localhost:8080/JEE_Test/Second.jsp");
                               System.out.println("Test.java: redirect(): url=" + url);
                               
                               connection = (HttpURLConnection) url.openConnection();
                               System.out.println("Test.java: redirect(): connection=" + connection);
                               
                               connection.setDoOutput(true);
                               System.out.println("Test.java: redirect(): after connection.setDoOutput()");
                               
                               connection.setRequestMethod("POST");
                               System.out.println("Test.java: redirect(): after connection.setRequestMethod()");
                               
                               OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
                               System.out.println("Test.java: redirect(): writer=" + writer);
                               
                               writer.write("message=" + message);
                               System.out.println("Test.java: redirect(): after writer.write()");
                               
                               writer.flush();
                               System.out.println("Test.java: redirect(): after writer.flush()");
                               
                               writer.close();
                               System.out.println("Test.java: redirect(): after writer.close()");
                
                               System.out.println("Test.java: redirect(): connection.getResponseCode()=" + connection.getResponseCode());
                               System.out.println("Test.java: redirect(): HttpURLConnection.HTTP_OK=" + HttpURLConnection.HTTP_OK);
                               
                               // if there is a response code AND that response code is 200 OK, do stuff in the first if block
                             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                                 // OK
                
                             // otherwise, if any other status code is returned, or no status code is returned, do stuff in the else block
                             } else {
                                 // Server returned HTTP error code.
                             }
                             
                          } catch (UnsupportedEncodingException e) {
                               e.printStackTrace();
                               
                          } catch (MalformedURLException e) {
                               e.printStackTrace();
                               
                          } catch (IOException e) {
                               e.printStackTrace();
                               
                          } finally {
                               
                               if (connection != null)
                                    connection.disconnect();
                          }
                          
                          
                     }
                     
                }
                But this is not happening; I stay at First.jsp instead of getting Second.jsp. I am getting connection.getResponseCode() as 200.

                What I am doing wrong? Please help.

                Edited by: srhcan on Mar 5, 2013 5:44 PM

                Edited by: srhcan on Mar 5, 2013 5:44 PM

                Edited by: srhcan on Mar 5, 2013 5:45 PM
                • 5. Re: How to call JSP from a java class?
                  r035198x
                  You don't need HttpURLConnection to navigate between JSPs in the same web application. Read about how to use RequestDispatcher here http://docs.oracle.com/javaee/5/tutorial/doc/bnagi.html, or better yet dump, JSP and use JSF with facelets, it already comes with a versatile navigation mechanism.