• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 926
  • Last Modified:

Convert from PHP to JSP

Hi there,
Just wonder could anyone convert the following PHP code to JSP code.  Our aim is to get personal details from the DB and also results from another table to produce a similar letter to this using JSP:

---------
<?php
session_start(); //Starts the session for session variables header("Cache-control: private");
//generates letter for users who have requested feedback
//changes request field to blank to ensure no second copies only available to admin
?>
<html>

<head>
  <title>Report</title>
</head>

<body>

<?php

$db = mysql_connect("localhost", "root");
mysql_select_db("psytest",$db);

//check for students requesting feedback
$result = mysql_query("Select * FROM student WHERE Student_Feedback ='ON'");

//changes feedback to off
$sql = mysql_query("UPDATE student SET Student_Feedback = '' WHERE Student_Feedback ='ON'");

if($myrow = mysql_fetch_array($result))
      {

          do {
                  ?>

            <table border="0" width="100%">
                    <tr>
             <td width="15%">
      <img src="../images/WITLOGO.GIF" height=110 width = 85 border="0"><br>
                  </p>
            </td>
                <td width="85%"><p align ="right"><?php
            printf("%s", $myrow["Student_FirstName"]);
            echo " ";
            printf("%s", $myrow["Student_LastName"]);?><br><?php
            printf("%s", $myrow["Student_Address1"]);?><br><?php
            printf("%s", $myrow["Student_Address2"]);?><br><?php
            printf("%s", $myrow["Student_County"]);?><br><?php
            echo date (" l jS F Y");?><br><?php
                 ?>
            </p>
            </td>
                    </tr>
                  </table>
            <?php
            ?>
            <p align="left">
            <?php
            echo ("Psychometric Test Office,");?><br><?php
            echo ("DID,");?><br><?php
            echo ("Cork Campus,");?><br><?php
            echo ("Dublin Road,");?><br><?php
            echo ("Waterford.");?><br><?php
            echo ("<u>ptoffice@did.ie</u>");?><br><br>
            Dear <?php printf("%s", $myrow["Student_FirstName"]); ?>,<br><br>

            You have recently did a Psychometric Test and requested  further feedback on your test results in the form of a postal letter.  The results of your most recently taken test are as follows.
</p>
<p align = "center">
<?php
$name = $myrow['Student_Username'];
$rank = '1';

$db = mysql_connect("localhost", "root");

mysql_select_db("psytest",$db);

$sql = mysql_query("SELECT * FROM `ranklist` WHERE Student_Username = '$name' ORDER BY `Course_Score` DESC");

if($myrow = mysql_fetch_array($sql))

      {
          echo "<b>The following courses are ranked in order of suitability to your aptitude.</b><br><br>";
            echo "<table border=1 width=50%>\n";
            echo "<tr><td width=15%><b><font color =#0000dd><u>Rank</u></font></b></td><td width=65%><b><font color

=#0000dd><u>Course Title</u></font></b></td><td width=20%><b><font color =#0000dd><u>Course Score</u></font></b></td></tr>\n";

        do {

                  printf("<tr><td><b>$rank</b></td><td><font color =#dd0000>%s</font></td><td>%s</td></tr>\n",

$myrow["Course_Name"], $myrow["Course_Score"]);
            $rank++;
                }
          while ($myrow = mysql_fetch_array($sql));
                  echo "</table>\n";
    }
?>
<br>
<p align = "left">
Please also find enclosed a copy of the Application Handbook which contains further details on all the courses listed above.  If you have any further questions regarding any of the courses please do not hesitate to contact the lecturer listed in the Application Handbook<br><br>

Wishing you all the best with your future studies.<br><br>

Yours sincerely<br><br>

<font size = "6" face ="Freestyle Script">The Test Team</font><br>

</p>
<?php
?>
</p>
            <?php
                }
          while ($myrow = mysql_fetch_array($result));
    }
else {
?>
<p align = "center">
<?php
      echo "<b>There are no user letters currently available for printing!</b>";
?>
<?php
    }
?>
</body>
</html>
0
acslater
Asked:
acslater
  • 2
1 Solution
 
acslaterAuthor Commented:
Any ideas anyone!!!
0
 
bloodredsunCommented:
Yes, you need to go here http://www.rentacoder.com/RentACoder/default.asp where you can bid for people to do your work. Sorry to be a bit harsh but EE is a question and answer site, we're happy to help and assist but not do wholesale work.

Here is some code from an earlier answer that will show you how to do what you want in JSP. It shows you how to access the database, retrieve a resultset and iterate over a resultset to generate the page


------------.

ConnectionBean.java code // our javabean
<code>
package com.bloodredsun.beans ;
import java.sql.*;

public class Connectionbean{

private Connection conn ;
private Statement stmt ;
private static final String driver = "" ; // your db driver
private static final String dbURL= "" ; //your db url
private static final String login= "" ; // your db login
private static final String password= "" ; //your db password


  public ConnectionBean(){
    try{
      Class.forName( driver ) ;
      conn = DriverManager.getConnection(dbURL, login, password);
      stmt = conn.createStatement() ;

    }catch (Exception e){
      System.out.print("Whoops, buggered") ;
    }
  }//end ConnectionBbean constructor

      public ResultSet executeQuery ( String pSql){
      return stmt.executeQuery( pSql ) ;
      }
}//end ConnectionBean.java
</code>

Database.jsp code
<code>
<%@ page import="java.sql.*, com.bloodredsun.beans.*" %>
<jsp:useBean id="beanconnection" class="ConnectionBean" scope="request"/>
<html>
<body>
<%
String sql = "Select * from myTable" ;
ResultSet rs = beanconnection.executeQuery( sql) ;
%>
<%-- We now have the ResultSet, do what you will --%>
<%
while ( rs.next() ){
%>
A line of db stuff <%= rs.getString("id") %>

<%
}
%>
</body>
</html>
</code>

This is bare-arsed stuff, you will have to complete it with the normal extra methods in the bean for closing the connection, and other things like that and therer is minimal exception handling (in fact I've not tested this but written it straight out, but the theory is sound). What you can see is that Javabeans allow you to COMPONENTIZE code in that the code required for creating the db connection and statement were put in the JavaBean. This can then be dropped in as many pages as you like with out the need for repeatedly setting up the connection.

Once you have got used to JavaBeans in your JSP, I would recommend then doing it in a servlet. Then, once you've done that, have a look at custom tags and you'll see why JSTL is so popular (JSTL just being a set of standardised custom tags).

My next advice would be to look at dataSources and connection pools, this has a huge performance benefit for larger projects and also takes you into the world of JNDI (which isn't a frightening as it seems!).

Here's the JSTL example:
-------------------
You set the datasource:

<sql:setDataSource   var="dataSrc"  driver="oracle.jdbc.driver.OracleDriver"  url="jdbc:oracle:thin:@localhost:1521:ORCL"  user="username"  password="password" />

Then query like so:

 <sql:query var="queryResultsSet" dataSource="${dataSrc}"> <!-- here is the datasource from earlier -->
  select * from blog group by created desc limit ?
  <sql:param value="${6}"/>
</sql:query>


Good luck and happy coding.
0
 
acslaterAuthor Commented:
thanks for that, helped out
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now