Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Building PHP classes

517214Jan 5 2008
i have been trying to run this scripts that i got from a text book(Php Oracle web

development) ,but everytime i run it my browser status bar message displays 'Done' but

nothing is displayed.

The scripts are:

<?php

//File: dbConn5.php

class dbConn5 {

private $user;

private $pswd;

private $db;

private $conn;

private $query;

private $row;

private $exec_mode;



public function __construct($user, $pswd, $db, $exec_mode=
OCI_COMMIT_ON_SUCCESS)

{

$this->user = $user;

$this->pswd = $pswd;

$this->db = $db;

$this->exec_mode = $exec_mode;

$this->GetConn();

}

private function GetConn()

{

if(!$this->conn = oci_connect($this->user, $this->pswd, $this->db))

{

$err = oci_error();

trigger_error('Failed to establish a connection: ' . $err['message']);

}

}

public function query($sql)

{

if(!$this->query = oci_parse($this->conn, $sql)) {

$err = oci_error($this->conn);

trigger_error('Failed to execute SQL query: ' . $err['message']);

return false;

}


else if(!oci_execute($this->query, $this->exec_mode)) {

$err = oci_error($this->query);

trigger_error('Failed to execute SQL query: ' . $err['message']);

return false;

}

return true;

}

public function fetch()

{

if($this->row=oci_fetch_assoc($this->query)){

return $this->row;

}

else {

return false;

}

}

}

?>

-------------------------------------------------------------------------------------------------------------------------------------

<?php

//File: hrCred.php

$user="scott";

$pswd="tiger";

$conn="(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))

)

(CONNECT_DATA =

(SERVICE_NAME = label)

)

)";

?>

----------------------------------------------------------------------------------------------------------------------------------



<html>

<body>

<?php

//File: select.php

include 'dbConn5.php';

include 'hrCred.php';

$db = new dbConn5($user, $pswd, $conn);

$sql="SELECT ENAME, DEPT FROM emp";

if($db->query($sql)){

print 'Employee Names: ' . '
';

while ($row = $db->fetch()) {

print $row['ENAME'] . '&nbsp;';

print $row['DEPT'] . '
';


}

}

?>

</body>

</html>



'Label' is the name of my database/sid while the php files are directly under the 'htdocs'
directory of my webserver.I am using ZendCore.

I am getting upset by this problem,i really need a solution.

Can anyone help????


Thanks
Charles Ajah

Comments

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).
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
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.
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
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.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 2 2008
Added on Jan 5 2008
0 comments
1,311 views