Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
How to call JSP from a java class?

srhcan
Member Posts: 45
I have a java class Enrollment which has a method enroll() which returns a String.
Edited by: srhcan on Feb 28, 2013 8:00 AM
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
Answers
-
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).
-
Kayaman wrote:can you give example of this (how to create HTTP request and passing the value)? Thanks
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). -
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. -
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 -
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.
This discussion has been closed.